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