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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
13 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" | 13 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
16 #include "chrome/browser/search_engines/template_url_parser.h" | 16 #include "chrome/browser/search_engines/template_url_parser.h" |
17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "content/browser/renderer_host/render_view_host.h" |
18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/content_url_request_user_data.h" |
21 #include "content/public/common/url_fetcher.h" | 25 #include "content/public/common/url_fetcher.h" |
22 #include "content/public/common/url_fetcher_delegate.h" | 26 #include "content/public/common/url_fetcher_delegate.h" |
23 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
24 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
25 | 29 |
26 // RequestDelegate ------------------------------------------------------------ | 30 // RequestDelegate ------------------------------------------------------------ |
27 class TemplateURLFetcher::RequestDelegate | 31 class TemplateURLFetcher::RequestDelegate |
28 : public content::URLFetcherDelegate, | 32 : public content::URLFetcherDelegate, |
29 public content::NotificationObserver { | 33 public content::NotificationObserver { |
30 public: | 34 public: |
31 // Takes ownership of |callbacks|. | 35 // Takes ownership of |callbacks|. |
32 RequestDelegate(TemplateURLFetcher* fetcher, | 36 RequestDelegate(TemplateURLFetcher* fetcher, |
33 const string16& keyword, | 37 const string16& keyword, |
34 const GURL& osdd_url, | 38 const GURL& osdd_url, |
35 const GURL& favicon_url, | 39 const GURL& favicon_url, |
| 40 content::WebContents* web_contents, |
36 TemplateURLFetcherCallbacks* callbacks, | 41 TemplateURLFetcherCallbacks* callbacks, |
37 ProviderType provider_type); | 42 ProviderType provider_type); |
38 | 43 |
39 // content::NotificationObserver: | 44 // content::NotificationObserver: |
40 virtual void Observe(int type, | 45 virtual void Observe(int type, |
41 const content::NotificationSource& source, | 46 const content::NotificationSource& source, |
42 const content::NotificationDetails& details); | 47 const content::NotificationDetails& details); |
43 | 48 |
44 // content::URLFetcherDelegate: | 49 // content::URLFetcherDelegate: |
45 // If data contains a valid OSDD, a TemplateURL is created and added to | 50 // If data contains a valid OSDD, a TemplateURL is created and added to |
(...skipping 25 matching lines...) Expand all Loading... |
71 content::NotificationRegistrar registrar_; | 76 content::NotificationRegistrar registrar_; |
72 | 77 |
73 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 78 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
74 }; | 79 }; |
75 | 80 |
76 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 81 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
77 TemplateURLFetcher* fetcher, | 82 TemplateURLFetcher* fetcher, |
78 const string16& keyword, | 83 const string16& keyword, |
79 const GURL& osdd_url, | 84 const GURL& osdd_url, |
80 const GURL& favicon_url, | 85 const GURL& favicon_url, |
| 86 content::WebContents* web_contents, |
81 TemplateURLFetcherCallbacks* callbacks, | 87 TemplateURLFetcherCallbacks* callbacks, |
82 ProviderType provider_type) | 88 ProviderType provider_type) |
83 : ALLOW_THIS_IN_INITIALIZER_LIST(url_fetcher_(content::URLFetcher::Create( | 89 : ALLOW_THIS_IN_INITIALIZER_LIST(url_fetcher_(content::URLFetcher::Create( |
84 osdd_url, content::URLFetcher::GET, this))), | 90 osdd_url, content::URLFetcher::GET, this))), |
85 fetcher_(fetcher), | 91 fetcher_(fetcher), |
86 keyword_(keyword), | 92 keyword_(keyword), |
87 osdd_url_(osdd_url), | 93 osdd_url_(osdd_url), |
88 favicon_url_(favicon_url), | 94 favicon_url_(favicon_url), |
89 provider_type_(provider_type), | 95 provider_type_(provider_type), |
90 callbacks_(callbacks) { | 96 callbacks_(callbacks) { |
91 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( | 97 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( |
92 fetcher_->profile()); | 98 fetcher_->profile()); |
93 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. | 99 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. |
94 | 100 |
95 if (!model->loaded()) { | 101 if (!model->loaded()) { |
96 // Start the model load and set-up waiting for it. | 102 // Start the model load and set-up waiting for it. |
97 registrar_.Add(this, | 103 registrar_.Add(this, |
98 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | 104 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
99 content::Source<TemplateURLService>(model)); | 105 content::Source<TemplateURLService>(model)); |
100 model->Load(); | 106 model->Load(); |
101 } | 107 } |
102 | 108 |
103 url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext()); | 109 url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext()); |
104 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 110 // Can be NULL during tests. |
| 111 if (web_contents) { |
| 112 url_fetcher_->SetContentURLRequestUserData( |
| 113 new content::ContentURLRequestUserData( |
| 114 web_contents->GetRenderProcessHost()->GetID(), |
| 115 web_contents->GetRenderViewHost()->routing_id())); |
| 116 } else { |
| 117 url_fetcher_->SetContentURLRequestUserData( |
| 118 new content::ContentURLRequestUserData()); |
| 119 } |
| 120 |
105 url_fetcher_->Start(); | 121 url_fetcher_->Start(); |
106 } | 122 } |
107 | 123 |
108 void TemplateURLFetcher::RequestDelegate::Observe( | 124 void TemplateURLFetcher::RequestDelegate::Observe( |
109 int type, | 125 int type, |
110 const content::NotificationSource& source, | 126 const content::NotificationSource& source, |
111 const content::NotificationDetails& details) { | 127 const content::NotificationDetails& details) { |
112 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | 128 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); |
113 | 129 |
114 if (!template_url_.get()) | 130 if (!template_url_.get()) |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 DCHECK(profile_); | 249 DCHECK(profile_); |
234 } | 250 } |
235 | 251 |
236 TemplateURLFetcher::~TemplateURLFetcher() { | 252 TemplateURLFetcher::~TemplateURLFetcher() { |
237 } | 253 } |
238 | 254 |
239 void TemplateURLFetcher::ScheduleDownload( | 255 void TemplateURLFetcher::ScheduleDownload( |
240 const string16& keyword, | 256 const string16& keyword, |
241 const GURL& osdd_url, | 257 const GURL& osdd_url, |
242 const GURL& favicon_url, | 258 const GURL& favicon_url, |
| 259 content::WebContents* web_contents, |
243 TemplateURLFetcherCallbacks* callbacks, | 260 TemplateURLFetcherCallbacks* callbacks, |
244 ProviderType provider_type) { | 261 ProviderType provider_type) { |
245 DCHECK(osdd_url.is_valid()); | 262 DCHECK(osdd_url.is_valid()); |
246 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); | 263 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); |
247 | 264 |
248 // For JS added OSDD empty keyword is OK because we will generate keyword | 265 // For JS added OSDD empty keyword is OK because we will generate keyword |
249 // later from OSDD content. | 266 // later from OSDD content. |
250 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER && | 267 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER && |
251 keyword.empty()) | 268 keyword.empty()) |
252 return; | 269 return; |
(...skipping 21 matching lines...) Expand all Loading... |
274 } | 291 } |
275 | 292 |
276 // Make sure we aren't already downloading this request. | 293 // Make sure we aren't already downloading this request. |
277 for (std::vector<RequestDelegate*>::iterator i = requests_->begin(); | 294 for (std::vector<RequestDelegate*>::iterator i = requests_->begin(); |
278 i != requests_->end(); ++i) { | 295 i != requests_->end(); ++i) { |
279 if ((*i)->url() == osdd_url || (*i)->keyword() == keyword) | 296 if ((*i)->url() == osdd_url || (*i)->keyword() == keyword) |
280 return; | 297 return; |
281 } | 298 } |
282 | 299 |
283 requests_->push_back( | 300 requests_->push_back( |
284 new RequestDelegate(this, keyword, osdd_url, favicon_url, | 301 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, |
285 owned_callbacks.release(), provider_type)); | 302 owned_callbacks.release(), provider_type)); |
286 } | 303 } |
287 | 304 |
288 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 305 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
289 DCHECK(std::find(requests_->begin(), requests_->end(), request) != | 306 DCHECK(std::find(requests_->begin(), requests_->end(), request) != |
290 requests_->end()); | 307 requests_->end()); |
291 requests_->erase(std::find(requests_->begin(), requests_->end(), request)); | 308 requests_->erase(std::find(requests_->begin(), requests_->end(), request)); |
292 delete request; | 309 delete request; |
293 } | 310 } |
OLD | NEW |