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.
5 #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_
6 #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/string16.h"
15 #include "components/keyed_service/core/keyed_service.h"
19 class TemplateURLService;
23 class URLRequestContextGetter;
26 // TemplateURLFetcher is responsible for downloading OpenSearch description
27 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL
28 // to the TemplateURLService. Downloading is done in the background.
30 class TemplateURLFetcher : public KeyedService {
32 typedef base::Callback<void(
33 net::URLFetcher* url_fetcher)> URLFetcherCustomizeCallback;
35 // Creates a TemplateURLFetcher.
36 TemplateURLFetcher(TemplateURLService* template_url_service,
37 net::URLRequestContextGetter* request_context);
38 ~TemplateURLFetcher() override;
40 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url,
41 // it is downloaded. If successful and the result can be parsed, a TemplateURL
42 // is added to the TemplateURLService.
44 // |keyword| must be non-empty. If there's already a non-replaceable
45 // TemplateURL in the model for |keyword|, or we're already downloading an
46 // OSDD for this keyword, no download is started.
48 // If |url_fetcher_customize_callback| is not null, it's run after a
49 // URLFetcher is created. This callback can be used to set additional
50 // parameters on the URLFetcher.
51 void ScheduleDownload(
52 const base::string16& keyword,
54 const GURL& favicon_url,
55 const URLFetcherCustomizeCallback& url_fetcher_customize_callback);
57 // The current number of outstanding requests.
58 int requests_count() const { return requests_.size(); }
61 // A RequestDelegate is created to download each OSDD. When done downloading
62 // RequestCompleted is invoked back on the TemplateURLFetcher.
63 class RequestDelegate;
65 // Invoked from the RequestDelegate when done downloading. Virtual for tests.
66 virtual void RequestCompleted(RequestDelegate* request);
69 friend class RequestDelegate;
71 TemplateURLService* template_url_service_;
72 scoped_refptr<net::URLRequestContextGetter> request_context_;
74 // In progress requests.
75 std::vector<std::unique_ptr<RequestDelegate>> requests_;
77 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher);
80 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_