blob: 465148c1df570af48828c159f72b1734861b46d9 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_
6#define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_
7
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include <string>
9
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000010#include "base/files/file_path.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "base/memory/singleton.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010012#include "base/strings/string16.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "base/values.h"
14#include "chrome/browser/download/all_download_item_notifier.h"
Ben Murdoch558790d2013-07-30 15:19:42 +010015#include "chrome/browser/download/download_danger_prompt.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010016#include "chrome/browser/download/download_path_reservation_tracker.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "chrome/browser/extensions/event_router.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "chrome/browser/extensions/extension_function.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010019#include "chrome/common/extensions/api/downloads.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#include "content/public/browser/download_item.h"
21#include "content/public/browser/download_manager.h"
22
23class DownloadFileIconExtractor;
24class DownloadQuery;
25
26namespace content {
27class ResourceContext;
28class ResourceDispatcherHost;
29}
30
31// Functions in the chrome.downloads namespace facilitate
32// controlling downloads from extensions. See the full API doc at
33// http://goo.gl/6hO1n
34
35namespace download_extension_errors {
36
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000037// Errors that can be returned through chrome.runtime.lastError.message.
Ben Murdoch558790d2013-07-30 15:19:42 +010038extern const char kEmptyFile[];
39extern const char kFileAlreadyDeleted[];
40extern const char kIconNotFound[];
41extern const char kInvalidDangerType[];
42extern const char kInvalidFilename[];
43extern const char kInvalidFilter[];
44extern const char kInvalidHeader[];
45extern const char kInvalidId[];
46extern const char kInvalidOrderBy[];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047extern const char kInvalidQueryLimit[];
Ben Murdoch558790d2013-07-30 15:19:42 +010048extern const char kInvalidState[];
49extern const char kInvalidURL[];
50extern const char kInvisibleContext[];
51extern const char kNotComplete[];
52extern const char kNotDangerous[];
53extern const char kNotInProgress[];
54extern const char kNotResumable[];
55extern const char kOpenPermission[];
56extern const char kTooManyListeners[];
57extern const char kUnexpectedDeterminer[];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058
59} // namespace download_extension_errors
60
61
62class DownloadsDownloadFunction : public AsyncExtensionFunction {
63 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064 DECLARE_EXTENSION_FUNCTION("downloads.download", DOWNLOADS_DOWNLOAD)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065 DownloadsDownloadFunction();
66 virtual bool RunImpl() OVERRIDE;
67
68 protected:
69 virtual ~DownloadsDownloadFunction();
70
71 private:
Ben Murdoch558790d2013-07-30 15:19:42 +010072 void OnStarted(
73 const base::FilePath& creator_suggested_filename,
74 extensions::api::downloads::FilenameConflictAction
75 creator_conflict_action,
76 content::DownloadItem* item,
77 net::Error error);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000078
79 DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction);
80};
81
82class DownloadsSearchFunction : public SyncExtensionFunction {
83 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000084 DECLARE_EXTENSION_FUNCTION("downloads.search", DOWNLOADS_SEARCH)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000085 DownloadsSearchFunction();
86 virtual bool RunImpl() OVERRIDE;
87
88 protected:
89 virtual ~DownloadsSearchFunction();
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction);
93};
94
95class DownloadsPauseFunction : public SyncExtensionFunction {
96 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000097 DECLARE_EXTENSION_FUNCTION("downloads.pause", DOWNLOADS_PAUSE)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000098 DownloadsPauseFunction();
99 virtual bool RunImpl() OVERRIDE;
100
101 protected:
102 virtual ~DownloadsPauseFunction();
103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction);
106};
107
108class DownloadsResumeFunction : public SyncExtensionFunction {
109 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000110 DECLARE_EXTENSION_FUNCTION("downloads.resume", DOWNLOADS_RESUME)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000111 DownloadsResumeFunction();
112 virtual bool RunImpl() OVERRIDE;
113
114 protected:
115 virtual ~DownloadsResumeFunction();
116
117 private:
118 DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction);
119};
120
121class DownloadsCancelFunction : public SyncExtensionFunction {
122 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000123 DECLARE_EXTENSION_FUNCTION("downloads.cancel", DOWNLOADS_CANCEL)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124 DownloadsCancelFunction();
125 virtual bool RunImpl() OVERRIDE;
126
127 protected:
128 virtual ~DownloadsCancelFunction();
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction);
132};
133
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000134class DownloadsEraseFunction : public SyncExtensionFunction {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000135 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000136 DECLARE_EXTENSION_FUNCTION("downloads.erase", DOWNLOADS_ERASE)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000137 DownloadsEraseFunction();
138 virtual bool RunImpl() OVERRIDE;
139
140 protected:
141 virtual ~DownloadsEraseFunction();
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction);
145};
146
Ben Murdoch558790d2013-07-30 15:19:42 +0100147class DownloadsRemoveFileFunction : public AsyncExtensionFunction,
148 public content::DownloadItem::Observer {
149 public:
150 DECLARE_EXTENSION_FUNCTION("downloads.removeFile", DOWNLOADS_REMOVEFILE)
151 DownloadsRemoveFileFunction();
152 virtual bool RunImpl() OVERRIDE;
153
154 protected:
155 virtual ~DownloadsRemoveFileFunction();
156
157 private:
158 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE;
159 virtual void OnDownloadDestroyed(content::DownloadItem* item) OVERRIDE;
160
161 content::DownloadItem* item_;
162
163 DISALLOW_COPY_AND_ASSIGN(DownloadsRemoveFileFunction);
164};
165
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000166class DownloadsAcceptDangerFunction : public AsyncExtensionFunction {
167 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000168 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000169 DownloadsAcceptDangerFunction();
170 virtual bool RunImpl() OVERRIDE;
171
172 protected:
173 virtual ~DownloadsAcceptDangerFunction();
Ben Murdoch558790d2013-07-30 15:19:42 +0100174 void DangerPromptCallback(int download_id,
175 DownloadDangerPrompt::Action action);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction);
179};
180
181class DownloadsShowFunction : public AsyncExtensionFunction {
182 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000183 DECLARE_EXTENSION_FUNCTION("downloads.show", DOWNLOADS_SHOW)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184 DownloadsShowFunction();
185 virtual bool RunImpl() OVERRIDE;
186
187 protected:
188 virtual ~DownloadsShowFunction();
189
190 private:
191 DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction);
192};
193
Ben Murdoch558790d2013-07-30 15:19:42 +0100194class DownloadsShowDefaultFolderFunction : public AsyncExtensionFunction {
195 public:
196 DECLARE_EXTENSION_FUNCTION(
197 "downloads.showDefaultFolder", DOWNLOADS_SHOWDEFAULTFOLDER)
198 DownloadsShowDefaultFolderFunction();
199 virtual bool RunImpl() OVERRIDE;
200
201 protected:
202 virtual ~DownloadsShowDefaultFolderFunction();
203
204 private:
205 DISALLOW_COPY_AND_ASSIGN(DownloadsShowDefaultFolderFunction);
206};
207
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000208class DownloadsOpenFunction : public SyncExtensionFunction {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000209 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000210 DECLARE_EXTENSION_FUNCTION("downloads.open", DOWNLOADS_OPEN)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000211 DownloadsOpenFunction();
212 virtual bool RunImpl() OVERRIDE;
213
214 protected:
215 virtual ~DownloadsOpenFunction();
216
217 private:
218 DISALLOW_COPY_AND_ASSIGN(DownloadsOpenFunction);
219};
220
221class DownloadsDragFunction : public AsyncExtensionFunction {
222 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000223 DECLARE_EXTENSION_FUNCTION("downloads.drag", DOWNLOADS_DRAG)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000224 DownloadsDragFunction();
225 virtual bool RunImpl() OVERRIDE;
226
227 protected:
228 virtual ~DownloadsDragFunction();
229
230 private:
231 DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction);
232};
233
234class DownloadsGetFileIconFunction : public AsyncExtensionFunction {
235 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000236 DECLARE_EXTENSION_FUNCTION("downloads.getFileIcon", DOWNLOADS_GETFILEICON)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000237 DownloadsGetFileIconFunction();
238 virtual bool RunImpl() OVERRIDE;
239 void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor);
240
241 protected:
242 virtual ~DownloadsGetFileIconFunction();
243
244 private:
245 void OnIconURLExtracted(const std::string& url);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000246 base::FilePath path_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000247 scoped_ptr<DownloadFileIconExtractor> icon_extractor_;
248 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction);
249};
250
251// Observes a single DownloadManager and many DownloadItems and dispatches
252// onCreated and onErased events.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000253class ExtensionDownloadsEventRouter : public extensions::EventRouter::Observer,
254 public AllDownloadItemNotifier::Observer {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000255 public:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100256 typedef base::Callback<void(
257 const base::FilePath& changed_filename,
258 DownloadPathReservationTracker::FilenameConflictAction)>
259 FilenameChangedCallback;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000260
261 // A downloads.onDeterminingFilename listener has returned. If the extension
262 // wishes to override the download's filename, then |filename| will be
263 // non-empty. |filename| will be interpreted as a relative path, appended to
264 // the default downloads directory. If the extension wishes to overwrite any
265 // existing files, then |overwrite| will be true. Returns true on success,
266 // false otherwise.
267 static bool DetermineFilename(
268 Profile* profile,
269 bool include_incognito,
270 const std::string& ext_id,
271 int download_id,
272 const base::FilePath& filename,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100273 extensions::api::downloads::FilenameConflictAction conflict_action,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000274 std::string* error);
275
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000276 explicit ExtensionDownloadsEventRouter(
277 Profile* profile, content::DownloadManager* manager);
278 virtual ~ExtensionDownloadsEventRouter();
279
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000280 // Called by ChromeDownloadManagerDelegate during the filename determination
281 // process, allows extensions to change the item's target filename. If no
282 // extension wants to change the target filename, then |no_change| will be
283 // called and the filename determination process will continue as normal. If
284 // an extension wants to change the target filename, then |change| will be
285 // called with the new filename and a flag indicating whether the new file
286 // should overwrite any old files of the same name.
287 void OnDeterminingFilename(
288 content::DownloadItem* item,
289 const base::FilePath& suggested_path,
290 const base::Closure& no_change,
291 const FilenameChangedCallback& change);
292
293 // AllDownloadItemNotifier::Observer
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000294 virtual void OnDownloadCreated(
295 content::DownloadManager* manager,
296 content::DownloadItem* download_item) OVERRIDE;
297 virtual void OnDownloadUpdated(
298 content::DownloadManager* manager,
299 content::DownloadItem* download_item) OVERRIDE;
300 virtual void OnDownloadRemoved(
301 content::DownloadManager* manager,
302 content::DownloadItem* download_item) OVERRIDE;
303
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000304 // extensions::EventRouter::Observer
305 virtual void OnListenerRemoved(
306 const extensions::EventListenerInfo& details) OVERRIDE;
307
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000308 // Used for testing.
309 struct DownloadsNotificationSource {
310 std::string event_name;
311 Profile* profile;
312 };
313
314 private:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000315 void DispatchEvent(
316 const char* event_name,
317 bool include_incognito,
318 const extensions::Event::WillDispatchCallback& will_dispatch_callback,
319 base::Value* json_arg);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000320
321 Profile* profile_;
322 AllDownloadItemNotifier notifier_;
323
324 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter);
325};
326
327#endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_