Upload upstream chromium 67.0.3396
[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 net {
22 class URLFetcher;
23 class URLRequestContextGetter;
24 }
25
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.
29 //
30 class TemplateURLFetcher : public KeyedService {
31  public:
32   typedef base::Callback<void(
33       net::URLFetcher* url_fetcher)> URLFetcherCustomizeCallback;
34
35   // Creates a TemplateURLFetcher.
36   TemplateURLFetcher(TemplateURLService* template_url_service,
37                      net::URLRequestContextGetter* request_context);
38   ~TemplateURLFetcher() override;
39
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.
43   //
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.
47   //
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,
53       const GURL& osdd_url,
54       const GURL& favicon_url,
55       const URLFetcherCustomizeCallback& url_fetcher_customize_callback);
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   scoped_refptr<net::URLRequestContextGetter> request_context_;
73
74   // In progress requests.
75   std::vector<std::unique_ptr<RequestDelegate>> requests_;
76
77   DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher);
78 };
79
80 #endif  // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_