OLD | NEW |
1 // Copyright 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.h" | 5 #include "webkit/common/user_agent/user_agent.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.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" | 9 #include "base/synchronization/lock.h" |
12 #include "webkit/common/user_agent/user_agent_util.h" | |
13 | 10 |
14 namespace webkit_glue { | 11 namespace webkit_glue { |
15 | 12 |
16 namespace { | 13 namespace { |
17 | 14 |
18 class UserAgentState { | 15 class UserAgentState { |
19 public: | 16 public: |
20 UserAgentState(); | 17 UserAgentState(); |
21 ~UserAgentState(); | 18 ~UserAgentState(); |
22 | 19 |
23 void Set(const std::string& user_agent, bool overriding); | 20 void Set(const std::string& user_agent); |
24 const std::string& Get(const GURL& url) const; | 21 const std::string& Get() const; |
25 | 22 |
26 private: | 23 private: |
27 mutable std::string user_agent_; | 24 mutable std::string user_agent_; |
28 // The UA string when we're pretending to be Mac Safari or Win Firefox. | |
29 mutable std::string user_agent_for_spoofing_hack_; | |
30 | 25 |
31 mutable bool user_agent_requested_; | 26 mutable bool user_agent_requested_; |
32 bool user_agent_is_overridden_; | |
33 | 27 |
34 // This object can be accessed from multiple threads, so use a lock around | 28 // This object can be accessed from multiple threads, so use a lock around |
35 // accesses to the data members. | 29 // accesses to the data members. |
36 mutable base::Lock lock_; | 30 mutable base::Lock lock_; |
37 }; | 31 }; |
38 | 32 |
39 UserAgentState::UserAgentState() | 33 UserAgentState::UserAgentState() |
40 : user_agent_requested_(false), | 34 : user_agent_requested_(false) { |
41 user_agent_is_overridden_(false) { | |
42 } | 35 } |
43 | 36 |
44 UserAgentState::~UserAgentState() { | 37 UserAgentState::~UserAgentState() { |
45 } | 38 } |
46 | 39 |
47 void UserAgentState::Set(const std::string& user_agent, bool overriding) { | 40 void UserAgentState::Set(const std::string& user_agent) { |
48 base::AutoLock auto_lock(lock_); | 41 base::AutoLock auto_lock(lock_); |
49 if (user_agent == user_agent_) { | 42 if (user_agent == user_agent_) { |
50 // We allow the user agent to be set multiple times as long as it | 43 // We allow the user agent to be set multiple times as long as it |
51 // is set to the same value, in order to simplify unit testing | 44 // is set to the same value, in order to simplify unit testing |
52 // given g_user_agent is a global. | 45 // given g_user_agent is a global. |
53 return; | 46 return; |
54 } | 47 } |
55 DCHECK(!user_agent.empty()); | 48 DCHECK(!user_agent.empty()); |
56 DCHECK(!user_agent_requested_) << "Setting the user agent after someone has " | 49 DCHECK(!user_agent_requested_) << "Setting the user agent after someone has " |
57 "already requested it can result in unexpected behavior."; | 50 "already requested it can result in unexpected behavior."; |
58 user_agent_is_overridden_ = overriding; | |
59 user_agent_ = user_agent; | 51 user_agent_ = user_agent; |
60 } | 52 } |
61 | 53 |
62 bool IsMicrosoftSiteThatNeedsSpoofingForSilverlight(const GURL& url) { | 54 const std::string& UserAgentState::Get() const { |
63 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
64 // The landing page for updating Silverlight gives a confusing experience | |
65 // in browsers that Silverlight doesn't officially support; spoof as | |
66 // Safari to reduce the chance that users won't complete updates. | |
67 // Should be removed if the sniffing is removed: http://crbug.com/88211 | |
68 if (url.host() == "www.microsoft.com" && | |
69 StartsWithASCII(url.path(), "/getsilverlight", false)) { | |
70 return true; | |
71 } | |
72 #endif | |
73 return false; | |
74 } | |
75 | |
76 bool IsYahooSiteThatNeedsSpoofingForSilverlight(const GURL& url) { | |
77 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
78 if ((url.host() == "downloads.yahoo.co.jp" && | |
79 StartsWithASCII(url.path(), "/docs/silverlight/", true)) || | |
80 url.host() == "gyao.yahoo.co.jp") { | |
81 return true; | |
82 } | |
83 #elif defined(OS_WIN) | |
84 if (url.host() == "promotion.shopping.yahoo.co.jp") { | |
85 return true; | |
86 } | |
87 #endif | |
88 return false; | |
89 } | |
90 | |
91 const std::string& UserAgentState::Get(const GURL& url) const { | |
92 base::AutoLock auto_lock(lock_); | 55 base::AutoLock auto_lock(lock_); |
93 user_agent_requested_ = true; | 56 user_agent_requested_ = true; |
94 | 57 |
95 DCHECK(!user_agent_.empty()); | 58 DCHECK(!user_agent_.empty()); |
96 | 59 |
97 // Workarounds for sites that use misguided UA sniffing. | |
98 if (!user_agent_is_overridden_) { | |
99 if (IsMicrosoftSiteThatNeedsSpoofingForSilverlight(url) || | |
100 IsYahooSiteThatNeedsSpoofingForSilverlight(url)) { | |
101 if (user_agent_for_spoofing_hack_.empty()) { | |
102 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
103 user_agent_for_spoofing_hack_ = | |
104 BuildUserAgentFromProduct("Version/5.1.1 Safari/534.51.22"); | |
105 #elif defined(OS_WIN) | |
106 // Pretend to be Firefox. Silverlight doesn't support Win Safari. | |
107 base::StringAppendF( | |
108 &user_agent_for_spoofing_hack_, | |
109 "Mozilla/5.0 (%s) Gecko/20100101 Firefox/8.0", | |
110 webkit_glue::BuildOSCpuInfo().c_str()); | |
111 #endif | |
112 } | |
113 DCHECK(!user_agent_for_spoofing_hack_.empty()); | |
114 return user_agent_for_spoofing_hack_; | |
115 } | |
116 } | |
117 | |
118 return user_agent_; | 60 return user_agent_; |
119 } | 61 } |
120 | 62 |
121 base::LazyInstance<UserAgentState> g_user_agent = LAZY_INSTANCE_INITIALIZER; | 63 base::LazyInstance<UserAgentState> g_user_agent = LAZY_INSTANCE_INITIALIZER; |
122 | 64 |
123 } // namespace | 65 } // namespace |
124 | 66 |
125 void SetUserAgent(const std::string& user_agent, bool overriding) { | 67 void SetUserAgent(const std::string& user_agent) { |
126 g_user_agent.Get().Set(user_agent, overriding); | 68 g_user_agent.Get().Set(user_agent); |
127 } | 69 } |
128 | 70 |
129 const std::string& GetUserAgent(const GURL& url) { | 71 const std::string& GetUserAgent(const GURL& url) { |
130 return g_user_agent.Get().Get(url); | 72 return g_user_agent.Get().Get(); |
131 } | 73 } |
132 | 74 |
133 } // namespace webkit_glue | 75 } // namespace webkit_glue |
OLD | NEW |