OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/common/user_agent/user_agent_util.h" | 5 #include "content/public/common/user_agent.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "base/sys_info.h" |
| 13 #include "build/build_config.h" |
| 14 #include "content/common/user_agent_internal.h" |
6 | 15 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 16 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <sys/utsname.h> | 17 #include <sys/utsname.h> |
9 #endif | 18 #endif |
10 | 19 |
11 #include "base/lazy_instance.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/sys_info.h" | |
15 | |
16 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
17 #include "base/win/windows_version.h" | 21 #include "base/win/windows_version.h" |
18 #endif | 22 #endif |
19 | 23 |
20 // Generated | 24 // Generated |
21 #include "webkit_version.h" // NOLINT | 25 #include "webkit_version.h" // NOLINT |
22 | 26 |
23 namespace webkit_glue { | 27 namespace content { |
| 28 |
| 29 namespace { |
| 30 |
| 31 class UserAgentState { |
| 32 public: |
| 33 UserAgentState(); |
| 34 ~UserAgentState(); |
| 35 |
| 36 void Set(const std::string& user_agent, bool overriding); |
| 37 const std::string& Get(const GURL& url) const; |
| 38 |
| 39 private: |
| 40 mutable std::string user_agent_; |
| 41 // The UA string when we're pretending to be Mac Safari or Win Firefox. |
| 42 mutable std::string user_agent_for_spoofing_hack_; |
| 43 |
| 44 mutable bool user_agent_requested_; |
| 45 bool user_agent_is_overridden_; |
| 46 |
| 47 // This object can be accessed from multiple threads, so use a lock around |
| 48 // accesses to the data members. |
| 49 mutable base::Lock lock_; |
| 50 }; |
| 51 |
| 52 UserAgentState::UserAgentState() |
| 53 : user_agent_requested_(false), |
| 54 user_agent_is_overridden_(false) { |
| 55 } |
| 56 |
| 57 UserAgentState::~UserAgentState() { |
| 58 } |
| 59 |
| 60 void UserAgentState::Set(const std::string& user_agent, bool overriding) { |
| 61 base::AutoLock auto_lock(lock_); |
| 62 if (user_agent == user_agent_) { |
| 63 // We allow the user agent to be set multiple times as long as it |
| 64 // is set to the same value, in order to simplify unit testing |
| 65 // given g_user_agent is a global. |
| 66 return; |
| 67 } |
| 68 DCHECK(!user_agent.empty()); |
| 69 DCHECK(!user_agent_requested_) << "Setting the user agent after someone has " |
| 70 "already requested it can result in unexpected behavior."; |
| 71 user_agent_is_overridden_ = overriding; |
| 72 user_agent_ = user_agent; |
| 73 } |
| 74 |
| 75 bool IsMicrosoftSiteThatNeedsSpoofingForSilverlight(const GURL& url) { |
| 76 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 77 // The landing page for updating Silverlight gives a confusing experience |
| 78 // in browsers that Silverlight doesn't officially support; spoof as |
| 79 // Safari to reduce the chance that users won't complete updates. |
| 80 // Should be removed if the sniffing is removed: http://crbug.com/88211 |
| 81 if (url.host() == "www.microsoft.com" && |
| 82 StartsWithASCII(url.path(), "/getsilverlight", false)) { |
| 83 return true; |
| 84 } |
| 85 #endif |
| 86 return false; |
| 87 } |
| 88 |
| 89 bool IsYahooSiteThatNeedsSpoofingForSilverlight(const GURL& url) { |
| 90 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 91 if ((url.host() == "downloads.yahoo.co.jp" && |
| 92 StartsWithASCII(url.path(), "/docs/silverlight/", true)) || |
| 93 url.host() == "gyao.yahoo.co.jp") { |
| 94 return true; |
| 95 } |
| 96 #elif defined(OS_WIN) |
| 97 if (url.host() == "promotion.shopping.yahoo.co.jp") { |
| 98 return true; |
| 99 } |
| 100 #endif |
| 101 return false; |
| 102 } |
| 103 |
| 104 const std::string& UserAgentState::Get(const GURL& url) const { |
| 105 base::AutoLock auto_lock(lock_); |
| 106 user_agent_requested_ = true; |
| 107 |
| 108 DCHECK(!user_agent_.empty()); |
| 109 |
| 110 // Workarounds for sites that use misguided UA sniffing. |
| 111 if (!user_agent_is_overridden_) { |
| 112 if (IsMicrosoftSiteThatNeedsSpoofingForSilverlight(url) || |
| 113 IsYahooSiteThatNeedsSpoofingForSilverlight(url)) { |
| 114 if (user_agent_for_spoofing_hack_.empty()) { |
| 115 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 116 user_agent_for_spoofing_hack_ = |
| 117 BuildUserAgentFromProduct("Version/5.1.1 Safari/534.51.22"); |
| 118 #elif defined(OS_WIN) |
| 119 // Pretend to be Firefox. Silverlight doesn't support Win Safari. |
| 120 base::StringAppendF(&user_agent_for_spoofing_hack_, |
| 121 "Mozilla/5.0 (%s) Gecko/20100101 Firefox/8.0", |
| 122 BuildOSCpuInfo().c_str()); |
| 123 #endif |
| 124 } |
| 125 DCHECK(!user_agent_for_spoofing_hack_.empty()); |
| 126 return user_agent_for_spoofing_hack_; |
| 127 } |
| 128 } |
| 129 |
| 130 return user_agent_; |
| 131 } |
| 132 |
| 133 base::LazyInstance<UserAgentState> g_user_agent = LAZY_INSTANCE_INITIALIZER; |
| 134 |
| 135 } // namespace |
| 136 |
| 137 void SetUserAgent(const std::string& user_agent, bool overriding) { |
| 138 g_user_agent.Get().Set(user_agent, overriding); |
| 139 } |
| 140 |
| 141 const std::string& GetUserAgent(const GURL& url) { |
| 142 return g_user_agent.Get().Get(url); |
| 143 } |
24 | 144 |
25 std::string GetWebKitVersion() { | 145 std::string GetWebKitVersion() { |
26 return base::StringPrintf("%d.%d (%s)", | 146 return base::StringPrintf("%d.%d (%s)", |
27 WEBKIT_VERSION_MAJOR, | 147 WEBKIT_VERSION_MAJOR, |
28 WEBKIT_VERSION_MINOR, | 148 WEBKIT_VERSION_MINOR, |
29 WEBKIT_SVN_REVISION); | 149 WEBKIT_SVN_REVISION); |
30 } | 150 } |
31 | 151 |
32 std::string GetWebKitRevision() { | 152 std::string GetWebKitRevision() { |
33 return WEBKIT_SVN_REVISION; | 153 return WEBKIT_SVN_REVISION; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 "Macintosh; "; | 279 "Macintosh; "; |
160 #elif defined(USE_X11) | 280 #elif defined(USE_X11) |
161 "X11; "; // strange, but that's what Firefox uses | 281 "X11; "; // strange, but that's what Firefox uses |
162 #elif defined(OS_ANDROID) | 282 #elif defined(OS_ANDROID) |
163 "Linux; "; | 283 "Linux; "; |
164 #else | 284 #else |
165 "Unknown; "; | 285 "Unknown; "; |
166 #endif | 286 #endif |
167 | 287 |
168 std::string os_info; | 288 std::string os_info; |
169 base::StringAppendF(&os_info, "%s%s", kUserAgentPlatform, | 289 base::StringAppendF( |
170 webkit_glue::BuildOSCpuInfo().c_str()); | 290 &os_info, "%s%s", kUserAgentPlatform, BuildOSCpuInfo().c_str()); |
171 return BuildUserAgentFromOSAndProduct(os_info, product); | 291 return BuildUserAgentFromOSAndProduct(os_info, product); |
172 } | 292 } |
173 | 293 |
174 std::string BuildUserAgentFromOSAndProduct(const std::string& os_info, | 294 std::string BuildUserAgentFromOSAndProduct(const std::string& os_info, |
175 const std::string& product) { | 295 const std::string& product) { |
176 // Derived from Safari's UA string. | 296 // Derived from Safari's UA string. |
177 // This is done to expose our product name in a manner that is maximally | 297 // This is done to expose our product name in a manner that is maximally |
178 // compatible with Safari, we hope!! | 298 // compatible with Safari, we hope!! |
179 std::string user_agent; | 299 std::string user_agent; |
180 base::StringAppendF( | 300 base::StringAppendF( |
181 &user_agent, | 301 &user_agent, |
182 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d", | 302 "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d", |
183 os_info.c_str(), | 303 os_info.c_str(), |
184 WEBKIT_VERSION_MAJOR, | 304 WEBKIT_VERSION_MAJOR, |
185 WEBKIT_VERSION_MINOR, | 305 WEBKIT_VERSION_MINOR, |
186 product.c_str(), | 306 product.c_str(), |
187 WEBKIT_VERSION_MAJOR, | 307 WEBKIT_VERSION_MAJOR, |
188 WEBKIT_VERSION_MINOR); | 308 WEBKIT_VERSION_MINOR); |
189 return user_agent; | 309 return user_agent; |
190 } | 310 } |
191 | 311 |
192 } // namespace webkit_glue | 312 } // namespace content |
OLD | NEW |