[Chromecast] Refactor UserAgent

1) Move the GetUserAgent() into its own files. 2) Remove
unused |user_agent| in GCMDriverDesktop to reduce confusion.

Merge-With: eureka-internal/631073

Bug: b/192098277, b/191670403
Test: Log the user agent and verify it's the expected value. This CL
also passes internal CQ.

Change-Id: I0603482337a0117e4ab626233961849fea1a8b2d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3059240
Commit-Queue: Guohui Deng <[email protected]>
Reviewed-by: Sean Topping <[email protected]>
Reviewed-by: Filip Gorski <[email protected]>
Cr-Commit-Position: refs/heads/master@{#909512}
diff --git a/chromecast/browser/cast_content_browser_client.cc b/chromecast/browser/cast_content_browser_client.cc
index eb5c93b4..5bd39647 100644
--- a/chromecast/browser/cast_content_browser_client.cc
+++ b/chromecast/browser/cast_content_browser_client.cc
@@ -51,8 +51,8 @@
 #include "chromecast/browser/service_connector.h"
 #include "chromecast/browser/service_manager_connection.h"
 #include "chromecast/browser/service_manager_context.h"
-#include "chromecast/common/cast_content_client.h"
 #include "chromecast/common/global_descriptors.h"
+#include "chromecast/common/user_agent.h"
 #include "chromecast/media/audio/cast_audio_manager.h"
 #include "chromecast/media/cdm/cast_cdm_factory.h"
 #include "chromecast/media/cdm/cast_cdm_origin_provider.h"
@@ -956,7 +956,7 @@
 }
 
 std::string CastContentBrowserClient::GetUserAgent() {
-  return chromecast::shell::GetUserAgent();
+  return chromecast::GetUserAgent();
 }
 
 void CastContentBrowserClient::CreateGeneralAudienceBrowsingService() {
diff --git a/chromecast/browser/cast_http_user_agent_settings.cc b/chromecast/browser/cast_http_user_agent_settings.cc
index 2d7916a..587c5b1 100644
--- a/chromecast/browser/cast_http_user_agent_settings.cc
+++ b/chromecast/browser/cast_http_user_agent_settings.cc
@@ -8,7 +8,7 @@
 #include "base/logging.h"
 #include "build/build_config.h"
 #include "chromecast/app/grit/chromecast_settings.h"
-#include "chromecast/common/cast_content_client.h"
+#include "chromecast/common/user_agent.h"
 #include "content/public/browser/browser_thread.h"
 #include "net/http/http_util.h"
 #include "ui/base/l10n/l10n_util.h"
@@ -66,7 +66,7 @@
 }
 
 std::string CastHttpUserAgentSettings::GetUserAgent() const {
-  return chromecast::shell::GetUserAgent();
+  return chromecast::GetUserAgent();
 }
 
 std::string CastHttpUserAgentSettings::AcceptLanguage() {
diff --git a/chromecast/browser/cast_network_contexts.cc b/chromecast/browser/cast_network_contexts.cc
index ee79ac0..674afdba 100644
--- a/chromecast/browser/cast_network_contexts.cc
+++ b/chromecast/browser/cast_network_contexts.cc
@@ -17,7 +17,7 @@
 #include "chromecast/browser/cast_browser_context.h"
 #include "chromecast/browser/cast_browser_process.h"
 #include "chromecast/browser/cast_http_user_agent_settings.h"
-#include "chromecast/common/cast_content_client.h"
+#include "chromecast/common/user_agent.h"
 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
 #include "components/variations/net/variations_http_headers.h"
 #include "content/public/browser/browser_context.h"
diff --git a/chromecast/browser/webview/web_content_controller.cc b/chromecast/browser/webview/web_content_controller.cc
index db8fcf14..3ee0a5f 100644
--- a/chromecast/browser/webview/web_content_controller.cc
+++ b/chromecast/browser/webview/web_content_controller.cc
@@ -15,7 +15,7 @@
 #include "chromecast/browser/webview/proto/webview.pb.h"
 #include "chromecast/browser/webview/webview_input_method_observer.h"
 #include "chromecast/browser/webview/webview_navigation_throttle.h"
-#include "chromecast/common/cast_content_client.h"
+#include "chromecast/common/user_agent.h"
 #include "chromecast/graphics/cast_focus_client_aura.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/browsing_data_remover.h"
@@ -569,7 +569,7 @@
       std::make_unique<webview::WebviewResponse>();
 
   response->set_id(id);
-  response->mutable_get_user_agent()->set_user_agent(shell::GetUserAgent());
+  response->mutable_get_user_agent()->set_user_agent(GetUserAgent());
   client_->EnqueueSend(std::move(response));
 }
 
diff --git a/chromecast/common/BUILD.gn b/chromecast/common/BUILD.gn
index 3e107cf8..f785c26d 100644
--- a/chromecast/common/BUILD.gn
+++ b/chromecast/common/BUILD.gn
@@ -83,6 +83,8 @@
     "global_descriptors.h",
     "identification_settings_manager.cc",
     "identification_settings_manager.h",
+    "user_agent.cc",
+    "user_agent.h",
   ]
 
   public_deps = [ "//base" ]
@@ -137,6 +139,5 @@
   if (enable_library_cdms) {
     deps += [ "//media/cdm:cdm_paths" ]  # Needed by cast_content_client.cc
   }
-
   defines = [ "DEVICE_USER_AGENT_SUFFIX=\"${device_user_agent_suffix}\"" ]
 }
diff --git a/chromecast/common/cast_content_client.cc b/chromecast/common/cast_content_client.cc
index 8b693d9a..45abda9 100644
--- a/chromecast/common/cast_content_client.cc
+++ b/chromecast/common/cast_content_client.cc
@@ -12,9 +12,7 @@
 #include "base/files/file_util.h"
 #include "base/native_library.h"
 #include "base/path_service.h"
-#include "base/strings/strcat.h"
-#include "base/strings/stringprintf.h"
-#include "base/system/sys_info.h"
+#include "base/strings/string_piece.h"
 #include "build/build_config.h"
 #include "chromecast/base/cast_constants.h"
 #include "chromecast/base/cast_paths.h"
@@ -22,7 +20,6 @@
 #include "chromecast/chromecast_buildflags.h"
 #include "components/cast/common/constants.h"
 #include "content/public/common/cdm_info.h"
-#include "content/public/common/user_agent.h"
 #include "media/base/media_switches.h"
 #include "media/media_buildflags.h"
 #include "mojo/public/cpp/bindings/binder_map.h"
@@ -64,37 +61,6 @@
 
 namespace {
 
-#if defined(OS_ANDROID)
-std::string BuildAndroidOsInfo() {
-  int32_t os_major_version = 0;
-  int32_t os_minor_version = 0;
-  int32_t os_bugfix_version = 0;
-  base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
-                                               &os_minor_version,
-                                               &os_bugfix_version);
-
-  std::string android_version_str;
-  base::StringAppendF(
-      &android_version_str, "%d.%d", os_major_version, os_minor_version);
-  if (os_bugfix_version != 0)
-    base::StringAppendF(&android_version_str, ".%d", os_bugfix_version);
-
-  std::string android_info_str;
-  // Append the build ID.
-  std::string android_build_id = base::SysInfo::GetAndroidBuildID();
-  if (android_build_id.size() > 0)
-    android_info_str += "; Build/" + android_build_id;
-
-  std::string os_info;
-  base::StringAppendF(
-      &os_info,
-      "Android %s%s",
-      android_version_str.c_str(),
-      android_info_str.c_str());
-  return os_info;
-}
-#endif
-
 #if BUILDFLAG(BUNDLE_WIDEVINE_CDM) && defined(OS_LINUX)
 // Copied from chrome_content_client.cc
 std::unique_ptr<content::CdmInfo> CreateWidevineCdmInfo(
@@ -156,40 +122,8 @@
 }
 #endif  // BUILDFLAG(BUNDLE_WIDEVINE_CDM) && defined(OS_LINUX)
 
-std::string GetControlKey() {
-  std::string control_key = base::StrCat({"CrKey/", kFrozenCrKeyValue});
-  return control_key;
-}
-
-std::string GetDeviceUserAgentSuffix() {
-  return std::string(DEVICE_USER_AGENT_SUFFIX);
-}
-
 }  // namespace
 
-std::string GetUserAgent() {
-  std::string product = "Chrome/" PRODUCT_VERSION;
-  std::string os_info;
-  base::StringAppendF(
-      &os_info,
-      "%s%s",
-#if defined(OS_ANDROID)
-      "Linux; ",
-      BuildAndroidOsInfo().c_str()
-#elif BUILDFLAG(USE_ANDROID_USER_AGENT)
-                      "Linux; ", "Android"
-#else
-      "X11; ",
-      content::BuildOSCpuInfo(content::IncludeAndroidBuildNumber::Exclude,
-                              content::IncludeAndroidModel::Include)
-          .c_str()
-#endif
-      );
-  return base::StrCat(
-      {content::BuildUserAgentFromOSAndProduct(os_info, product), " ",
-       GetControlKey(), " ", GetDeviceUserAgentSuffix()});
-}
-
 CastContentClient::~CastContentClient() {
 }
 
diff --git a/chromecast/common/cast_content_client.h b/chromecast/common/cast_content_client.h
index eca825f..2810340 100644
--- a/chromecast/common/cast_content_client.h
+++ b/chromecast/common/cast_content_client.h
@@ -7,15 +7,13 @@
 
 #include <vector>
 
+#include "build/build_config.h"
 #include "content/public/common/content_client.h"
 #include "url/gurl.h"
 
 namespace chromecast {
 namespace shell {
 
-// TODO(halliwell) Move this function to its own header.
-std::string GetUserAgent();
-
 class CastContentClient : public content::ContentClient {
  public:
   ~CastContentClient() override;
diff --git a/chromecast/common/user_agent.cc b/chromecast/common/user_agent.cc
new file mode 100644
index 0000000..f4d6c158
--- /dev/null
+++ b/chromecast/common/user_agent.cc
@@ -0,0 +1,80 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromecast/common/user_agent.h"
+
+#include "base/logging.h"
+#include "base/strings/strcat.h"
+#include "base/strings/stringprintf.h"
+#include "base/system/sys_info.h"
+#include "build/build_config.h"
+#include "chromecast/base/version.h"
+#include "chromecast/chromecast_buildflags.h"
+#include "components/cast/common/constants.h"
+#include "content/public/common/user_agent.h"
+
+namespace chromecast {
+
+namespace {
+
+#if defined(OS_ANDROID)
+std::string BuildAndroidOsInfo() {
+  int32_t os_major_version = 0;
+  int32_t os_minor_version = 0;
+  int32_t os_bugfix_version = 0;
+  base::SysInfo::OperatingSystemVersionNumbers(
+      &os_major_version, &os_minor_version, &os_bugfix_version);
+
+  std::string android_version_str;
+  base::StringAppendF(&android_version_str, "%d.%d", os_major_version,
+                      os_minor_version);
+  if (os_bugfix_version != 0)
+    base::StringAppendF(&android_version_str, ".%d", os_bugfix_version);
+
+  std::string android_info_str;
+  // Append the build ID.
+  std::string android_build_id = base::SysInfo::GetAndroidBuildID();
+  if (android_build_id.size() > 0)
+    android_info_str += "; Build/" + android_build_id;
+
+  std::string os_info;
+  base::StringAppendF(&os_info, "Android %s%s", android_version_str.c_str(),
+                      android_info_str.c_str());
+  return os_info;
+}
+#endif
+
+std::string GetChromeKeyString() {
+  std::string chrome_key = base::StrCat({"CrKey/", kFrozenCrKeyValue});
+  return chrome_key;
+}
+
+std::string GetDeviceUserAgentSuffix() {
+  return std::string(DEVICE_USER_AGENT_SUFFIX);
+}
+
+}  // namespace
+
+std::string GetUserAgent() {
+  std::string product = "Chrome/" PRODUCT_VERSION;
+  std::string os_info;
+  base::StringAppendF(&os_info, "%s%s",
+#if defined(OS_ANDROID)
+                      "Linux; ", BuildAndroidOsInfo().c_str()
+#elif BUILDFLAG(USE_ANDROID_USER_AGENT)
+                      "Linux; ", "Android"
+#else
+                      "X11; ",
+                      content::BuildOSCpuInfo(
+                          content::IncludeAndroidBuildNumber::Exclude,
+                          content::IncludeAndroidModel::Include)
+                          .c_str()
+#endif
+  );
+  return base::StrCat(
+      {content::BuildUserAgentFromOSAndProduct(os_info, product), " ",
+       GetChromeKeyString(), " ", GetDeviceUserAgentSuffix()});
+}
+
+}  // namespace chromecast
diff --git a/chromecast/common/user_agent.h b/chromecast/common/user_agent.h
new file mode 100644
index 0000000..221bb22
--- /dev/null
+++ b/chromecast/common/user_agent.h
@@ -0,0 +1,16 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMECAST_COMMON_USER_AGENT_H_
+#define CHROMECAST_COMMON_USER_AGENT_H_
+
+#include <string>
+
+namespace chromecast {
+
+std::string GetUserAgent();
+
+}  // namespace chromecast
+
+#endif  // CHROMECAST_COMMON_USER_AGENT_H_