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 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ 6 #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_vector.h" 12 #include "base/strings/string16.h" 13 #include "components/keyed_service/core/keyed_service.h" 14 15 class GURL; 16 class TemplateURL; 17 class TemplateURLService; 18 19 namespace net { 20 class URLFetcher; 21 class URLRequestContextGetter; 22 } 23 24 // TemplateURLFetcher is responsible for downloading OpenSearch description 25 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL 26 // to the TemplateURLService. Downloading is done in the background. 27 // 28 class TemplateURLFetcher : public KeyedService { 29 public: 30 typedef base::Callback<void( 31 net::URLFetcher* url_fetcher)> URLFetcherCustomizeCallback; 32 typedef base::Callback<void( 33 scoped_ptr<TemplateURL> template_url)> ConfirmAddSearchProviderCallback; 34 35 enum ProviderType { 36 AUTODETECTED_PROVIDER, 37 EXPLICIT_PROVIDER // Supplied by Javascript. 38 }; 39 40 // Creates a TemplateURLFetcher. 41 TemplateURLFetcher(TemplateURLService* template_url_service, 42 net::URLRequestContextGetter* request_context); 43 virtual ~TemplateURLFetcher(); 44 45 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url, 46 // it is downloaded. If successful and the result can be parsed, a TemplateURL 47 // is added to the TemplateURLService. 48 // 49 // If |provider_type| is AUTODETECTED_PROVIDER, |keyword| must be non-empty, 50 // and if there's already a non-replaceable TemplateURL in the model for 51 // |keyword|, or we're already downloading an OSDD for this keyword, no 52 // download is started. If |provider_type| is EXPLICIT_PROVIDER, |keyword| is 53 // ignored. 54 // 55 // If |url_fetcher_customize_callback| is not null, it's run after a 56 // URLFetcher is created. This callback can be used to set additional 57 // parameters on the URLFetcher. 58 void ScheduleDownload( 59 const base::string16& keyword, 60 const GURL& osdd_url, 61 const GURL& favicon_url, 62 const URLFetcherCustomizeCallback& url_fetcher_customize_callback, 63 const ConfirmAddSearchProviderCallback& confirm_add_callback, 64 ProviderType provider_type); 65 66 // The current number of outstanding requests. 67 int requests_count() const { return requests_.size(); } 68 69 private: 70 // A RequestDelegate is created to download each OSDD. When done downloading 71 // RequestCompleted is invoked back on the TemplateURLFetcher. 72 class RequestDelegate; 73 friend class RequestDelegate; 74 75 typedef ScopedVector<RequestDelegate> Requests; 76 77 // Invoked from the RequestDelegate when done downloading. 78 void RequestCompleted(RequestDelegate* request); 79 80 TemplateURLService* template_url_service_; 81 scoped_refptr<net::URLRequestContextGetter> request_context_; 82 83 // In progress requests. 84 Requests requests_; 85 86 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher); 87 }; 88 89 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ 90