| 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/search_engines/template_url_parser.h" | 11 #include "chrome/browser/search_engines/template_url_parser.h" |
| 12 #include "components/search_engines/template_url.h" | 12 #include "components/search_engines/template_url.h" |
| 13 #include "components/search_engines/template_url_service.h" | 13 #include "components/search_engines/template_url_service.h" |
| 14 #include "content/public/browser/render_frame_host.h" | |
| 15 #include "content/public/browser/render_process_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/common/url_fetcher.h" | |
| 18 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
| 19 #include "net/url_request/url_fetcher.h" | 15 #include "net/url_request/url_fetcher.h" |
| 20 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 23 | 19 |
| 24 // RequestDelegate ------------------------------------------------------------ | 20 // RequestDelegate ------------------------------------------------------------ |
| 25 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { | 21 class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate { |
| 26 public: | 22 public: |
| 27 // Takes ownership of |callbacks|. | 23 RequestDelegate( |
| 28 RequestDelegate(TemplateURLFetcher* fetcher, | 24 TemplateURLFetcher* fetcher, |
| 29 const base::string16& keyword, | 25 const base::string16& keyword, |
| 30 const GURL& osdd_url, | 26 const GURL& osdd_url, |
| 31 const GURL& favicon_url, | 27 const GURL& favicon_url, |
| 32 content::WebContents* web_contents, | 28 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, |
| 33 const ConfirmAddSearchProviderCallback& callback, | 29 const ConfirmAddSearchProviderCallback& confirm_add_callback, |
| 34 ProviderType provider_type); | 30 ProviderType provider_type); |
| 35 | 31 |
| 36 // net::URLFetcherDelegate: | 32 // net::URLFetcherDelegate: |
| 37 // If data contains a valid OSDD, a TemplateURL is created and added to | 33 // If data contains a valid OSDD, a TemplateURL is created and added to |
| 38 // the TemplateURLService. | 34 // the TemplateURLService. |
| 39 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 35 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 40 | 36 |
| 41 // URL of the OSDD. | 37 // URL of the OSDD. |
| 42 GURL url() const { return osdd_url_; } | 38 GURL url() const { return osdd_url_; } |
| 43 | 39 |
| 44 // Keyword to use. | 40 // Keyword to use. |
| 45 base::string16 keyword() const { return keyword_; } | 41 base::string16 keyword() const { return keyword_; } |
| 46 | 42 |
| 47 // The type of search provider being fetched. | 43 // The type of search provider being fetched. |
| 48 ProviderType provider_type() const { return provider_type_; } | 44 ProviderType provider_type() const { return provider_type_; } |
| 49 | 45 |
| 50 private: | 46 private: |
| 51 void OnLoaded(); | 47 void OnLoaded(); |
| 52 void AddSearchProvider(); | 48 void AddSearchProvider(); |
| 53 | 49 |
| 54 scoped_ptr<net::URLFetcher> url_fetcher_; | 50 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 55 TemplateURLFetcher* fetcher_; | 51 TemplateURLFetcher* fetcher_; |
| 56 scoped_ptr<TemplateURL> template_url_; | 52 scoped_ptr<TemplateURL> template_url_; |
| 57 base::string16 keyword_; | 53 base::string16 keyword_; |
| 58 const GURL osdd_url_; | 54 const GURL osdd_url_; |
| 59 const GURL favicon_url_; | 55 const GURL favicon_url_; |
| 60 const ProviderType provider_type_; | 56 const ProviderType provider_type_; |
| 61 ConfirmAddSearchProviderCallback callback_; | 57 ConfirmAddSearchProviderCallback confirm_add_callback_; |
| 62 | 58 |
| 63 scoped_ptr<TemplateURLService::Subscription> template_url_subscription_; | 59 scoped_ptr<TemplateURLService::Subscription> template_url_subscription_; |
| 64 | 60 |
| 65 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | 61 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
| 66 }; | 62 }; |
| 67 | 63 |
| 68 TemplateURLFetcher::RequestDelegate::RequestDelegate( | 64 TemplateURLFetcher::RequestDelegate::RequestDelegate( |
| 69 TemplateURLFetcher* fetcher, | 65 TemplateURLFetcher* fetcher, |
| 70 const base::string16& keyword, | 66 const base::string16& keyword, |
| 71 const GURL& osdd_url, | 67 const GURL& osdd_url, |
| 72 const GURL& favicon_url, | 68 const GURL& favicon_url, |
| 73 content::WebContents* web_contents, | 69 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, |
| 74 const ConfirmAddSearchProviderCallback& callback, | 70 const ConfirmAddSearchProviderCallback& confirm_add_callback, |
| 75 ProviderType provider_type) | 71 ProviderType provider_type) |
| 76 : url_fetcher_(net::URLFetcher::Create( | 72 : url_fetcher_(net::URLFetcher::Create( |
| 77 osdd_url, net::URLFetcher::GET, this)), | 73 osdd_url, net::URLFetcher::GET, this)), |
| 78 fetcher_(fetcher), | 74 fetcher_(fetcher), |
| 79 keyword_(keyword), | 75 keyword_(keyword), |
| 80 osdd_url_(osdd_url), | 76 osdd_url_(osdd_url), |
| 81 favicon_url_(favicon_url), | 77 favicon_url_(favicon_url), |
| 82 provider_type_(provider_type), | 78 provider_type_(provider_type), |
| 83 callback_(callback) { | 79 confirm_add_callback_(confirm_add_callback) { |
| 84 TemplateURLService* model = fetcher_->template_url_service_; | 80 TemplateURLService* model = fetcher_->template_url_service_; |
| 85 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. | 81 DCHECK(model); // TemplateURLFetcher::ScheduleDownload verifies this. |
| 86 | 82 |
| 87 if (!model->loaded()) { | 83 if (!model->loaded()) { |
| 88 // Start the model load and set-up waiting for it. | 84 // Start the model load and set-up waiting for it. |
| 89 template_url_subscription_ = model->RegisterOnLoadedCallback( | 85 template_url_subscription_ = model->RegisterOnLoadedCallback( |
| 90 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, | 86 base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded, |
| 91 base::Unretained(this))); | 87 base::Unretained(this))); |
| 92 model->Load(); | 88 model->Load(); |
| 93 } | 89 } |
| 94 | 90 |
| 91 if (!url_fetcher_customize_callback.is_null()) |
| 92 url_fetcher_customize_callback.Run(url_fetcher_.get()); |
| 93 |
| 95 url_fetcher_->SetRequestContext(fetcher->request_context_.get()); | 94 url_fetcher_->SetRequestContext(fetcher->request_context_.get()); |
| 96 // Can be NULL during tests. | |
| 97 if (web_contents) { | |
| 98 content::AssociateURLFetcherWithRenderFrame( | |
| 99 url_fetcher_.get(), | |
| 100 web_contents->GetURL(), | |
| 101 web_contents->GetRenderProcessHost()->GetID(), | |
| 102 web_contents->GetMainFrame()->GetRoutingID()); | |
| 103 } | |
| 104 | |
| 105 url_fetcher_->Start(); | 95 url_fetcher_->Start(); |
| 106 } | 96 } |
| 107 | 97 |
| 108 void TemplateURLFetcher::RequestDelegate::OnLoaded() { | 98 void TemplateURLFetcher::RequestDelegate::OnLoaded() { |
| 109 template_url_subscription_.reset(); | 99 template_url_subscription_.reset(); |
| 110 if (!template_url_.get()) | 100 if (!template_url_.get()) |
| 111 return; | 101 return; |
| 112 AddSearchProvider(); | 102 AddSearchProvider(); |
| 113 // WARNING: AddSearchProvider deletes us. | 103 // WARNING: AddSearchProvider deletes us. |
| 114 } | 104 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 model->Add(new TemplateURL(data)); | 183 model->Add(new TemplateURL(data)); |
| 194 break; | 184 break; |
| 195 | 185 |
| 196 case EXPLICIT_PROVIDER: | 186 case EXPLICIT_PROVIDER: |
| 197 // Confirm addition and allow user to edit default choices. It's ironic | 187 // Confirm addition and allow user to edit default choices. It's ironic |
| 198 // that only *non*-autodetected additions get confirmed, but the user | 188 // that only *non*-autodetected additions get confirmed, but the user |
| 199 // expects feedback that his action did something. | 189 // expects feedback that his action did something. |
| 200 // The source WebContents' delegate takes care of adding the URL to the | 190 // The source WebContents' delegate takes care of adding the URL to the |
| 201 // model, which takes ownership, or of deleting it if the add is | 191 // model, which takes ownership, or of deleting it if the add is |
| 202 // cancelled. | 192 // cancelled. |
| 203 callback_.Run(make_scoped_ptr(new TemplateURL(data))); | 193 confirm_add_callback_.Run(make_scoped_ptr(new TemplateURL(data))); |
| 204 break; | 194 break; |
| 205 | 195 |
| 206 default: | 196 default: |
| 207 NOTREACHED(); | 197 NOTREACHED(); |
| 208 break; | 198 break; |
| 209 } | 199 } |
| 210 | 200 |
| 211 fetcher_->RequestCompleted(this); | 201 fetcher_->RequestCompleted(this); |
| 212 // WARNING: RequestCompleted deletes us. | 202 // WARNING: RequestCompleted deletes us. |
| 213 } | 203 } |
| 214 | 204 |
| 215 // TemplateURLFetcher --------------------------------------------------------- | 205 // TemplateURLFetcher --------------------------------------------------------- |
| 216 | 206 |
| 217 TemplateURLFetcher::TemplateURLFetcher( | 207 TemplateURLFetcher::TemplateURLFetcher( |
| 218 TemplateURLService* template_url_service, | 208 TemplateURLService* template_url_service, |
| 219 net::URLRequestContextGetter* request_context) | 209 net::URLRequestContextGetter* request_context) |
| 220 : template_url_service_(template_url_service), | 210 : template_url_service_(template_url_service), |
| 221 request_context_(request_context) { | 211 request_context_(request_context) { |
| 222 } | 212 } |
| 223 | 213 |
| 224 TemplateURLFetcher::~TemplateURLFetcher() { | 214 TemplateURLFetcher::~TemplateURLFetcher() { |
| 225 } | 215 } |
| 226 | 216 |
| 227 void TemplateURLFetcher::ScheduleDownload( | 217 void TemplateURLFetcher::ScheduleDownload( |
| 228 const base::string16& keyword, | 218 const base::string16& keyword, |
| 229 const GURL& osdd_url, | 219 const GURL& osdd_url, |
| 230 const GURL& favicon_url, | 220 const GURL& favicon_url, |
| 231 content::WebContents* web_contents, | 221 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, |
| 232 const ConfirmAddSearchProviderCallback& callback, | 222 const ConfirmAddSearchProviderCallback& confirm_add_callback, |
| 233 ProviderType provider_type) { | 223 ProviderType provider_type) { |
| 234 DCHECK(osdd_url.is_valid()); | 224 DCHECK(osdd_url.is_valid()); |
| 235 | 225 |
| 236 // For a JS-added OSDD, the provided keyword is irrelevant because we will | 226 // For a JS-added OSDD, the provided keyword is irrelevant because we will |
| 237 // generate a keyword later from the OSDD content. For the autodetected case, | 227 // generate a keyword later from the OSDD content. For the autodetected case, |
| 238 // we need a valid keyword up front. | 228 // we need a valid keyword up front. |
| 239 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 229 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
| 240 DCHECK(!keyword.empty()); | 230 DCHECK(!keyword.empty()); |
| 241 | 231 |
| 242 if (!template_url_service_->loaded()) { | 232 if (!template_url_service_->loaded()) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 254 } | 244 } |
| 255 | 245 |
| 256 // Make sure we aren't already downloading this request. | 246 // Make sure we aren't already downloading this request. |
| 257 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 247 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
| 258 if (((*i)->url() == osdd_url) || | 248 if (((*i)->url() == osdd_url) || |
| 259 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && | 249 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && |
| 260 ((*i)->keyword() == keyword))) | 250 ((*i)->keyword() == keyword))) |
| 261 return; | 251 return; |
| 262 } | 252 } |
| 263 | 253 |
| 264 requests_.push_back( | 254 requests_.push_back(new RequestDelegate( |
| 265 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, | 255 this, keyword, osdd_url, favicon_url, url_fetcher_customize_callback, |
| 266 callback, provider_type)); | 256 confirm_add_callback, provider_type)); |
| 267 } | 257 } |
| 268 | 258 |
| 269 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 259 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
| 270 Requests::iterator i = | 260 Requests::iterator i = |
| 271 std::find(requests_.begin(), requests_.end(), request); | 261 std::find(requests_.begin(), requests_.end(), request); |
| 272 DCHECK(i != requests_.end()); | 262 DCHECK(i != requests_.end()); |
| 273 requests_.weak_erase(i); | 263 requests_.weak_erase(i); |
| 274 delete request; | 264 delete request; |
| 275 } | 265 } |
| OLD | NEW |