Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search_engines / template_url_fetcher.h
1 // Copyright (c) 2012 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 CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_
7
8 #include "base/memory/scoped_vector.h"
9 #include "base/strings/string16.h"
10 #include "components/keyed_service/core/keyed_service.h"
11 #include "ui/gfx/native_widget_types.h"
12
13 class GURL;
14 class Profile;
15 class TemplateURL;
16 class TemplateURLFetcherCallbacks;
17
18 namespace content {
19 class WebContents;
20 }
21
22 // TemplateURLFetcher is responsible for downloading OpenSearch description
23 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL
24 // to the TemplateURLService. Downloading is done in the background.
25 //
26 class TemplateURLFetcher : public KeyedService {
27  public:
28   enum ProviderType {
29     AUTODETECTED_PROVIDER,
30     EXPLICIT_PROVIDER  // Supplied by Javascript.
31   };
32
33   // Creates a TemplateURLFetcher with the specified Profile.
34   explicit TemplateURLFetcher(Profile* profile);
35   virtual ~TemplateURLFetcher();
36
37   // If TemplateURLFetcher is not already downloading the OSDD for osdd_url,
38   // it is downloaded. If successful and the result can be parsed, a TemplateURL
39   // is added to the TemplateURLService. Takes ownership of |callbacks|.
40   //
41   // If |provider_type| is AUTODETECTED_PROVIDER, |keyword| must be non-empty,
42   // and if there's already a non-replaceable TemplateURL in the model for
43   // |keyword|, or we're already downloading an OSDD for this keyword, no
44   // download is started.  If |provider_type| is EXPLICIT_PROVIDER, |keyword| is
45   // ignored.
46   //
47   // |web_contents| specifies which WebContents displays the page the OSDD is
48   // downloaded for. |web_contents| must not be NULL, except during tests.
49   void ScheduleDownload(const base::string16& keyword,
50                         const GURL& osdd_url,
51                         const GURL& favicon_url,
52                         content::WebContents* web_contents,
53                         TemplateURLFetcherCallbacks* callbacks,
54                         ProviderType provider_type);
55
56   // The current number of outstanding requests.
57   int requests_count() const { return requests_.size(); }
58
59  private:
60   // A RequestDelegate is created to download each OSDD. When done downloading
61   // RequestCompleted is invoked back on the TemplateURLFetcher.
62   class RequestDelegate;
63   friend class RequestDelegate;
64
65   typedef ScopedVector<RequestDelegate> Requests;
66
67   Profile* profile() const { return profile_; }
68
69   // Invoked from the RequestDelegate when done downloading.
70   void RequestCompleted(RequestDelegate* request);
71
72   Profile* profile_;
73
74   // In progress requests.
75   Requests requests_;
76
77   DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher);
78 };
79
80 #endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_