OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ |
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ |
7 | 7 |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
10 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
11 #include "ui/gfx/native_widget_types.h" | |
12 | 14 |
13 class GURL; | 15 class GURL; |
14 class Profile; | |
15 class TemplateURL; | 16 class TemplateURL; |
16 class TemplateURLFetcherCallbacks; | 17 class TemplateURLService; |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 class WebContents; | 20 class WebContents; |
20 } | 21 } |
21 | 22 |
| 23 namespace net { |
| 24 class URLRequestContextGetter; |
| 25 } |
| 26 |
22 // TemplateURLFetcher is responsible for downloading OpenSearch description | 27 // TemplateURLFetcher is responsible for downloading OpenSearch description |
23 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL | 28 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL |
24 // to the TemplateURLService. Downloading is done in the background. | 29 // to the TemplateURLService. Downloading is done in the background. |
25 // | 30 // |
26 class TemplateURLFetcher : public KeyedService { | 31 class TemplateURLFetcher : public KeyedService { |
27 public: | 32 public: |
| 33 typedef base::Callback<void( |
| 34 scoped_ptr<TemplateURL> template_url)> ConfirmAddSearchProviderCallback; |
| 35 |
28 enum ProviderType { | 36 enum ProviderType { |
29 AUTODETECTED_PROVIDER, | 37 AUTODETECTED_PROVIDER, |
30 EXPLICIT_PROVIDER // Supplied by Javascript. | 38 EXPLICIT_PROVIDER // Supplied by Javascript. |
31 }; | 39 }; |
32 | 40 |
33 // Creates a TemplateURLFetcher with the specified Profile. | 41 // Creates a TemplateURLFetcher. |
34 explicit TemplateURLFetcher(Profile* profile); | 42 TemplateURLFetcher(TemplateURLService* template_url_service, |
| 43 net::URLRequestContextGetter* request_context); |
35 virtual ~TemplateURLFetcher(); | 44 virtual ~TemplateURLFetcher(); |
36 | 45 |
37 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url, | 46 // 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 | 47 // it is downloaded. If successful and the result can be parsed, a TemplateURL |
39 // is added to the TemplateURLService. Takes ownership of |callbacks|. | 48 // is added to the TemplateURLService. Takes ownership of |callbacks|. |
40 // | 49 // |
41 // If |provider_type| is AUTODETECTED_PROVIDER, |keyword| must be non-empty, | 50 // 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 | 51 // 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 | 52 // |keyword|, or we're already downloading an OSDD for this keyword, no |
44 // download is started. If |provider_type| is EXPLICIT_PROVIDER, |keyword| is | 53 // download is started. If |provider_type| is EXPLICIT_PROVIDER, |keyword| is |
45 // ignored. | 54 // ignored. |
46 // | 55 // |
47 // |web_contents| specifies which WebContents displays the page the OSDD is | 56 // |web_contents| specifies which WebContents displays the page the OSDD is |
48 // downloaded for. |web_contents| must not be NULL, except during tests. | 57 // downloaded for. |web_contents| must not be NULL, except during tests. |
49 void ScheduleDownload(const base::string16& keyword, | 58 void ScheduleDownload(const base::string16& keyword, |
50 const GURL& osdd_url, | 59 const GURL& osdd_url, |
51 const GURL& favicon_url, | 60 const GURL& favicon_url, |
52 content::WebContents* web_contents, | 61 content::WebContents* web_contents, |
53 TemplateURLFetcherCallbacks* callbacks, | 62 const ConfirmAddSearchProviderCallback& callback, |
54 ProviderType provider_type); | 63 ProviderType provider_type); |
55 | 64 |
56 // The current number of outstanding requests. | 65 // The current number of outstanding requests. |
57 int requests_count() const { return requests_.size(); } | 66 int requests_count() const { return requests_.size(); } |
58 | 67 |
59 private: | 68 private: |
60 // A RequestDelegate is created to download each OSDD. When done downloading | 69 // A RequestDelegate is created to download each OSDD. When done downloading |
61 // RequestCompleted is invoked back on the TemplateURLFetcher. | 70 // RequestCompleted is invoked back on the TemplateURLFetcher. |
62 class RequestDelegate; | 71 class RequestDelegate; |
63 friend class RequestDelegate; | 72 friend class RequestDelegate; |
64 | 73 |
65 typedef ScopedVector<RequestDelegate> Requests; | 74 typedef ScopedVector<RequestDelegate> Requests; |
66 | 75 |
67 Profile* profile() const { return profile_; } | |
68 | |
69 // Invoked from the RequestDelegate when done downloading. | 76 // Invoked from the RequestDelegate when done downloading. |
70 void RequestCompleted(RequestDelegate* request); | 77 void RequestCompleted(RequestDelegate* request); |
71 | 78 |
72 Profile* profile_; | 79 TemplateURLService* template_url_service_; |
| 80 scoped_refptr<net::URLRequestContextGetter> request_context_; |
73 | 81 |
74 // In progress requests. | 82 // In progress requests. |
75 Requests requests_; | 83 Requests requests_; |
76 | 84 |
77 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher); | 85 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher); |
78 }; | 86 }; |
79 | 87 |
80 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ | 88 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_H_ |
OLD | NEW |