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