OLD | NEW |
| (Empty) |
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_DOWNLOAD_DOWNLOAD_EXTENSION_API_H_ | |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_EXTENSION_API_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 | |
13 #include "base/file_path.h" | |
14 #include "base/memory/singleton.h" | |
15 #include "base/stl_util.h" | |
16 #include "base/string16.h" | |
17 #include "base/values.h" | |
18 #include "chrome/browser/extensions/extension_function.h" | |
19 #include "content/public/browser/download_id.h" | |
20 #include "content/public/browser/download_item.h" | |
21 #include "content/public/browser/download_manager.h" | |
22 | |
23 class DownloadFileIconExtractor; | |
24 class DownloadQuery; | |
25 | |
26 namespace content { | |
27 class ResourceContext; | |
28 class ResourceDispatcherHost; | |
29 } | |
30 | |
31 // Functions in the chrome.experimental.downloads namespace facilitate | |
32 // controlling downloads from extensions. See the full API doc at | |
33 // http://goo.gl/6hO1n | |
34 | |
35 namespace download_extension_errors { | |
36 | |
37 // Errors that can be returned through chrome.extension.lastError.message. | |
38 extern const char kGenericError[]; | |
39 extern const char kIconNotFoundError[]; | |
40 extern const char kInvalidDangerTypeError[]; | |
41 extern const char kInvalidFilterError[]; | |
42 extern const char kInvalidOperationError[]; | |
43 extern const char kInvalidOrderByError[]; | |
44 extern const char kInvalidQueryLimit[]; | |
45 extern const char kInvalidStateError[]; | |
46 extern const char kInvalidUrlError[]; | |
47 extern const char kNotImplementedError[]; | |
48 | |
49 } // namespace download_extension_errors | |
50 | |
51 class DownloadsFunctionInterface { | |
52 public: | |
53 enum DownloadsFunctionName { | |
54 DOWNLOADS_FUNCTION_DOWNLOAD = 0, | |
55 DOWNLOADS_FUNCTION_SEARCH = 1, | |
56 DOWNLOADS_FUNCTION_PAUSE = 2, | |
57 DOWNLOADS_FUNCTION_RESUME = 3, | |
58 DOWNLOADS_FUNCTION_CANCEL = 4, | |
59 DOWNLOADS_FUNCTION_ERASE = 5, | |
60 DOWNLOADS_FUNCTION_SET_DESTINATION = 6, | |
61 DOWNLOADS_FUNCTION_ACCEPT_DANGER = 7, | |
62 DOWNLOADS_FUNCTION_SHOW = 8, | |
63 DOWNLOADS_FUNCTION_DRAG = 9, | |
64 DOWNLOADS_FUNCTION_GET_FILE_ICON = 10, | |
65 // Insert new values here, not at the beginning. | |
66 DOWNLOADS_FUNCTION_LAST | |
67 }; | |
68 | |
69 protected: | |
70 // Return true if args_ is well-formed, otherwise set error_ and return false. | |
71 virtual bool ParseArgs() = 0; | |
72 | |
73 // Implementation-specific logic. "Do the thing that you do." Should return | |
74 // true if the call succeeded and false otherwise. | |
75 virtual bool RunInternal() = 0; | |
76 | |
77 // Which subclass is this. | |
78 virtual DownloadsFunctionName function() const = 0; | |
79 | |
80 // Wrap ParseArgs(), RunInternal(). | |
81 static bool RunImplImpl(DownloadsFunctionInterface* pimpl); | |
82 }; | |
83 | |
84 class SyncDownloadsFunction : public SyncExtensionFunction, | |
85 public DownloadsFunctionInterface { | |
86 protected: | |
87 explicit SyncDownloadsFunction(DownloadsFunctionName function); | |
88 virtual ~SyncDownloadsFunction(); | |
89 | |
90 // ExtensionFunction: | |
91 virtual bool RunImpl() OVERRIDE; | |
92 | |
93 // DownloadsFunctionInterface: | |
94 virtual DownloadsFunctionName function() const OVERRIDE; | |
95 | |
96 content::DownloadItem* GetActiveItem(int download_id); | |
97 | |
98 private: | |
99 DownloadsFunctionName function_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(SyncDownloadsFunction); | |
102 }; | |
103 | |
104 class AsyncDownloadsFunction : public AsyncExtensionFunction, | |
105 public DownloadsFunctionInterface { | |
106 protected: | |
107 explicit AsyncDownloadsFunction(DownloadsFunctionName function); | |
108 virtual ~AsyncDownloadsFunction(); | |
109 | |
110 // ExtensionFunction: | |
111 virtual bool RunImpl() OVERRIDE; | |
112 | |
113 // DownloadsFunctionInterface: | |
114 virtual DownloadsFunctionName function() const OVERRIDE; | |
115 | |
116 content::DownloadItem* GetActiveItem(int download_id); | |
117 | |
118 private: | |
119 DownloadsFunctionName function_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(AsyncDownloadsFunction); | |
122 }; | |
123 | |
124 class DownloadsDownloadFunction : public AsyncDownloadsFunction { | |
125 public: | |
126 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.download"); | |
127 | |
128 DownloadsDownloadFunction(); | |
129 | |
130 protected: | |
131 virtual ~DownloadsDownloadFunction(); | |
132 | |
133 // DownloadsFunctionInterface: | |
134 virtual bool ParseArgs() OVERRIDE; | |
135 virtual bool RunInternal() OVERRIDE; | |
136 | |
137 private: | |
138 struct IOData { | |
139 public: | |
140 IOData(); | |
141 ~IOData(); | |
142 | |
143 GURL url; | |
144 string16 filename; | |
145 bool save_as; | |
146 base::ListValue* extra_headers; | |
147 std::string method; | |
148 std::string post_body; | |
149 content::ResourceDispatcherHost* rdh; | |
150 content::ResourceContext* resource_context; | |
151 int render_process_host_id; | |
152 int render_view_host_routing_id; | |
153 }; | |
154 | |
155 void BeginDownloadOnIOThread(); | |
156 void OnStarted(content::DownloadId dl_id, net::Error error); | |
157 | |
158 scoped_ptr<IOData> iodata_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction); | |
161 }; | |
162 | |
163 class DownloadsSearchFunction : public SyncDownloadsFunction { | |
164 public: | |
165 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.search"); | |
166 | |
167 DownloadsSearchFunction(); | |
168 | |
169 protected: | |
170 virtual ~DownloadsSearchFunction(); | |
171 | |
172 // DownloadsFunctionInterface: | |
173 virtual bool ParseArgs() OVERRIDE; | |
174 virtual bool RunInternal() OVERRIDE; | |
175 | |
176 private: | |
177 bool ParseOrderBy(const base::Value& order_by_value); | |
178 | |
179 scoped_ptr<DownloadQuery> query_; | |
180 int get_id_; | |
181 bool has_get_id_; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction); | |
184 }; | |
185 | |
186 class DownloadsPauseFunction : public SyncDownloadsFunction { | |
187 public: | |
188 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.pause"); | |
189 | |
190 DownloadsPauseFunction(); | |
191 | |
192 protected: | |
193 virtual ~DownloadsPauseFunction(); | |
194 | |
195 // DownloadsFunctionInterface: | |
196 virtual bool ParseArgs() OVERRIDE; | |
197 virtual bool RunInternal() OVERRIDE; | |
198 | |
199 private: | |
200 int download_id_; | |
201 DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction); | |
202 }; | |
203 | |
204 class DownloadsResumeFunction : public SyncDownloadsFunction { | |
205 public: | |
206 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.resume"); | |
207 | |
208 DownloadsResumeFunction(); | |
209 | |
210 protected: | |
211 virtual ~DownloadsResumeFunction(); | |
212 | |
213 // DownloadsFunctionInterface: | |
214 virtual bool ParseArgs() OVERRIDE; | |
215 virtual bool RunInternal() OVERRIDE; | |
216 | |
217 private: | |
218 int download_id_; | |
219 DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction); | |
220 }; | |
221 | |
222 class DownloadsCancelFunction : public SyncDownloadsFunction { | |
223 public: | |
224 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.cancel"); | |
225 | |
226 DownloadsCancelFunction(); | |
227 | |
228 protected: | |
229 virtual ~DownloadsCancelFunction(); | |
230 | |
231 // DownloadsFunctionInterface: | |
232 virtual bool ParseArgs() OVERRIDE; | |
233 virtual bool RunInternal() OVERRIDE; | |
234 | |
235 private: | |
236 int download_id_; | |
237 DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction); | |
238 }; | |
239 | |
240 class DownloadsEraseFunction : public AsyncDownloadsFunction { | |
241 public: | |
242 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.erase"); | |
243 | |
244 DownloadsEraseFunction(); | |
245 | |
246 protected: | |
247 virtual ~DownloadsEraseFunction(); | |
248 | |
249 // DownloadsFunctionInterface: | |
250 virtual bool ParseArgs() OVERRIDE; | |
251 virtual bool RunInternal() OVERRIDE; | |
252 | |
253 private: | |
254 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); | |
255 }; | |
256 | |
257 class DownloadsSetDestinationFunction : public AsyncDownloadsFunction { | |
258 public: | |
259 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.setDestination"); | |
260 | |
261 DownloadsSetDestinationFunction(); | |
262 | |
263 protected: | |
264 virtual ~DownloadsSetDestinationFunction(); | |
265 | |
266 // DownloadsFunctionInterface: | |
267 virtual bool ParseArgs() OVERRIDE; | |
268 virtual bool RunInternal() OVERRIDE; | |
269 | |
270 private: | |
271 DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction); | |
272 }; | |
273 | |
274 class DownloadsAcceptDangerFunction : public AsyncDownloadsFunction { | |
275 public: | |
276 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.acceptDanger"); | |
277 | |
278 DownloadsAcceptDangerFunction(); | |
279 | |
280 protected: | |
281 virtual ~DownloadsAcceptDangerFunction(); | |
282 | |
283 // DownloadsFunctionInterface: | |
284 virtual bool ParseArgs() OVERRIDE; | |
285 virtual bool RunInternal() OVERRIDE; | |
286 | |
287 private: | |
288 DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction); | |
289 }; | |
290 | |
291 class DownloadsShowFunction : public AsyncDownloadsFunction { | |
292 public: | |
293 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.show"); | |
294 | |
295 DownloadsShowFunction(); | |
296 | |
297 protected: | |
298 virtual ~DownloadsShowFunction(); | |
299 | |
300 // DownloadsFunctionInterface: | |
301 virtual bool ParseArgs() OVERRIDE; | |
302 virtual bool RunInternal() OVERRIDE; | |
303 | |
304 private: | |
305 DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction); | |
306 }; | |
307 | |
308 class DownloadsDragFunction : public AsyncDownloadsFunction { | |
309 public: | |
310 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.drag"); | |
311 | |
312 DownloadsDragFunction(); | |
313 | |
314 protected: | |
315 virtual ~DownloadsDragFunction(); | |
316 | |
317 // DownloadsFunctionInterface: | |
318 virtual bool ParseArgs() OVERRIDE; | |
319 virtual bool RunInternal() OVERRIDE; | |
320 | |
321 private: | |
322 DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction); | |
323 }; | |
324 | |
325 class DownloadsGetFileIconFunction : public AsyncDownloadsFunction { | |
326 public: | |
327 DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.getFileIcon"); | |
328 | |
329 DownloadsGetFileIconFunction(); | |
330 void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor); | |
331 | |
332 protected: | |
333 virtual ~DownloadsGetFileIconFunction(); | |
334 | |
335 // DownloadsFunctionInterface: | |
336 virtual bool ParseArgs() OVERRIDE; | |
337 virtual bool RunInternal() OVERRIDE; | |
338 | |
339 private: | |
340 void OnIconURLExtracted(const std::string& url); | |
341 FilePath path_; | |
342 int icon_size_; | |
343 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; | |
344 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); | |
345 }; | |
346 | |
347 // Observes a single DownloadManager and many DownloadItems and dispatches | |
348 // onCreated and onErased events. | |
349 class ExtensionDownloadsEventRouter : public content::DownloadManager::Observer, | |
350 public content::DownloadItem::Observer { | |
351 public: | |
352 explicit ExtensionDownloadsEventRouter(Profile* profile); | |
353 virtual ~ExtensionDownloadsEventRouter(); | |
354 | |
355 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | |
356 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | |
357 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | |
358 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE; | |
359 | |
360 private: | |
361 struct OnChangedStat { | |
362 OnChangedStat(); | |
363 ~OnChangedStat(); | |
364 int fires; | |
365 int total; | |
366 }; | |
367 | |
368 typedef std::map<int, content::DownloadItem*> ItemMap; | |
369 typedef std::map<int, base::DictionaryValue*> ItemJsonMap; | |
370 typedef std::map<int, OnChangedStat*> OnChangedStatMap; | |
371 | |
372 void Init(content::DownloadManager* manager); | |
373 void DispatchEvent(const char* event_name, base::Value* json_arg); | |
374 | |
375 Profile* profile_; | |
376 content::DownloadManager* manager_; | |
377 ItemMap downloads_; | |
378 ItemJsonMap item_jsons_; | |
379 STLValueDeleter<ItemJsonMap> delete_item_jsons_; | |
380 OnChangedStatMap on_changed_stats_; | |
381 STLValueDeleter<OnChangedStatMap> delete_on_changed_stats_; | |
382 | |
383 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); | |
384 }; | |
385 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_EXTENSION_API_H_ | |
OLD | NEW |