| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_fetcher.h" | 5 #include "components/search_engines/template_url_fetcher.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 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 "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "components/search_engines/template_url.h" | 12 #include "components/search_engines/template_url.h" |
| 13 #include "components/search_engines/template_url_parser.h" | 13 #include "components/search_engines/template_url_parser.h" |
| 14 #include "components/search_engines/template_url_service.h" | 14 #include "components/search_engines/template_url_service.h" |
| 15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 16 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 20 | 20 |
| 21 // RequestDelegate ------------------------------------------------------------ | 21 // RequestDelegate ------------------------------------------------------------ |
| 22 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { | 22 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { |
| 23 public: | 23 public: |
| 24 RequestDelegate( | 24 RequestDelegate( |
| 25 TemplateURLFetcher* fetcher, | 25 TemplateURLFetcher* fetcher, |
| 26 const base::string16& keyword, | 26 const base::string16& keyword, |
| 27 const GURL& osdd_url, | 27 const GURL& osdd_url, |
| 28 const GURL& favicon_url, | 28 const GURL& favicon_url, |
| 29 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, | 29 const URLFetcherCustomizeCallback& url_fetcher_customize_callback); |
| 30 const ConfirmAddSearchProviderCallback& confirm_add_callback, | |
| 31 ProviderType provider_type); | |
| 32 | 30 |
| 33 // net::URLFetcherDelegate: | 31 // net::URLFetcherDelegate: |
| 34 // If data contains a valid OSDD, a TemplateURL is created and added to | 32 // If data contains a valid OSDD, a TemplateURL is created and added to |
| 35 // the TemplateURLService. | 33 // the TemplateURLService. |
| 36 void OnURLFetchComplete(const net::URLFetcher* source) override; | 34 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 37 | 35 |
| 38 // URL of the OSDD. | 36 // URL of the OSDD. |
| 39 GURL url() const { return osdd_url_; } | 37 GURL url() const { return osdd_url_; } |
| 40 | 38 |
| 41 // Keyword to use. | 39 // Keyword to use. |
| 42 base::string16 keyword() const { return keyword_; } | 40 base::string16 keyword() const { return keyword_; } |
| 43 | 41 |
| 44 // The type of search provider being fetched. | |
| 45 ProviderType provider_type() const { return provider_type_; } | |
| 46 | |
| 47 private: | 42 private: |
| 48 void OnLoaded(); | 43 void OnLoaded(); |
| 49 void AddSearchProvider(); | 44 void AddSearchProvider(); |
| 50 | 45 |
| 51 std::unique_ptr<net::URLFetcher> url_fetcher_; | 46 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 52 TemplateURLFetcher* fetcher_; | 47 TemplateURLFetcher* fetcher_; |
| 53 std::unique_ptr<TemplateURL> template_url_; | 48 std::unique_ptr<TemplateURL> template_url_; |
| 54 base::string16 keyword_; | 49 base::string16 keyword_; |
| 55 const GURL osdd_url_; | 50 const GURL osdd_url_; |
| 56 const GURL favicon_url_; | 51 const GURL favicon_url_; |
| 57 const ProviderType provider_type_; | |
| 58 ConfirmAddSearchProviderCallback confirm_add_callback_; | |
| 59 | 52 |
| 60 std::unique_ptr<TemplateURLService::Subscription> template_url_subscription_; | 53 std::unique_ptr<TemplateURLService::Subscription> template_url_subscription_; |
| 61 | 54 |
| 62 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 55 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
| 63 }; | 56 }; |
| 64 | 57 |
| 65 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 58 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
| 66 TemplateURLFetcher* fetcher, | 59 TemplateURLFetcher* fetcher, |
| 67 const base::string16& keyword, | 60 const base::string16& keyword, |
| 68 const GURL& osdd_url, | 61 const GURL& osdd_url, |
| 69 const GURL& favicon_url, | 62 const GURL& favicon_url, |
| 70 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, | 63 const URLFetcherCustomizeCallback& url_fetcher_customize_callback) |
| 71 const ConfirmAddSearchProviderCallback& confirm_add_callback, | |
| 72 ProviderType provider_type) | |
| 73 : url_fetcher_( | 64 : url_fetcher_( |
| 74 net::URLFetcher::Create(osdd_url, net::URLFetcher::GET, this)), | 65 net::URLFetcher::Create(osdd_url, net::URLFetcher::GET, this)), |
| 75 fetcher_(fetcher), | 66 fetcher_(fetcher), |
| 76 keyword_(keyword), | 67 keyword_(keyword), |
| 77 osdd_url_(osdd_url), | 68 osdd_url_(osdd_url), |
| 78 favicon_url_(favicon_url), | 69 favicon_url_(favicon_url) { |
| 79 provider_type_(provider_type), | |
| 80 confirm_add_callback_(confirm_add_callback) { | |
| 81 TemplateURLService* model = fetcher_->template_url_service_; | 70 TemplateURLService* model = fetcher_->template_url_service_; |
| 82 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. | 71 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. |
| 83 | 72 |
| 84 if (!model->loaded()) { | 73 if (!model->loaded()) { |
| 85 // Start the model load and set-up waiting for it. | 74 // Start the model load and set-up waiting for it. |
| 86 template_url_subscription_ = model->RegisterOnLoadedCallback( | 75 template_url_subscription_ = model->RegisterOnLoadedCallback( |
| 87 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, | 76 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, |
| 88 base::Unretained(this))); | 77 base::Unretained(this))); |
| 89 model->Load(); | 78 model->Load(); |
| 90 } | 79 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 fetcher_->template_url_service_->search_terms_data(), false, | 115 fetcher_->template_url_service_->search_terms_data(), false, |
| 127 data.data(), data.length(), NULL)); | 116 data.data(), data.length(), NULL)); |
| 128 if (!template_url_.get() || | 117 if (!template_url_.get() || |
| 129 !template_url_->url_ref().SupportsReplacement( | 118 !template_url_->url_ref().SupportsReplacement( |
| 130 fetcher_->template_url_service_->search_terms_data())) { | 119 fetcher_->template_url_service_->search_terms_data())) { |
| 131 fetcher_->RequestCompleted(this); | 120 fetcher_->RequestCompleted(this); |
| 132 // WARNING: RequestCompleted deletes us. | 121 // WARNING: RequestCompleted deletes us. |
| 133 return; | 122 return; |
| 134 } | 123 } |
| 135 | 124 |
| 136 if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) { | 125 if (keyword_.empty()) { |
| 137 // Use the parser-generated new keyword from the URL in the OSDD for the | 126 // Use the parser-generated new keyword from the URL in the OSDD for the |
| 138 // non-autodetected case. The existing |keyword_| was generated from the | 127 // non-autodetected case. The existing |keyword_| was generated from the |
| 139 // URL that hosted the OSDD, which results in the wrong keyword when the | 128 // URL that hosted the OSDD, which results in the wrong keyword when the |
| 140 // OSDD was located on a third-party site that has nothing in common with | 129 // OSDD was located on a third-party site that has nothing in common with |
| 141 // search engine described by OSDD. | 130 // search engine described by OSDD. |
| 142 keyword_ = template_url_->keyword(); | 131 keyword_ = template_url_->keyword(); |
| 143 DCHECK(!keyword_.empty()); | 132 DCHECK(!keyword_.empty()); |
| 144 } | 133 } |
| 145 | 134 |
| 146 // Wait for the model to be loaded before adding the provider. | 135 // Wait for the model to be loaded before adding the provider. |
| 147 if (!fetcher_->template_url_service_->loaded()) | 136 if (!fetcher_->template_url_service_->loaded()) |
| 148 return; | 137 return; |
| 149 AddSearchProvider(); | 138 AddSearchProvider(); |
| 150 // WARNING: AddSearchProvider deletes us. | 139 // WARNING: AddSearchProvider deletes us. |
| 151 } | 140 } |
| 152 | 141 |
| 153 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { | 142 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { |
| 154 DCHECK(template_url_.get()); | 143 DCHECK(template_url_.get()); |
| 155 DCHECK(!keyword_.empty()); | 144 DCHECK(!keyword_.empty()); |
| 156 TemplateURLService* model = fetcher_->template_url_service_; | 145 TemplateURLService* model = fetcher_->template_url_service_; |
| 157 DCHECK(model); | 146 DCHECK(model); |
| 158 DCHECK(model->loaded()); | 147 DCHECK(model->loaded()); |
| 159 | 148 |
| 160 TemplateURL* existing_url = NULL; | 149 TemplateURL* existing_url = NULL; |
| 161 if (model->CanAddAutogeneratedKeyword(keyword_, GURL(template_url_->url()), | 150 if (!model->CanAddAutogeneratedKeyword(keyword_, GURL(template_url_->url()), |
| 162 &existing_url)) { | 151 &existing_url)) { |
| 163 if (existing_url) | |
| 164 model->Remove(existing_url); | |
| 165 } else if (provider_type_ == AUTODETECTED_PROVIDER) { | |
| 166 fetcher_->RequestCompleted(this); // WARNING: Deletes us! | 152 fetcher_->RequestCompleted(this); // WARNING: Deletes us! |
| 167 return; | 153 return; |
| 168 } | 154 } |
| 169 | 155 |
| 156 if (existing_url) |
| 157 model->Remove(existing_url); |
| 158 |
| 170 // The short name is what is shown to the user. We preserve original names | 159 // The short name is what is shown to the user. We preserve original names |
| 171 // since it is better when generated keyword in many cases. | 160 // since it is better when generated keyword in many cases. |
| 172 TemplateURLData data(template_url_->data()); | 161 TemplateURLData data(template_url_->data()); |
| 173 data.SetKeyword(keyword_); | 162 data.SetKeyword(keyword_); |
| 174 data.originating_url = osdd_url_; | 163 data.originating_url = osdd_url_; |
| 175 | 164 |
| 176 // The page may have specified a URL to use for favicons, if not, set it. | 165 // The page may have specified a URL to use for favicons, if not, set it. |
| 177 if (!data.favicon_url.is_valid()) | 166 if (!data.favicon_url.is_valid()) |
| 178 data.favicon_url = favicon_url_; | 167 data.favicon_url = favicon_url_; |
| 179 | 168 |
| 180 switch (provider_type_) { | 169 // Mark the keyword as replaceable so it can be removed if necessary. |
| 181 case AUTODETECTED_PROVIDER: | 170 data.safe_for_autoreplace = true; |
| 182 // Mark the keyword as replaceable so it can be removed if necessary. | 171 model->Add(new TemplateURL(data)); |
| 183 data.safe_for_autoreplace = true; | |
| 184 model->Add(new TemplateURL(data)); | |
| 185 break; | |
| 186 | |
| 187 case EXPLICIT_PROVIDER: | |
| 188 // Confirm addition and allow user to edit default choices. It's ironic | |
| 189 // that only *non*-autodetected additions get confirmed, but the user | |
| 190 // expects feedback that his action did something. | |
| 191 // The source WebContents' delegate takes care of adding the URL to the | |
| 192 // model, which takes ownership, or of deleting it if the add is | |
| 193 // cancelled. | |
| 194 confirm_add_callback_.Run(base::WrapUnique(new TemplateURL(data))); | |
| 195 break; | |
| 196 | |
| 197 default: | |
| 198 NOTREACHED(); | |
| 199 break; | |
| 200 } | |
| 201 | 172 |
| 202 fetcher_->RequestCompleted(this); | 173 fetcher_->RequestCompleted(this); |
| 203 // WARNING: RequestCompleted deletes us. | 174 // WARNING: RequestCompleted deletes us. |
| 204 } | 175 } |
| 205 | 176 |
| 206 // TemplateURLFetcher --------------------------------------------------------- | 177 // TemplateURLFetcher --------------------------------------------------------- |
| 207 | 178 |
| 208 TemplateURLFetcher::TemplateURLFetcher( | 179 TemplateURLFetcher::TemplateURLFetcher( |
| 209 TemplateURLService* template_url_service, | 180 TemplateURLService* template_url_service, |
| 210 net::URLRequestContextGetter* request_context) | 181 net::URLRequestContextGetter* request_context) |
| 211 : template_url_service_(template_url_service), | 182 : template_url_service_(template_url_service), |
| 212 request_context_(request_context) { | 183 request_context_(request_context) { |
| 213 } | 184 } |
| 214 | 185 |
| 215 TemplateURLFetcher::~TemplateURLFetcher() { | 186 TemplateURLFetcher::~TemplateURLFetcher() { |
| 216 } | 187 } |
| 217 | 188 |
| 218 void TemplateURLFetcher::ScheduleDownload( | 189 void TemplateURLFetcher::ScheduleDownload( |
| 219 const base::string16& keyword, | 190 const base::string16& keyword, |
| 220 const GURL& osdd_url, | 191 const GURL& osdd_url, |
| 221 const GURL& favicon_url, | 192 const GURL& favicon_url, |
| 222 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, | 193 const URLFetcherCustomizeCallback& url_fetcher_customize_callback) { |
| 223 const ConfirmAddSearchProviderCallback& confirm_add_callback, | |
| 224 ProviderType provider_type) { | |
| 225 DCHECK(osdd_url.is_valid()); | 194 DCHECK(osdd_url.is_valid()); |
| 195 DCHECK(!keyword.empty()); |
| 226 | 196 |
| 227 // For a JS-added OSDD, the provided keyword is irrelevant because we will | 197 if (!template_url_service_->loaded()) { |
| 228 // generate a keyword later from the OSDD content. For the autodetected case, | 198 // We could try to set up a callback to this function again once the model |
| 229 // we need a valid keyword up front. | 199 // is loaded but meh. |
| 230 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 200 template_url_service_->Load(); |
| 231 DCHECK(!keyword.empty()); | 201 return; |
| 202 } |
| 232 | 203 |
| 233 if (!template_url_service_->loaded()) { | 204 const TemplateURL* template_url = |
| 234 // We could try to set up a callback to this function again once the model | 205 template_url_service_->GetTemplateURLForKeyword(keyword); |
| 235 // is loaded but since this is an auto-add case anyway, meh. | 206 if (template_url && (!template_url->safe_for_autoreplace() || |
| 236 template_url_service_->Load(); | 207 template_url->originating_url() == osdd_url)) |
| 237 return; | 208 return; |
| 238 } | |
| 239 | |
| 240 const TemplateURL* template_url = | |
| 241 template_url_service_->GetTemplateURLForKeyword(keyword); | |
| 242 if (template_url && (!template_url->safe_for_autoreplace() || | |
| 243 template_url->originating_url() == osdd_url)) | |
| 244 return; | |
| 245 } | |
| 246 | 209 |
| 247 // Make sure we aren't already downloading this request. | 210 // Make sure we aren't already downloading this request. |
| 248 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 211 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
| 249 if (((*i)->url() == osdd_url) || | 212 if (((*i)->url() == osdd_url) || ((*i)->keyword() == keyword)) |
| 250 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && | |
| 251 ((*i)->keyword() == keyword))) | |
| 252 return; | 213 return; |
| 253 } | 214 } |
| 254 | 215 |
| 255 requests_.push_back(new RequestDelegate( | 216 requests_.push_back(new RequestDelegate( |
| 256 this, keyword, osdd_url, favicon_url, url_fetcher_customize_callback, | 217 this, keyword, osdd_url, favicon_url, url_fetcher_customize_callback)); |
| 257 confirm_add_callback, provider_type)); | |
| 258 } | 218 } |
| 259 | 219 |
| 260 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 220 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
| 261 Requests::iterator i = | 221 Requests::iterator i = |
| 262 std::find(requests_.begin(), requests_.end(), request); | 222 std::find(requests_.begin(), requests_.end(), request); |
| 263 DCHECK(i != requests_.end()); | 223 DCHECK(i != requests_.end()); |
| 264 requests_.weak_erase(i); | 224 requests_.weak_erase(i); |
| 265 delete request; | 225 delete request; |
| 266 } | 226 } |
| OLD | NEW |