[go: up one dir, main page]

[iOS] Fix the logging for the default mode on page load

This CL makes sure to record the IOS.PageLoad.DefaultModeMobile metric
at the same time as the official PageLoad metric.
Previously it was called too many times (in particular, it was called
for iframes).

(cherry picked from commit 9cf634e31fe51f7a23ab8230c9fc5cf01e4de215)

Fixed: 1340297
Change-Id: I82e53331d1d44796c31360dc41afaf83ed660748
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3733208
Reviewed-by: Ali Juma <ajuma@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1019523}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3748495
Auto-Submit: Gauthier Ambard <gambard@chromium.org>
Commit-Queue: Ali Juma <ajuma@chromium.org>
Cr-Commit-Position: refs/branch-heads/5112@{#657}
Cr-Branched-From: b13d3fe7b3c47a56354ef54b221008afa754412e-refs/heads/main@{#1012729}
diff --git a/ios/chrome/browser/web/chrome_web_client.h b/ios/chrome/browser/web/chrome_web_client.h
index 64f967c6..f283578 100644
--- a/ios/chrome/browser/web/chrome_web_client.h
+++ b/ios/chrome/browser/web/chrome_web_client.h
@@ -54,7 +54,9 @@
   bool EnableLongPressAndForceTouchHandling() const override;
   bool EnableLongPressUIContextMenu() const override;
   web::UserAgentType GetDefaultUserAgent(web::WebState* web_state,
-                                         const GURL& url) override;
+                                         const GURL& url) const override;
+  void LogDefaultUserAgent(web::WebState* web_state,
+                           const GURL& url) const override;
   bool RestoreSessionFromCache(web::WebState* web_state) const override;
   void CleanupNativeRestoreURLs(web::WebState* web_state) const override;
   void WillDisplayMediaCapturePermissionPrompt(
diff --git a/ios/chrome/browser/web/chrome_web_client.mm b/ios/chrome/browser/web/chrome_web_client.mm
index 70549c8..f040487 100644
--- a/ios/chrome/browser/web/chrome_web_client.mm
+++ b/ios/chrome/browser/web/chrome_web_client.mm
@@ -198,6 +198,18 @@
                             version_info::GetMajorVersionNumber().c_str());
 }
 
+// Whether the desktop user agent should be used by default.
+bool ShouldUseDesktop(web::WebState* web_state, const GURL& url) {
+  ChromeBrowserState* browser_state =
+      ChromeBrowserState::FromBrowserState(web_state->GetBrowserState());
+  HostContentSettingsMap* settings_map =
+      ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state);
+  ContentSetting setting = settings_map->GetContentSetting(
+      url, url, ContentSettingsType::REQUEST_DESKTOP_SITE);
+
+  return setting == CONTENT_SETTING_ALLOW;
+}
+
 }  // namespace
 
 ChromeWebClient::ChromeWebClient() {}
@@ -409,20 +421,19 @@
 
 web::UserAgentType ChromeWebClient::GetDefaultUserAgent(
     web::WebState* web_state,
-    const GURL& url) {
-  ChromeBrowserState* browser_state =
-      ChromeBrowserState::FromBrowserState(web_state->GetBrowserState());
-  HostContentSettingsMap* settings_map =
-      ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state);
-  ContentSetting setting = settings_map->GetContentSetting(
-      url, url, ContentSettingsType::REQUEST_DESKTOP_SITE);
-  bool use_desktop_agent = setting == CONTENT_SETTING_ALLOW;
-  base::UmaHistogramBoolean("IOS.PageLoad.DefaultModeMobile",
-                            !use_desktop_agent);
+    const GURL& url) const {
+  bool use_desktop_agent = ShouldUseDesktop(web_state, url);
   return use_desktop_agent ? web::UserAgentType::DESKTOP
                            : web::UserAgentType::MOBILE;
 }
 
+void ChromeWebClient::LogDefaultUserAgent(web::WebState* web_state,
+                                          const GURL& url) const {
+  bool use_desktop_agent = ShouldUseDesktop(web_state, url);
+  base::UmaHistogramBoolean("IOS.PageLoad.DefaultModeMobile",
+                            !use_desktop_agent);
+}
+
 bool ChromeWebClient::RestoreSessionFromCache(web::WebState* web_state) const {
   return WebSessionStateTabHelper::FromWebState(web_state)
       ->RestoreSessionFromCache();
diff --git a/ios/web/public/test/fakes/fake_web_client.h b/ios/web/public/test/fakes/fake_web_client.h
index 28e32f9d..0019e14a 100644
--- a/ios/web/public/test/fakes/fake_web_client.h
+++ b/ios/web/public/test/fakes/fake_web_client.h
@@ -53,7 +53,7 @@
                         base::OnceCallback<void(NSString*)> callback) override;
   UIView* GetWindowedContainer() override;
   UserAgentType GetDefaultUserAgent(web::WebState* web_state,
-                                    const GURL& url) override;
+                                    const GURL& url) const override;
 
   // Sets |plugin_not_supported_text_|.
   void SetPluginNotSupportedText(const std::u16string& text);
diff --git a/ios/web/public/test/fakes/fake_web_client.mm b/ios/web/public/test/fakes/fake_web_client.mm
index 1a5d8a5..ed54b686 100644
--- a/ios/web/public/test/fakes/fake_web_client.mm
+++ b/ios/web/public/test/fakes/fake_web_client.mm
@@ -99,7 +99,7 @@
 }
 
 UserAgentType FakeWebClient::GetDefaultUserAgent(web::WebState* web_state,
-                                                 const GURL& url) {
+                                                 const GURL& url) const {
   return default_user_agent_;
 }
 
diff --git a/ios/web/public/web_client.h b/ios/web/public/web_client.h
index e21c003..e28947e 100644
--- a/ios/web/public/web_client.h
+++ b/ios/web/public/web_client.h
@@ -174,7 +174,12 @@
   // Returns the UserAgentType that should be used by default for the web
   // content, based on the |web_state|.
   virtual UserAgentType GetDefaultUserAgent(web::WebState* web_state,
-                                            const GURL& url);
+                                            const GURL& url) const;
+
+  // Logs the default mode used (Mobile or Desktop). This is supposed to be
+  // called only if the user didn't force the mode.
+  virtual void LogDefaultUserAgent(web::WebState* web_state,
+                                   const GURL& url) const;
 
   // Returns true if URL was restored via session restoration cache.
   virtual bool RestoreSessionFromCache(web::WebState* web_state) const;
diff --git a/ios/web/web_client.mm b/ios/web/web_client.mm
index 7628691..4e0dea3 100644
--- a/ios/web/web_client.mm
+++ b/ios/web/web_client.mm
@@ -115,8 +115,11 @@
     web::WebState* web_state) const {}
 
 UserAgentType WebClient::GetDefaultUserAgent(web::WebState* web_state,
-                                             const GURL& url) {
+                                             const GURL& url) const {
   return UserAgentType::MOBILE;
 }
 
+void WebClient::LogDefaultUserAgent(web::WebState* web_state,
+                                    const GURL& url) const {}
+
 }  // namespace web
diff --git a/ios/web/web_state/ui/crw_web_request_controller.mm b/ios/web/web_state/ui/crw_web_request_controller.mm
index 2efe5f1..5257bad 100644
--- a/ios/web/web_state/ui/crw_web_request_controller.mm
+++ b/ios/web/web_state/ui/crw_web_request_controller.mm
@@ -389,6 +389,14 @@
       UMA_HISTOGRAM_MEDIUM_TIMES("PLT.iOS.BrowserInitiatedPageLoadTime2",
                                  context->GetElapsedTimeSinceCreation());
     }
+
+    // Use the Session Restoration User Agent as it is the only way to know if
+    // it is automatic or not.
+    if (self.webState->GetUserAgentForSessionRestoration() ==
+        web::UserAgentType::AUTOMATIC) {
+      web::GetWebClient()->LogDefaultUserAgent(self.webState,
+                                               context->GetUrl());
+    }
   }
 }