Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / components / search_engines / template_url_fetcher.h
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 <memory>
9 #include <vector>
10
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"
16
17 class GURL;
18 class TemplateURL;
19 class TemplateURLService;
20
21 namespace network {
22 namespace mojom {
23 class URLLoaderFactory;
24 }
25 }  // namespace network
26
27 namespace url {
28 class Origin;
29 }
30
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.
34 //
35 class TemplateURLFetcher : public KeyedService {
36  public:
37   // Creates a TemplateURLFetcher.
38   explicit TemplateURLFetcher(TemplateURLService* template_url_service);
39   ~TemplateURLFetcher() override;
40
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.
44   //
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.
48   //
49   void ScheduleDownload(const base::string16& keyword,
50                         const GURL& osdd_url,
51                         const GURL& favicon_url,
52                         const url::Origin& initiator,
53                         network::mojom::URLLoaderFactory* url_loader_factory,
54                         int render_frame_id,
55                         int resource_type);
56
57   // The current number of outstanding requests.
58   int requests_count() const { return requests_.size(); }
59
60  protected:
61   // A RequestDelegate is created to download each OSDD. When done downloading
62   // RequestCompleted is invoked back on the TemplateURLFetcher.
63   class RequestDelegate;
64
65   // Invoked from the RequestDelegate when done downloading. Virtual for tests.
66   virtual void RequestCompleted(RequestDelegate* request);
67
68  private:
69   friend class RequestDelegate;
70
71   TemplateURLService* template_url_service_;
72
73   // In progress requests.
74   std::vector<std::unique_ptr<RequestDelegate>> requests_;
75
76   DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher);
77 };
78
79 #endif  // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_