| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/net/url_fetcher.h" | 18 #include "content/common/net/url_fetcher.h" |
| 19 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/common/url_fetcher_delegate.h" |
| 22 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 23 | 24 |
| 24 // RequestDelegate ------------------------------------------------------------ | 25 // RequestDelegate ------------------------------------------------------------ |
| 25 class TemplateURLFetcher::RequestDelegate | 26 class TemplateURLFetcher::RequestDelegate |
| 26 : public URLFetcher::Delegate, | 27 : public content::URLFetcherDelegate, |
| 27 public content::NotificationObserver { | 28 public content::NotificationObserver { |
| 28 public: | 29 public: |
| 29 // Takes ownership of |callbacks|. | 30 // Takes ownership of |callbacks|. |
| 30 RequestDelegate(TemplateURLFetcher* fetcher, | 31 RequestDelegate(TemplateURLFetcher* fetcher, |
| 31 const string16& keyword, | 32 const string16& keyword, |
| 32 const GURL& osdd_url, | 33 const GURL& osdd_url, |
| 33 const GURL& favicon_url, | 34 const GURL& favicon_url, |
| 34 TemplateURLFetcherCallbacks* callbacks, | 35 TemplateURLFetcherCallbacks* callbacks, |
| 35 ProviderType provider_type); | 36 ProviderType provider_type); |
| 36 | 37 |
| 37 // content::NotificationObserver: | 38 // content::NotificationObserver: |
| 38 virtual void Observe(int type, | 39 virtual void Observe(int type, |
| 39 const content::NotificationSource& source, | 40 const content::NotificationSource& source, |
| 40 const content::NotificationDetails& details); | 41 const content::NotificationDetails& details); |
| 41 | 42 |
| 42 // URLFetcher::Delegate: | 43 // content::URLFetcherDelegate: |
| 43 // If data contains a valid OSDD, a TemplateURL is created and added to | 44 // If data contains a valid OSDD, a TemplateURL is created and added to |
| 44 // the TemplateURLService. | 45 // the TemplateURLService. |
| 45 virtual void OnURLFetchComplete(const URLFetcher* source, | 46 virtual void OnURLFetchComplete(const URLFetcher* source); |
| 46 const GURL& url, | |
| 47 const net::URLRequestStatus& status, | |
| 48 int response_code, | |
| 49 const net::ResponseCookies& cookies, | |
| 50 const std::string& data); | |
| 51 | 47 |
| 52 // URL of the OSDD. | 48 // URL of the OSDD. |
| 53 GURL url() const { return osdd_url_; } | 49 GURL url() const { return osdd_url_; } |
| 54 | 50 |
| 55 // Keyword to use. | 51 // Keyword to use. |
| 56 string16 keyword() const { return keyword_; } | 52 string16 keyword() const { return keyword_; } |
| 57 | 53 |
| 58 // The type of search provider being fetched. | 54 // The type of search provider being fetched. |
| 59 ProviderType provider_type() const { return provider_type_; } | 55 ProviderType provider_type() const { return provider_type_; } |
| 60 | 56 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 const content::NotificationDetails& details) { | 109 const content::NotificationDetails& details) { |
| 114 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | 110 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); |
| 115 | 111 |
| 116 if (!template_url_.get()) | 112 if (!template_url_.get()) |
| 117 return; | 113 return; |
| 118 AddSearchProvider(); | 114 AddSearchProvider(); |
| 119 // WARNING: AddSearchProvider deletes us. | 115 // WARNING: AddSearchProvider deletes us. |
| 120 } | 116 } |
| 121 | 117 |
| 122 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( | 118 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( |
| 123 const URLFetcher* source, | 119 const URLFetcher* source) { |
| 124 const GURL& url, | |
| 125 const net::URLRequestStatus& status, | |
| 126 int response_code, | |
| 127 const net::ResponseCookies& cookies, | |
| 128 const std::string& data) { | |
| 129 template_url_.reset(new TemplateURL()); | 120 template_url_.reset(new TemplateURL()); |
| 130 | 121 |
| 131 // Validation checks. | 122 // Validation checks. |
| 132 // 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. |
| 133 // If the OSDD file was loaded HTTP, we also have to check the response_code. | 124 // If the OSDD file was loaded HTTP, we also have to check the response_code. |
| 134 // For other schemes, e.g. when the OSDD file is bundled with an extension, | 125 // For other schemes, e.g. when the OSDD file is bundled with an extension, |
| 135 // the response_code is not applicable and should be -1. Also, ensure that | 126 // the response_code is not applicable and should be -1. Also, ensure that |
| 136 // the returned information results in a valid search URL. | 127 // the returned information results in a valid search URL. |
| 137 if (!status.is_success() || | 128 std::string data; |
| 138 ((response_code != -1) && (response_code != 200)) || | 129 if (!source->status().is_success() || |
| 130 ((source->response_code() != -1) && (source->response_code() != 200)) || |
| 131 !source->GetResponseAsString(&data) || |
| 139 !TemplateURLParser::Parse( | 132 !TemplateURLParser::Parse( |
| 140 reinterpret_cast<const unsigned char*>(data.c_str()), | 133 reinterpret_cast<const unsigned char*>(data.c_str()), |
| 141 data.length(), | 134 data.length(), |
| 142 NULL, | 135 NULL, |
| 143 template_url_.get()) || | 136 template_url_.get()) || |
| 144 !template_url_->url() || !template_url_->url()->SupportsReplacement()) { | 137 !template_url_->url() || !template_url_->url()->SupportsReplacement()) { |
| 145 fetcher_->RequestCompleted(this); | 138 fetcher_->RequestCompleted(this); |
| 146 // WARNING: RequestCompleted deletes us. | 139 // WARNING: RequestCompleted deletes us. |
| 147 return; | 140 return; |
| 148 } | 141 } |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 new RequestDelegate(this, keyword, osdd_url, favicon_url, | 318 new RequestDelegate(this, keyword, osdd_url, favicon_url, |
| 326 owned_callbacks.release(), provider_type)); | 319 owned_callbacks.release(), provider_type)); |
| 327 } | 320 } |
| 328 | 321 |
| 329 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 322 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
| 330 DCHECK(find(requests_->begin(), requests_->end(), request) != | 323 DCHECK(find(requests_->begin(), requests_->end(), request) != |
| 331 requests_->end()); | 324 requests_->end()); |
| 332 requests_->erase(find(requests_->begin(), requests_->end(), request)); | 325 requests_->erase(find(requests_->begin(), requests_->end(), request)); |
| 333 delete request; | 326 delete request; |
| 334 } | 327 } |
| OLD | NEW |