| 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/chrome_notification_types.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" | 13 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" |
| 15 #include "chrome/browser/search_engines/template_url_parser.h" | 14 #include "chrome/browser/search_engines/template_url_parser.h" |
| 16 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 18 #include "content/public/browser/notification_observer.h" | |
| 19 #include "content/public/browser/notification_registrar.h" | |
| 20 #include "content/public/browser/notification_source.h" | |
| 21 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/url_fetcher.h" | 20 #include "content/public/common/url_fetcher.h" |
| 25 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 26 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
| 27 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 28 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 29 | 25 |
| 30 // RequestDelegate ------------------------------------------------------------ | 26 // RequestDelegate ------------------------------------------------------------ |
| 31 class TemplateURLFetcher::RequestDelegate | 27 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { |
| 32 : public net::URLFetcherDelegate, | |
| 33 public content::NotificationObserver { | |
| 34 public: | 28 public: |
| 35 // Takes ownership of |callbacks|. | 29 // Takes ownership of |callbacks|. |
| 36 RequestDelegate(TemplateURLFetcher* fetcher, | 30 RequestDelegate(TemplateURLFetcher* fetcher, |
| 37 const string16& keyword, | 31 const string16& keyword, |
| 38 const GURL& osdd_url, | 32 const GURL& osdd_url, |
| 39 const GURL& favicon_url, | 33 const GURL& favicon_url, |
| 40 content::WebContents* web_contents, | 34 content::WebContents* web_contents, |
| 41 TemplateURLFetcherCallbacks* callbacks, | 35 TemplateURLFetcherCallbacks* callbacks, |
| 42 ProviderType provider_type); | 36 ProviderType provider_type); |
| 43 | 37 |
| 44 // content::NotificationObserver: | |
| 45 virtual void Observe(int type, | |
| 46 const content::NotificationSource& source, | |
| 47 const content::NotificationDetails& details) OVERRIDE; | |
| 48 | |
| 49 // net::URLFetcherDelegate: | 38 // net::URLFetcherDelegate: |
| 50 // If data contains a valid OSDD, a TemplateURL is created and added to | 39 // If data contains a valid OSDD, a TemplateURL is created and added to |
| 51 // the TemplateURLService. | 40 // the TemplateURLService. |
| 52 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 41 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 53 | 42 |
| 54 // URL of the OSDD. | 43 // URL of the OSDD. |
| 55 GURL url() const { return osdd_url_; } | 44 GURL url() const { return osdd_url_; } |
| 56 | 45 |
| 57 // Keyword to use. | 46 // Keyword to use. |
| 58 string16 keyword() const { return keyword_; } | 47 string16 keyword() const { return keyword_; } |
| 59 | 48 |
| 60 // The type of search provider being fetched. | 49 // The type of search provider being fetched. |
| 61 ProviderType provider_type() const { return provider_type_; } | 50 ProviderType provider_type() const { return provider_type_; } |
| 62 | 51 |
| 63 private: | 52 private: |
| 53 void OnLoaded(); |
| 64 void AddSearchProvider(); | 54 void AddSearchProvider(); |
| 65 | 55 |
| 66 scoped_ptr<net::URLFetcher> url_fetcher_; | 56 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 67 TemplateURLFetcher* fetcher_; | 57 TemplateURLFetcher* fetcher_; |
| 68 scoped_ptr<TemplateURL> template_url_; | 58 scoped_ptr<TemplateURL> template_url_; |
| 69 string16 keyword_; | 59 string16 keyword_; |
| 70 const GURL osdd_url_; | 60 const GURL osdd_url_; |
| 71 const GURL favicon_url_; | 61 const GURL favicon_url_; |
| 72 const ProviderType provider_type_; | 62 const ProviderType provider_type_; |
| 73 scoped_ptr<TemplateURLFetcherCallbacks> callbacks_; | 63 scoped_ptr<TemplateURLFetcherCallbacks> callbacks_; |
| 74 | 64 |
| 75 // Handles registering for our notifications. | 65 scoped_ptr<TemplateURLService::Subscription> template_url_subscription_; |
| 76 content::NotificationRegistrar registrar_; | |
| 77 | 66 |
| 78 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 67 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
| 79 }; | 68 }; |
| 80 | 69 |
| 81 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 70 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
| 82 TemplateURLFetcher* fetcher, | 71 TemplateURLFetcher* fetcher, |
| 83 const string16& keyword, | 72 const string16& keyword, |
| 84 const GURL& osdd_url, | 73 const GURL& osdd_url, |
| 85 const GURL& favicon_url, | 74 const GURL& favicon_url, |
| 86 content::WebContents* web_contents, | 75 content::WebContents* web_contents, |
| 87 TemplateURLFetcherCallbacks* callbacks, | 76 TemplateURLFetcherCallbacks* callbacks, |
| 88 ProviderType provider_type) | 77 ProviderType provider_type) |
| 89 : url_fetcher_(net::URLFetcher::Create( | 78 : url_fetcher_(net::URLFetcher::Create( |
| 90 osdd_url, net::URLFetcher::GET, this)), | 79 osdd_url, net::URLFetcher::GET, this)), |
| 91 fetcher_(fetcher), | 80 fetcher_(fetcher), |
| 92 keyword_(keyword), | 81 keyword_(keyword), |
| 93 osdd_url_(osdd_url), | 82 osdd_url_(osdd_url), |
| 94 favicon_url_(favicon_url), | 83 favicon_url_(favicon_url), |
| 95 provider_type_(provider_type), | 84 provider_type_(provider_type), |
| 96 callbacks_(callbacks) { | 85 callbacks_(callbacks) { |
| 97 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( | 86 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( |
| 98 fetcher_->profile()); | 87 fetcher_->profile()); |
| 99 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. | 88 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. |
| 100 | 89 |
| 101 if (!model->loaded()) { | 90 if (!model->loaded()) { |
| 102 // Start the model load and set-up waiting for it. | 91 // Start the model load and set-up waiting for it. |
| 103 registrar_.Add(this, | 92 template_url_subscription_ = model->RegisterOnLoadedCallback( |
| 104 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | 93 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, |
| 105 content::Source<TemplateURLService>(model)); | 94 base::Unretained(this))); |
| 106 model->Load(); | 95 model->Load(); |
| 107 } | 96 } |
| 108 | 97 |
| 109 url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext()); | 98 url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext()); |
| 110 // Can be NULL during tests. | 99 // Can be NULL during tests. |
| 111 if (web_contents) { | 100 if (web_contents) { |
| 112 content::AssociateURLFetcherWithRenderView( | 101 content::AssociateURLFetcherWithRenderView( |
| 113 url_fetcher_.get(), | 102 url_fetcher_.get(), |
| 114 web_contents->GetURL(), | 103 web_contents->GetURL(), |
| 115 web_contents->GetRenderProcessHost()->GetID(), | 104 web_contents->GetRenderProcessHost()->GetID(), |
| 116 web_contents->GetRenderViewHost()->GetRoutingID()); | 105 web_contents->GetRenderViewHost()->GetRoutingID()); |
| 117 } | 106 } |
| 118 | 107 |
| 119 url_fetcher_->Start(); | 108 url_fetcher_->Start(); |
| 120 } | 109 } |
| 121 | 110 |
| 122 void TemplateURLFetcher::RequestDelegate::Observe( | 111 void TemplateURLFetcher::RequestDelegate::OnLoaded() { |
| 123 int type, | 112 template_url_subscription_.reset(); |
| 124 const content::NotificationSource& source, | |
| 125 const content::NotificationDetails& details) { | |
| 126 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | |
| 127 | |
| 128 if (!template_url_.get()) | 113 if (!template_url_.get()) |
| 129 return; | 114 return; |
| 130 AddSearchProvider(); | 115 AddSearchProvider(); |
| 131 // WARNING: AddSearchProvider deletes us. | 116 // WARNING: AddSearchProvider deletes us. |
| 132 } | 117 } |
| 133 | 118 |
| 134 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( | 119 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( |
| 135 const net::URLFetcher* source) { | 120 const net::URLFetcher* source) { |
| 136 // Validation checks. | 121 // Validation checks. |
| 137 // Make sure we can still replace the keyword, i.e. the fetch was successful. | 122 // Make sure we can still replace the keyword, i.e. the fetch was successful. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 owned_callbacks.release(), provider_type)); | 273 owned_callbacks.release(), provider_type)); |
| 289 } | 274 } |
| 290 | 275 |
| 291 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 276 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
| 292 Requests::iterator i = | 277 Requests::iterator i = |
| 293 std::find(requests_.begin(), requests_.end(), request); | 278 std::find(requests_.begin(), requests_.end(), request); |
| 294 DCHECK(i != requests_.end()); | 279 DCHECK(i != requests_.end()); |
| 295 requests_.weak_erase(i); | 280 requests_.weak_erase(i); |
| 296 delete request; | 281 delete request; |
| 297 } | 282 } |
| OLD | NEW |