| 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_model.h" | 14 #include "chrome/browser/search_engines/template_url_model.h" |
| 15 #include "chrome/browser/search_engines/template_url_parser.h" | 15 #include "chrome/browser/search_engines/template_url_parser.h" |
| 16 #include "chrome/common/net/url_fetcher.h" | 16 #include "chrome/common/net/url_fetcher.h" |
| 17 #include "chrome/common/notification_observer.h" | 17 #include "chrome/common/notification_observer.h" |
| 18 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
| 19 #include "chrome/common/notification_source.h" | 19 #include "chrome/common/notification_source.h" |
| 20 #include "chrome/common/notification_type.h" | 20 #include "chrome/common/notification_type.h" |
| 21 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 22 | 22 |
| 23 // RequestDelegate ------------------------------------------------------------ | 23 // RequestDelegate ------------------------------------------------------------ |
| 24 class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate, | 24 class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate, |
| 25 public NotificationObserver { | 25 public NotificationObserver { |
| 26 public: | 26 public: |
| 27 // Takes ownership of |callbacks|. | 27 // Takes ownership of |callbacks|. |
| 28 RequestDelegate(TemplateURLFetcher* fetcher, | 28 RequestDelegate(TemplateURLFetcher* fetcher, |
| 29 const string16& keyword, | 29 const std::wstring& keyword, |
| 30 const GURL& osdd_url, | 30 const GURL& osdd_url, |
| 31 const GURL& favicon_url, | 31 const GURL& favicon_url, |
| 32 TemplateURLFetcherCallbacks* callbacks, | 32 TemplateURLFetcherCallbacks* callbacks, |
| 33 ProviderType provider_type); | 33 ProviderType provider_type); |
| 34 | 34 |
| 35 // NotificationObserver: | 35 // NotificationObserver: |
| 36 virtual void Observe(NotificationType type, | 36 virtual void Observe(NotificationType type, |
| 37 const NotificationSource& source, | 37 const NotificationSource& source, |
| 38 const NotificationDetails& details); | 38 const NotificationDetails& details); |
| 39 | 39 |
| 40 // URLFetcher::Delegate: | 40 // URLFetcher::Delegate: |
| 41 // If data contains a valid OSDD, a TemplateURL is created and added to | 41 // If data contains a valid OSDD, a TemplateURL is created and added to |
| 42 // the TemplateURLModel. | 42 // the TemplateURLModel. |
| 43 virtual void OnURLFetchComplete(const URLFetcher* source, | 43 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 44 const GURL& url, | 44 const GURL& url, |
| 45 const net::URLRequestStatus& status, | 45 const net::URLRequestStatus& status, |
| 46 int response_code, | 46 int response_code, |
| 47 const ResponseCookies& cookies, | 47 const ResponseCookies& cookies, |
| 48 const std::string& data); | 48 const std::string& data); |
| 49 | 49 |
| 50 // URL of the OSDD. | 50 // URL of the OSDD. |
| 51 GURL url() const { return osdd_url_; } | 51 const GURL& url() const { return osdd_url_; } |
| 52 | 52 |
| 53 // Keyword to use. | 53 // Keyword to use. |
| 54 string16 keyword() const { return keyword_; } | 54 const std::wstring keyword() const { return keyword_; } |
| 55 | 55 |
| 56 // The type of search provider being fetched. | 56 // The type of search provider being fetched. |
| 57 ProviderType provider_type() const { return provider_type_; } | 57 ProviderType provider_type() const { return provider_type_; } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 void AddSearchProvider(); | 60 void AddSearchProvider(); |
| 61 | 61 |
| 62 URLFetcher url_fetcher_; | 62 URLFetcher url_fetcher_; |
| 63 TemplateURLFetcher* fetcher_; | 63 TemplateURLFetcher* fetcher_; |
| 64 scoped_ptr<TemplateURL> template_url_; | 64 scoped_ptr<TemplateURL> template_url_; |
| 65 string16 keyword_; | 65 std::wstring keyword_; |
| 66 const GURL osdd_url_; | 66 const GURL osdd_url_; |
| 67 const GURL favicon_url_; | 67 const GURL favicon_url_; |
| 68 const ProviderType provider_type_; | 68 const ProviderType provider_type_; |
| 69 scoped_ptr<TemplateURLFetcherCallbacks> callbacks_; | 69 scoped_ptr<TemplateURLFetcherCallbacks> callbacks_; |
| 70 | 70 |
| 71 // Handles registering for our notifications. | 71 // Handles registering for our notifications. |
| 72 NotificationRegistrar registrar_; | 72 NotificationRegistrar registrar_; |
| 73 | 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 74 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 77 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
| 78 TemplateURLFetcher* fetcher, | 78 TemplateURLFetcher* fetcher, |
| 79 const string16& keyword, | 79 const std::wstring& keyword, |
| 80 const GURL& osdd_url, | 80 const GURL& osdd_url, |
| 81 const GURL& favicon_url, | 81 const GURL& favicon_url, |
| 82 TemplateURLFetcherCallbacks* callbacks, | 82 TemplateURLFetcherCallbacks* callbacks, |
| 83 ProviderType provider_type) | 83 ProviderType provider_type) |
| 84 : ALLOW_THIS_IN_INITIALIZER_LIST(url_fetcher_(osdd_url, | 84 : ALLOW_THIS_IN_INITIALIZER_LIST(url_fetcher_(osdd_url, |
| 85 URLFetcher::GET, this)), | 85 URLFetcher::GET, this)), |
| 86 fetcher_(fetcher), | 86 fetcher_(fetcher), |
| 87 keyword_(keyword), | 87 keyword_(keyword), |
| 88 osdd_url_(osdd_url), | 88 osdd_url_(osdd_url), |
| 89 favicon_url_(favicon_url), | 89 favicon_url_(favicon_url), |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 153 } |
| 154 | 154 |
| 155 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { | 155 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { |
| 156 DCHECK(template_url_.get()); | 156 DCHECK(template_url_.get()); |
| 157 if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) { | 157 if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) { |
| 158 // Generate new keyword from URL in OSDD for none autodetected case. | 158 // Generate new keyword from URL in OSDD for none autodetected case. |
| 159 // Previous keyword was generated from URL where OSDD was placed and | 159 // Previous keyword was generated from URL where OSDD was placed and |
| 160 // it gives wrong result when OSDD is located on third party site that | 160 // it gives wrong result when OSDD is located on third party site that |
| 161 // has nothing in common with search engine in OSDD. | 161 // has nothing in common with search engine in OSDD. |
| 162 GURL keyword_url(template_url_->url()->url()); | 162 GURL keyword_url(template_url_->url()->url()); |
| 163 string16 new_keyword = TemplateURLModel::GenerateKeyword( | 163 std::wstring new_keyword = TemplateURLModel::GenerateKeyword( |
| 164 keyword_url, false); | 164 keyword_url, false); |
| 165 if (!new_keyword.empty()) | 165 if (!new_keyword.empty()) |
| 166 keyword_ = new_keyword; | 166 keyword_ = new_keyword; |
| 167 } | 167 } |
| 168 TemplateURLModel* model = fetcher_->profile()->GetTemplateURLModel(); | 168 TemplateURLModel* model = fetcher_->profile()->GetTemplateURLModel(); |
| 169 const TemplateURL* existing_url; | 169 const TemplateURL* existing_url; |
| 170 if (keyword_.empty() || | 170 if (keyword_.empty() || |
| 171 !model || !model->loaded() || | 171 !model || !model->loaded() || |
| 172 !model->CanReplaceKeyword(keyword_, GURL(template_url_->url()->url()), | 172 !model->CanReplaceKeyword(keyword_, GURL(template_url_->url()->url()), |
| 173 &existing_url)) { | 173 &existing_url)) { |
| 174 if (provider_type_ == AUTODETECTED_PROVIDER || !model || !model->loaded()) { | 174 if (provider_type_ == AUTODETECTED_PROVIDER || !model || !model->loaded()) { |
| 175 fetcher_->RequestCompleted(this); | 175 fetcher_->RequestCompleted(this); |
| 176 // WARNING: RequestCompleted deletes us. | 176 // WARNING: RequestCompleted deletes us. |
| 177 return; | 177 return; |
| 178 } | 178 } |
| 179 | 179 |
| 180 existing_url = NULL; | 180 existing_url = NULL; |
| 181 | 181 |
| 182 // Try to generate a keyword automatically when we are setting the default | 182 // Try to generate a keyword automatically when we are setting the default |
| 183 // provider. The keyword isn't as important in this case. | 183 // provider. The keyword isn't as important in this case. |
| 184 if (provider_type_ == EXPLICIT_DEFAULT_PROVIDER) { | 184 if (provider_type_ == EXPLICIT_DEFAULT_PROVIDER) { |
| 185 // The loop numbers are arbitrary and are simply a strong effort. | 185 // The loop numbers are arbitrary and are simply a strong effort. |
| 186 string16 new_keyword; | 186 std::wstring new_keyword; |
| 187 for (int i = 0; i < 100; ++i) { | 187 for (int i = 0; i < 100; ++i) { |
| 188 // Concatenate a number at end of the keyword and try that. | 188 // Concatenate a number at end of the keyword and try that. |
| 189 new_keyword = keyword_; | 189 new_keyword = keyword_; |
| 190 // Try the keyword alone the first time | 190 // Try the keyword alone the first time |
| 191 if (i > 0) | 191 if (i > 0) |
| 192 new_keyword.append(base::IntToString16(i)); | 192 new_keyword.append(UTF16ToWide(base::IntToString16(i))); |
| 193 if (!model->GetTemplateURLForKeyword(new_keyword) || | 193 if (!model->GetTemplateURLForKeyword(new_keyword) || |
| 194 model->CanReplaceKeyword(new_keyword, | 194 model->CanReplaceKeyword(new_keyword, |
| 195 GURL(template_url_->url()->url()), | 195 GURL(template_url_->url()->url()), |
| 196 &existing_url)) { | 196 &existing_url)) { |
| 197 break; | 197 break; |
| 198 } | 198 } |
| 199 new_keyword.clear(); | 199 new_keyword.clear(); |
| 200 existing_url = NULL; | 200 existing_url = NULL; |
| 201 } | 201 } |
| 202 | 202 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // TemplateURLFetcher --------------------------------------------------------- | 261 // TemplateURLFetcher --------------------------------------------------------- |
| 262 | 262 |
| 263 TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) { | 263 TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) { |
| 264 DCHECK(profile_); | 264 DCHECK(profile_); |
| 265 } | 265 } |
| 266 | 266 |
| 267 TemplateURLFetcher::~TemplateURLFetcher() { | 267 TemplateURLFetcher::~TemplateURLFetcher() { |
| 268 } | 268 } |
| 269 | 269 |
| 270 void TemplateURLFetcher::ScheduleDownload( | 270 void TemplateURLFetcher::ScheduleDownload( |
| 271 const string16& keyword, | 271 const std::wstring& keyword, |
| 272 const GURL& osdd_url, | 272 const GURL& osdd_url, |
| 273 const GURL& favicon_url, | 273 const GURL& favicon_url, |
| 274 TemplateURLFetcherCallbacks* callbacks, | 274 TemplateURLFetcherCallbacks* callbacks, |
| 275 ProviderType provider_type) { | 275 ProviderType provider_type) { |
| 276 DCHECK(osdd_url.is_valid()); | 276 DCHECK(osdd_url.is_valid()); |
| 277 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); | 277 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); |
| 278 | 278 |
| 279 // For JS added OSDD empty keyword is OK because we will generate keyword | 279 // For JS added OSDD empty keyword is OK because we will generate keyword |
| 280 // later from OSDD content. | 280 // later from OSDD content. |
| 281 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER && | 281 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER && |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 new RequestDelegate(this, keyword, osdd_url, favicon_url, | 319 new RequestDelegate(this, keyword, osdd_url, favicon_url, |
| 320 owned_callbacks.release(), provider_type)); | 320 owned_callbacks.release(), provider_type)); |
| 321 } | 321 } |
| 322 | 322 |
| 323 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 323 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
| 324 DCHECK(find(requests_->begin(), requests_->end(), request) != | 324 DCHECK(find(requests_->begin(), requests_->end(), request) != |
| 325 requests_->end()); | 325 requests_->end()); |
| 326 requests_->erase(find(requests_->begin(), requests_->end(), request)); | 326 requests_->erase(find(requests_->begin(), requests_->end(), request)); |
| 327 delete request; | 327 delete request; |
| 328 } | 328 } |
| OLD | NEW |