OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_TEMPLATE_URL_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_TEMPLATE_URL_FETCHER_H_ |
6 #define CHROME_BROWSER_TEMPLATE_URL_FETCHER_H_ | 6 #define CHROME_BROWSER_TEMPLATE_URL_FETCHER_H_ |
7 | 7 |
8 #include "base/gfx/native_widget_types.h" | 8 #include "base/gfx/native_widget_types.h" |
9 #include "chrome/browser/net/url_fetcher.h" | |
10 #include "chrome/browser/profile.h" | |
11 #include "chrome/common/scoped_vector.h" | 9 #include "chrome/common/scoped_vector.h" |
12 | 10 |
13 class GURL; | 11 class GURL; |
14 class Profile; | 12 class Profile; |
15 class TemplateURL; | 13 class TemplateURL; |
16 class WebContents; | 14 class WebContents; |
17 | 15 |
18 // TemplateURLFetcher is responsible for downloading OpenSearch description | 16 // TemplateURLFetcher is responsible for downloading OpenSearch description |
19 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL | 17 // documents, creating a TemplateURL from the OSDD, and adding the TemplateURL |
20 // to the TemplateURLModel. Downloading is done in the background. | 18 // to the TemplateURLModel. Downloading is done in the background. |
21 // | 19 // |
22 class TemplateURLFetcher { | 20 class TemplateURLFetcher { |
23 public: | 21 public: |
24 // Creates a TemplateURLFetcher with the specified Profile. | 22 // Creates a TemplateURLFetcher with the specified Profile. |
25 explicit TemplateURLFetcher(Profile* profile); | 23 explicit TemplateURLFetcher(Profile* profile); |
| 24 ~TemplateURLFetcher(); |
26 | 25 |
27 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url, | 26 // If TemplateURLFetcher is not already downloading the OSDD for osdd_url, |
28 // it is downloaded. If successful and the result can be parsed, a TemplateURL | 27 // it is downloaded. If successful and the result can be parsed, a TemplateURL |
29 // is added to the TemplateURLModel. | 28 // is added to the TemplateURLModel. |
30 void ScheduleDownload(const std::wstring& keyword, | 29 void ScheduleDownload(const std::wstring& keyword, |
31 const GURL& osdd_url, | 30 const GURL& osdd_url, |
32 const GURL& favicon_url, | 31 const GURL& favicon_url, |
33 const gfx::NativeView parent_window, | 32 const gfx::NativeView parent_window, |
34 bool autodetected); | 33 bool autodetected); |
35 | 34 |
36 private: | 35 private: |
37 friend class RequestDelegate; | 36 friend class RequestDelegate; |
38 | 37 |
39 // A RequestDelegate is created to download each OSDD. When done downloading | 38 // A RequestDelegate is created to download each OSDD. When done downloading |
40 // RequestCompleted is invoked back on the TemplateURLFetcher. | 39 // RequestCompleted is invoked back on the TemplateURLFetcher. |
41 class RequestDelegate : public URLFetcher::Delegate { | 40 class RequestDelegate; |
42 public: | |
43 RequestDelegate(TemplateURLFetcher* fetcher, | |
44 const std::wstring& keyword, | |
45 const GURL& osdd_url, | |
46 const GURL& favicon_url, | |
47 gfx::NativeView parent_window, | |
48 bool autodetected) | |
49 #pragma warning(disable:4355) | |
50 : url_fetcher_(osdd_url, URLFetcher::GET, this), | |
51 fetcher_(fetcher), | |
52 keyword_(keyword), | |
53 osdd_url_(osdd_url), | |
54 favicon_url_(favicon_url), | |
55 parent_window_(parent_window), | |
56 autodetected_(autodetected) { | |
57 url_fetcher_.set_request_context(fetcher->profile()->GetRequestContext()); | |
58 url_fetcher_.Start(); | |
59 } | |
60 | |
61 // If data contains a valid OSDD, a TemplateURL is created and added to | |
62 // the TemplateURLModel. | |
63 virtual void OnURLFetchComplete(const URLFetcher* source, | |
64 const GURL& url, | |
65 const URLRequestStatus& status, | |
66 int response_code, | |
67 const ResponseCookies& cookies, | |
68 const std::string& data); | |
69 | |
70 // URL of the OSDD. | |
71 const GURL& url() const { return osdd_url_; } | |
72 | |
73 // Keyword to use. | |
74 const std::wstring keyword() const { return keyword_; } | |
75 | |
76 private: | |
77 URLFetcher url_fetcher_; | |
78 TemplateURLFetcher* fetcher_; | |
79 const std::wstring keyword_; | |
80 const GURL osdd_url_; | |
81 const GURL favicon_url_; | |
82 bool autodetected_; | |
83 | |
84 // Used to determine where to place a confirmation dialog. May be NULL, | |
85 // in which case the confirmation will be centered in the screen if needed. | |
86 gfx::NativeView parent_window_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); | |
89 }; | |
90 | 41 |
91 Profile* profile() const { return profile_; } | 42 Profile* profile() const { return profile_; } |
92 | 43 |
93 // Invoked from the RequestDelegate when done downloading. | 44 // Invoked from the RequestDelegate when done downloading. |
94 void RequestCompleted(RequestDelegate* request); | 45 void RequestCompleted(RequestDelegate* request); |
95 | 46 |
96 Profile* profile_; | 47 Profile* profile_; |
97 | 48 |
98 // In progress requests. | 49 // In progress requests. |
99 ScopedVector<RequestDelegate> requests_; | 50 ScopedVector<RequestDelegate> requests_; |
100 | 51 |
101 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher); | 52 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcher); |
102 }; | 53 }; |
103 | 54 |
104 #endif // CHROME_BROWSER_OSDD_FETCHER_H_ | 55 #endif // CHROME_BROWSER_OSDD_FETCHER_H_ |
OLD | NEW |