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 URLLoaderFactory;
25 } // namespace network
31 // TemplateURLFetcher is responsible for downloading OpenSearch description
32 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL
33 // to the TemplateURLService. Downloading is done in the background.
35 class TemplateURLFetcher : public KeyedService {
37 // Creates a TemplateURLFetcher.
38 explicit TemplateURLFetcher(TemplateURLService* template_url_service);
39 ~TemplateURLFetcher() override;
41 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url,
42 // it is downloaded. If successful and the result can be parsed, a TemplateURL
43 // is added to the TemplateURLService.
45 // |keyword| must be non-empty. If there's already a non-replaceable
46 // TemplateURL in the model for |keyword|, or we're already downloading an
47 // OSDD for this keyword, no download is started.
49 void ScheduleDownload(const base::string16& keyword,
51 const GURL& favicon_url,
52 const url::Origin& initiator,
53 network::mojom::URLLoaderFactory* url_loader_factory,
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_;
73 // In progress requests.
74 std::vector<std::unique_ptr<RequestDelegate>> requests_;
76 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher);
79 #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_