OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/glue/user_agent.h" | 5 #include "webkit/glue/user_agent.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <sys/utsname.h> | 8 #include <sys/utsname.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/synchronization/lock.h" |
14 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "googleurl/src/gurl.h" |
15 | 17 |
16 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
17 #include "base/win/windows_version.h" | 19 #include "base/win/windows_version.h" |
18 #endif | 20 #endif |
19 | 21 |
20 // Generated | 22 // Generated |
21 #include "webkit_version.h" // NOLINT | 23 #include "webkit_version.h" // NOLINT |
22 | 24 |
| 25 namespace { |
| 26 |
| 27 class UserAgentState { |
| 28 public: |
| 29 UserAgentState(); |
| 30 ~UserAgentState(); |
| 31 |
| 32 void Set(const std::string& user_agent, bool overriding); |
| 33 const std::string& Get(const GURL& url) const; |
| 34 |
| 35 private: |
| 36 mutable std::string user_agent_; |
| 37 // The UA string when we're pretending to be Mac Safari or Win Firefox. |
| 38 mutable std::string user_agent_for_spoofing_hack_; |
| 39 |
| 40 mutable bool user_agent_requested_; |
| 41 bool user_agent_is_overridden_; |
| 42 |
| 43 // This object can be accessed from multiple threads, so use a lock around |
| 44 // accesses to the data members. |
| 45 mutable base::Lock lock_; |
| 46 }; |
| 47 |
| 48 UserAgentState::UserAgentState() |
| 49 : user_agent_requested_(false), |
| 50 user_agent_is_overridden_(false) { |
| 51 } |
| 52 |
| 53 UserAgentState::~UserAgentState() { |
| 54 } |
| 55 |
| 56 void UserAgentState::Set(const std::string& user_agent, bool overriding) { |
| 57 base::AutoLock auto_lock(lock_); |
| 58 if (user_agent == user_agent_) { |
| 59 // We allow the user agent to be set multiple times as long as it |
| 60 // is set to the same value, in order to simplify unit testing |
| 61 // given g_user_agent is a global. |
| 62 return; |
| 63 } |
| 64 DCHECK(!user_agent.empty()); |
| 65 DCHECK(!user_agent_requested_) << "Setting the user agent after someone has " |
| 66 "already requested it can result in unexpected behavior."; |
| 67 user_agent_is_overridden_ = overriding; |
| 68 user_agent_ = user_agent; |
| 69 } |
| 70 |
| 71 bool IsMicrosoftSiteThatNeedsSpoofingForSilverlight(const GURL& url) { |
| 72 #if defined(OS_MACOSX) |
| 73 // The landing page for updating Silverlight gives a confusing experience |
| 74 // in browsers that Silverlight doesn't officially support; spoof as |
| 75 // Safari to reduce the chance that users won't complete updates. |
| 76 // Should be removed if the sniffing is removed: http://crbug.com/88211 |
| 77 if (url.host() == "www.microsoft.com" && |
| 78 StartsWithASCII(url.path(), "/getsilverlight", false)) { |
| 79 return true; |
| 80 } |
| 81 #endif |
| 82 return false; |
| 83 } |
| 84 |
| 85 bool IsYahooSiteThatNeedsSpoofingForSilverlight(const GURL& url) { |
| 86 #if defined(OS_MACOSX) || defined(OS_WIN) |
| 87 // The following Yahoo! JAPAN pages erroneously judge that Silverlight does |
| 88 // not support Chromium. Until the pages are fixed, spoof the UA. |
| 89 // http://crbug.com/104426 |
| 90 if (url.host() == "headlines.yahoo.co.jp" && |
| 91 StartsWithASCII(url.path(), "/videonews/", true)) { |
| 92 return true; |
| 93 } |
| 94 #endif |
| 95 #if defined(OS_MACOSX) |
| 96 if ((url.host() == "downloads.yahoo.co.jp" && |
| 97 StartsWithASCII(url.path(), "/docs/silverlight/", true)) || |
| 98 url.host() == "gyao.yahoo.co.jp") { |
| 99 return true; |
| 100 } |
| 101 #elif defined(OS_WIN) |
| 102 if ((url.host() == "weather.yahoo.co.jp" && |
| 103 StartsWithASCII(url.path(), "/weather/zoomradar/", true)) || |
| 104 url.host() == "promotion.shopping.yahoo.co.jp" || |
| 105 url.host() == "pokemon.kids.yahoo.co.jp") { |
| 106 return true; |
| 107 } |
| 108 #endif |
| 109 return false; |
| 110 } |
| 111 |
| 112 const std::string& UserAgentState::Get(const GURL& url) const { |
| 113 base::AutoLock auto_lock(lock_); |
| 114 user_agent_requested_ = true; |
| 115 |
| 116 DCHECK(!user_agent_.empty()); |
| 117 |
| 118 // Workarounds for sites that use misguided UA sniffing. |
| 119 if (!user_agent_is_overridden_) { |
| 120 if (IsMicrosoftSiteThatNeedsSpoofingForSilverlight(url) || |
| 121 IsYahooSiteThatNeedsSpoofingForSilverlight(url)) { |
| 122 if (user_agent_for_spoofing_hack_.empty()) { |
| 123 #if defined(OS_MACOSX) |
| 124 user_agent_for_spoofing_hack_ = webkit_glue::BuildUserAgentFromProduct( |
| 125 "Version/5.1.1 Safari/534.51.22"); |
| 126 #elif defined(OS_WIN) |
| 127 // Pretend to be Firefox. Silverlight doesn't support Win Safari. |
| 128 base::StringAppendF( |
| 129 &user_agent_for_spoofing_hack_, |
| 130 "Mozilla/5.0 (%s) Gecko/20100101 Firefox/8.0", |
| 131 webkit_glue::BuildOSCpuInfo().c_str()); |
| 132 #endif |
| 133 } |
| 134 DCHECK(!user_agent_for_spoofing_hack_.empty()); |
| 135 return user_agent_for_spoofing_hack_; |
| 136 } |
| 137 } |
| 138 |
| 139 return user_agent_; |
| 140 } |
| 141 |
| 142 base::LazyInstance<UserAgentState> g_user_agent = LAZY_INSTANCE_INITIALIZER; |
| 143 |
| 144 } // namespace |
| 145 |
23 namespace webkit_glue { | 146 namespace webkit_glue { |
24 | 147 |
| 148 void SetUserAgent(const std::string& user_agent, bool overriding) { |
| 149 g_user_agent.Get().Set(user_agent, overriding); |
| 150 } |
| 151 |
| 152 const std::string& GetUserAgent(const GURL& url) { |
| 153 return g_user_agent.Get().Get(url); |
| 154 } |
| 155 |
25 std::string GetWebKitVersion() { | 156 std::string GetWebKitVersion() { |
26 return base::StringPrintf("%d.%d (%s)", | 157 return base::StringPrintf("%d.%d (%s)", |
27 WEBKIT_VERSION_MAJOR, | 158 WEBKIT_VERSION_MAJOR, |
28 WEBKIT_VERSION_MINOR, | 159 WEBKIT_VERSION_MINOR, |
29 WEBKIT_SVN_REVISION); | 160 WEBKIT_SVN_REVISION); |
30 } | 161 } |
31 | 162 |
32 std::string GetWebKitRevision() { | 163 std::string GetWebKitRevision() { |
33 return WEBKIT_SVN_REVISION; | 164 return WEBKIT_SVN_REVISION; |
34 } | 165 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 webkit_glue::BuildOSCpuInfo().c_str(), | 300 webkit_glue::BuildOSCpuInfo().c_str(), |
170 WEBKIT_VERSION_MAJOR, | 301 WEBKIT_VERSION_MAJOR, |
171 WEBKIT_VERSION_MINOR, | 302 WEBKIT_VERSION_MINOR, |
172 product.c_str(), | 303 product.c_str(), |
173 WEBKIT_VERSION_MAJOR, | 304 WEBKIT_VERSION_MAJOR, |
174 WEBKIT_VERSION_MINOR); | 305 WEBKIT_VERSION_MINOR); |
175 return user_agent; | 306 return user_agent; |
176 } | 307 } |
177 | 308 |
178 } // namespace webkit_glue | 309 } // namespace webkit_glue |
OLD | NEW |