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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/search_engines/template_url_fetcher.h" | 7 #include "chrome/browser/search_engines/template_url_fetcher.h" |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" | |
13 #include "chrome/browser/search_engines/template_url_parser.h" | 11 #include "chrome/browser/search_engines/template_url_parser.h" |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
15 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | |
16 #include "components/search_engines/template_url.h" | 12 #include "components/search_engines/template_url.h" |
17 #include "components/search_engines/template_url_service.h" | 13 #include "components/search_engines/template_url_service.h" |
18 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
19 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/url_fetcher.h" | 17 #include "content/public/common/url_fetcher.h" |
22 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
23 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
24 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
| 21 #include "net/url_request/url_request_context_getter.h" |
25 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
26 | 23 |
27 // RequestDelegate ------------------------------------------------------------ | 24 // RequestDelegate ------------------------------------------------------------ |
28 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { | 25 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { |
29 public: | 26 public: |
30 // Takes ownership of |callbacks|. | 27 // Takes ownership of |callbacks|. |
31 RequestDelegate(TemplateURLFetcher* fetcher, | 28 RequestDelegate(TemplateURLFetcher* fetcher, |
32 const base::string16& keyword, | 29 const base::string16& keyword, |
33 const GURL& osdd_url, | 30 const GURL& osdd_url, |
34 const GURL& favicon_url, | 31 const GURL& favicon_url, |
35 content::WebContents* web_contents, | 32 content::WebContents* web_contents, |
36 TemplateURLFetcherCallbacks* callbacks, | 33 const ConfirmAddSearchProviderCallback& callback, |
37 ProviderType provider_type); | 34 ProviderType provider_type); |
38 | 35 |
39 // net::URLFetcherDelegate: | 36 // net::URLFetcherDelegate: |
40 // If data contains a valid OSDD, a TemplateURL is created and added to | 37 // If data contains a valid OSDD, a TemplateURL is created and added to |
41 // the TemplateURLService. | 38 // the TemplateURLService. |
42 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 39 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
43 | 40 |
44 // URL of the OSDD. | 41 // URL of the OSDD. |
45 GURL url() const { return osdd_url_; } | 42 GURL url() const { return osdd_url_; } |
46 | 43 |
47 // Keyword to use. | 44 // Keyword to use. |
48 base::string16 keyword() const { return keyword_; } | 45 base::string16 keyword() const { return keyword_; } |
49 | 46 |
50 // The type of search provider being fetched. | 47 // The type of search provider being fetched. |
51 ProviderType provider_type() const { return provider_type_; } | 48 ProviderType provider_type() const { return provider_type_; } |
52 | 49 |
53 private: | 50 private: |
54 void OnLoaded(); | 51 void OnLoaded(); |
55 void AddSearchProvider(); | 52 void AddSearchProvider(); |
56 | 53 |
57 scoped_ptr<net::URLFetcher> url_fetcher_; | 54 scoped_ptr<net::URLFetcher> url_fetcher_; |
58 TemplateURLFetcher* fetcher_; | 55 TemplateURLFetcher* fetcher_; |
59 scoped_ptr<TemplateURL> template_url_; | 56 scoped_ptr<TemplateURL> template_url_; |
60 base::string16 keyword_; | 57 base::string16 keyword_; |
61 const GURL osdd_url_; | 58 const GURL osdd_url_; |
62 const GURL favicon_url_; | 59 const GURL favicon_url_; |
63 const ProviderType provider_type_; | 60 const ProviderType provider_type_; |
64 scoped_ptr<TemplateURLFetcherCallbacks> callbacks_; | 61 ConfirmAddSearchProviderCallback callback_; |
65 | 62 |
66 scoped_ptr<TemplateURLService::Subscription> template_url_subscription_; | 63 scoped_ptr<TemplateURLService::Subscription> template_url_subscription_; |
67 | 64 |
68 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 65 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
69 }; | 66 }; |
70 | 67 |
71 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 68 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
72 TemplateURLFetcher* fetcher, | 69 TemplateURLFetcher* fetcher, |
73 const base::string16& keyword, | 70 const base::string16& keyword, |
74 const GURL& osdd_url, | 71 const GURL& osdd_url, |
75 const GURL& favicon_url, | 72 const GURL& favicon_url, |
76 content::WebContents* web_contents, | 73 content::WebContents* web_contents, |
77 TemplateURLFetcherCallbacks* callbacks, | 74 const ConfirmAddSearchProviderCallback& callback, |
78 ProviderType provider_type) | 75 ProviderType provider_type) |
79 : url_fetcher_(net::URLFetcher::Create( | 76 : url_fetcher_(net::URLFetcher::Create( |
80 osdd_url, net::URLFetcher::GET, this)), | 77 osdd_url, net::URLFetcher::GET, this)), |
81 fetcher_(fetcher), | 78 fetcher_(fetcher), |
82 keyword_(keyword), | 79 keyword_(keyword), |
83 osdd_url_(osdd_url), | 80 osdd_url_(osdd_url), |
84 favicon_url_(favicon_url), | 81 favicon_url_(favicon_url), |
85 provider_type_(provider_type), | 82 provider_type_(provider_type), |
86 callbacks_(callbacks) { | 83 callback_(callback) { |
87 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( | 84 TemplateURLService* model = fetcher_->template_url_service_; |
88 fetcher_->profile()); | |
89 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. | 85 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. |
90 | 86 |
91 if (!model->loaded()) { | 87 if (!model->loaded()) { |
92 // Start the model load and set-up waiting for it. | 88 // Start the model load and set-up waiting for it. |
93 template_url_subscription_ = model->RegisterOnLoadedCallback( | 89 template_url_subscription_ = model->RegisterOnLoadedCallback( |
94 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, | 90 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, |
95 base::Unretained(this))); | 91 base::Unretained(this))); |
96 model->Load(); | 92 model->Load(); |
97 } | 93 } |
98 | 94 |
99 url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext()); | 95 url_fetcher_->SetRequestContext(fetcher->request_context_.get()); |
100 // Can be NULL during tests. | 96 // Can be NULL during tests. |
101 if (web_contents) { | 97 if (web_contents) { |
102 content::AssociateURLFetcherWithRenderFrame( | 98 content::AssociateURLFetcherWithRenderFrame( |
103 url_fetcher_.get(), | 99 url_fetcher_.get(), |
104 web_contents->GetURL(), | 100 web_contents->GetURL(), |
105 web_contents->GetRenderProcessHost()->GetID(), | 101 web_contents->GetRenderProcessHost()->GetID(), |
106 web_contents->GetMainFrame()->GetRoutingID()); | 102 web_contents->GetMainFrame()->GetRoutingID()); |
107 } | 103 } |
108 | 104 |
109 url_fetcher_->Start(); | 105 url_fetcher_->Start(); |
(...skipping 18 matching lines...) Expand all Loading... |
128 std::string data; | 124 std::string data; |
129 if (!source->GetStatus().is_success() || | 125 if (!source->GetStatus().is_success() || |
130 ((source->GetResponseCode() != -1) && | 126 ((source->GetResponseCode() != -1) && |
131 (source->GetResponseCode() != 200)) || | 127 (source->GetResponseCode() != 200)) || |
132 !source->GetResponseAsString(&data)) { | 128 !source->GetResponseAsString(&data)) { |
133 fetcher_->RequestCompleted(this); | 129 fetcher_->RequestCompleted(this); |
134 // WARNING: RequestCompleted deletes us. | 130 // WARNING: RequestCompleted deletes us. |
135 return; | 131 return; |
136 } | 132 } |
137 | 133 |
138 template_url_.reset(TemplateURLParser::Parse(fetcher_->profile(), false, | 134 template_url_.reset(TemplateURLParser::Parse( |
| 135 fetcher_->template_url_service_->search_terms_data(), false, |
139 data.data(), data.length(), NULL)); | 136 data.data(), data.length(), NULL)); |
140 if (!template_url_.get() || | 137 if (!template_url_.get() || |
141 !template_url_->url_ref().SupportsReplacement( | 138 !template_url_->url_ref().SupportsReplacement( |
142 UIThreadSearchTermsData(fetcher_->profile()))) { | 139 fetcher_->template_url_service_->search_terms_data())) { |
143 fetcher_->RequestCompleted(this); | 140 fetcher_->RequestCompleted(this); |
144 // WARNING: RequestCompleted deletes us. | 141 // WARNING: RequestCompleted deletes us. |
145 return; | 142 return; |
146 } | 143 } |
147 | 144 |
148 if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) { | 145 if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) { |
149 // Use the parser-generated new keyword from the URL in the OSDD for the | 146 // Use the parser-generated new keyword from the URL in the OSDD for the |
150 // non-autodetected case. The existing |keyword_| was generated from the | 147 // non-autodetected case. The existing |keyword_| was generated from the |
151 // URL that hosted the OSDD, which results in the wrong keyword when the | 148 // URL that hosted the OSDD, which results in the wrong keyword when the |
152 // OSDD was located on a third-party site that has nothing in common with | 149 // OSDD was located on a third-party site that has nothing in common with |
153 // search engine described by OSDD. | 150 // search engine described by OSDD. |
154 keyword_ = template_url_->keyword(); | 151 keyword_ = template_url_->keyword(); |
155 DCHECK(!keyword_.empty()); | 152 DCHECK(!keyword_.empty()); |
156 } | 153 } |
157 | 154 |
158 // Wait for the model to be loaded before adding the provider. | 155 // Wait for the model to be loaded before adding the provider. |
159 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( | 156 if (!fetcher_->template_url_service_->loaded()) |
160 fetcher_->profile()); | |
161 if (!model->loaded()) | |
162 return; | 157 return; |
163 AddSearchProvider(); | 158 AddSearchProvider(); |
164 // WARNING: AddSearchProvider deletes us. | 159 // WARNING: AddSearchProvider deletes us. |
165 } | 160 } |
166 | 161 |
167 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { | 162 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { |
168 DCHECK(template_url_.get()); | 163 DCHECK(template_url_.get()); |
169 DCHECK(!keyword_.empty()); | 164 DCHECK(!keyword_.empty()); |
170 Profile* profile = fetcher_->profile(); | 165 TemplateURLService* model = fetcher_->template_url_service_; |
171 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); | |
172 DCHECK(model); | 166 DCHECK(model); |
173 DCHECK(model->loaded()); | 167 DCHECK(model->loaded()); |
174 | 168 |
175 TemplateURL* existing_url = NULL; | 169 TemplateURL* existing_url = NULL; |
176 if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()), | 170 if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()), |
177 &existing_url)) { | 171 &existing_url)) { |
178 if (existing_url) | 172 if (existing_url) |
179 model->Remove(existing_url); | 173 model->Remove(existing_url); |
180 } else if (provider_type_ == AUTODETECTED_PROVIDER) { | 174 } else if (provider_type_ == AUTODETECTED_PROVIDER) { |
181 fetcher_->RequestCompleted(this); // WARNING: Deletes us! | 175 fetcher_->RequestCompleted(this); // WARNING: Deletes us! |
(...skipping 17 matching lines...) Expand all Loading... |
199 model->Add(new TemplateURL(data)); | 193 model->Add(new TemplateURL(data)); |
200 break; | 194 break; |
201 | 195 |
202 case EXPLICIT_PROVIDER: | 196 case EXPLICIT_PROVIDER: |
203 // Confirm addition and allow user to edit default choices. It's ironic | 197 // Confirm addition and allow user to edit default choices. It's ironic |
204 // that only *non*-autodetected additions get confirmed, but the user | 198 // that only *non*-autodetected additions get confirmed, but the user |
205 // expects feedback that his action did something. | 199 // expects feedback that his action did something. |
206 // The source WebContents' delegate takes care of adding the URL to the | 200 // The source WebContents' delegate takes care of adding the URL to the |
207 // model, which takes ownership, or of deleting it if the add is | 201 // model, which takes ownership, or of deleting it if the add is |
208 // cancelled. | 202 // cancelled. |
209 callbacks_->ConfirmAddSearchProvider(new TemplateURL(data), profile); | 203 callback_.Run(make_scoped_ptr(new TemplateURL(data))); |
210 break; | 204 break; |
211 | 205 |
212 default: | 206 default: |
213 NOTREACHED(); | 207 NOTREACHED(); |
214 break; | 208 break; |
215 } | 209 } |
216 | 210 |
217 fetcher_->RequestCompleted(this); | 211 fetcher_->RequestCompleted(this); |
218 // WARNING: RequestCompleted deletes us. | 212 // WARNING: RequestCompleted deletes us. |
219 } | 213 } |
220 | 214 |
221 // TemplateURLFetcher --------------------------------------------------------- | 215 // TemplateURLFetcher --------------------------------------------------------- |
222 | 216 |
223 TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) { | 217 TemplateURLFetcher::TemplateURLFetcher( |
224 DCHECK(profile_); | 218 TemplateURLService* template_url_service, |
| 219 net::URLRequestContextGetter* request_context) |
| 220 : template_url_service_(template_url_service), |
| 221 request_context_(request_context) { |
225 } | 222 } |
226 | 223 |
227 TemplateURLFetcher::~TemplateURLFetcher() { | 224 TemplateURLFetcher::~TemplateURLFetcher() { |
228 } | 225 } |
229 | 226 |
230 void TemplateURLFetcher::ScheduleDownload( | 227 void TemplateURLFetcher::ScheduleDownload( |
231 const base::string16& keyword, | 228 const base::string16& keyword, |
232 const GURL& osdd_url, | 229 const GURL& osdd_url, |
233 const GURL& favicon_url, | 230 const GURL& favicon_url, |
234 content::WebContents* web_contents, | 231 content::WebContents* web_contents, |
235 TemplateURLFetcherCallbacks* callbacks, | 232 const ConfirmAddSearchProviderCallback& callback, |
236 ProviderType provider_type) { | 233 ProviderType provider_type) { |
237 DCHECK(osdd_url.is_valid()); | 234 DCHECK(osdd_url.is_valid()); |
238 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); | |
239 | |
240 TemplateURLService* url_model = | |
241 TemplateURLServiceFactory::GetForProfile(profile()); | |
242 if (!url_model) | |
243 return; | |
244 | 235 |
245 // For a JS-added OSDD, the provided keyword is irrelevant because we will | 236 // For a JS-added OSDD, the provided keyword is irrelevant because we will |
246 // generate a keyword later from the OSDD content. For the autodetected case, | 237 // generate a keyword later from the OSDD content. For the autodetected case, |
247 // we need a valid keyword up front. | 238 // we need a valid keyword up front. |
248 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 239 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
249 DCHECK(!keyword.empty()); | 240 DCHECK(!keyword.empty()); |
250 | 241 |
251 if (!url_model->loaded()) { | 242 if (!template_url_service_->loaded()) { |
252 // We could try to set up a callback to this function again once the model | 243 // We could try to set up a callback to this function again once the model |
253 // is loaded but since this is an auto-add case anyway, meh. | 244 // is loaded but since this is an auto-add case anyway, meh. |
254 url_model->Load(); | 245 template_url_service_->Load(); |
255 return; | 246 return; |
256 } | 247 } |
257 | 248 |
258 const TemplateURL* template_url = | 249 const TemplateURL* template_url = |
259 url_model->GetTemplateURLForKeyword(keyword); | 250 template_url_service_->GetTemplateURLForKeyword(keyword); |
260 if (template_url && (!template_url->safe_for_autoreplace() || | 251 if (template_url && (!template_url->safe_for_autoreplace() || |
261 template_url->originating_url() == osdd_url)) | 252 template_url->originating_url() == osdd_url)) |
262 return; | 253 return; |
263 } | 254 } |
264 | 255 |
265 // Make sure we aren't already downloading this request. | 256 // Make sure we aren't already downloading this request. |
266 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 257 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
267 if (((*i)->url() == osdd_url) || | 258 if (((*i)->url() == osdd_url) || |
268 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && | 259 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && |
269 ((*i)->keyword() == keyword))) | 260 ((*i)->keyword() == keyword))) |
270 return; | 261 return; |
271 } | 262 } |
272 | 263 |
273 requests_.push_back( | 264 requests_.push_back( |
274 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, | 265 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, |
275 owned_callbacks.release(), provider_type)); | 266 callback, provider_type)); |
276 } | 267 } |
277 | 268 |
278 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 269 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
279 Requests::iterator i = | 270 Requests::iterator i = |
280 std::find(requests_.begin(), requests_.end(), request); | 271 std::find(requests_.begin(), requests_.end(), request); |
281 DCHECK(i != requests_.end()); | 272 DCHECK(i != requests_.end()); |
282 requests_.weak_erase(i); | 273 requests_.weak_erase(i); |
283 delete request; | 274 delete request; |
284 } | 275 } |
OLD | NEW |