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