blob: a6fda46a07de3433650ce90f29f80a300a6cd3cb [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)a36e5922013-08-05 13:57:33 +01008#include <set>
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include <string>
10
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000011#include "base/files/file_path.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "base/memory/singleton.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010013#include "base/strings/string16.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/values.h"
15#include "chrome/browser/download/all_download_item_notifier.h"
Ben Murdoch558790d2013-07-30 15:19:42 +010016#include "chrome/browser/download/download_danger_prompt.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/browser/download/download_path_reservation_tracker.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "chrome/browser/extensions/event_router.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "chrome/browser/extensions/extension_function.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010020#include "chrome/common/extensions/api/downloads.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021#include "content/public/browser/download_item.h"
22#include "content/public/browser/download_manager.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010023#include "content/public/browser/notification_observer.h"
24#include "content/public/browser/notification_registrar.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025
26class DownloadFileIconExtractor;
27class DownloadQuery;
28
29namespace content {
30class ResourceContext;
31class ResourceDispatcherHost;
32}
33
34// Functions in the chrome.downloads namespace facilitate
35// controlling downloads from extensions. See the full API doc at
36// http://goo.gl/6hO1n
37
38namespace download_extension_errors {
39
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040// Errors that can be returned through chrome.runtime.lastError.message.
Ben Murdoch558790d2013-07-30 15:19:42 +010041extern const char kEmptyFile[];
42extern const char kFileAlreadyDeleted[];
43extern const char kIconNotFound[];
44extern const char kInvalidDangerType[];
45extern const char kInvalidFilename[];
46extern const char kInvalidFilter[];
47extern const char kInvalidHeader[];
48extern const char kInvalidId[];
49extern const char kInvalidOrderBy[];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050extern const char kInvalidQueryLimit[];
Ben Murdoch558790d2013-07-30 15:19:42 +010051extern const char kInvalidState[];
52extern const char kInvalidURL[];
53extern const char kInvisibleContext[];
54extern const char kNotComplete[];
55extern const char kNotDangerous[];
56extern const char kNotInProgress[];
57extern const char kNotResumable[];
58extern const char kOpenPermission[];
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010059extern const char kShelfDisabled[];
60extern const char kShelfPermission[];
Ben Murdoch558790d2013-07-30 15:19:42 +010061extern const char kTooManyListeners[];
62extern const char kUnexpectedDeterminer[];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000063
64} // namespace download_extension_errors
65
66
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010067class DownloadedByExtension : public base::SupportsUserData::Data {
68 public:
69 static DownloadedByExtension* Get(content::DownloadItem* item);
70
71 DownloadedByExtension(content::DownloadItem* item,
72 const std::string& id,
73 const std::string& name);
74
75 const std::string& id() const { return id_; }
76 const std::string& name() const { return name_; }
77
78 private:
79 static const char kKey[];
80
81 std::string id_;
82 std::string name_;
83
84 DISALLOW_COPY_AND_ASSIGN(DownloadedByExtension);
85};
86
Torne (Richard Coles)58218062012-11-14 11:43:16 +000087class DownloadsDownloadFunction : public AsyncExtensionFunction {
88 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000089 DECLARE_EXTENSION_FUNCTION("downloads.download", DOWNLOADS_DOWNLOAD)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000090 DownloadsDownloadFunction();
91 virtual bool RunImpl() OVERRIDE;
92
93 protected:
94 virtual ~DownloadsDownloadFunction();
95
96 private:
Ben Murdoch558790d2013-07-30 15:19:42 +010097 void OnStarted(
98 const base::FilePath& creator_suggested_filename,
99 extensions::api::downloads::FilenameConflictAction
100 creator_conflict_action,
101 content::DownloadItem* item,
102 net::Error error);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103
104 DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction);
105};
106
107class DownloadsSearchFunction : public SyncExtensionFunction {
108 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000109 DECLARE_EXTENSION_FUNCTION("downloads.search", DOWNLOADS_SEARCH)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110 DownloadsSearchFunction();
111 virtual bool RunImpl() OVERRIDE;
112
113 protected:
114 virtual ~DownloadsSearchFunction();
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction);
118};
119
120class DownloadsPauseFunction : public SyncExtensionFunction {
121 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000122 DECLARE_EXTENSION_FUNCTION("downloads.pause", DOWNLOADS_PAUSE)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000123 DownloadsPauseFunction();
124 virtual bool RunImpl() OVERRIDE;
125
126 protected:
127 virtual ~DownloadsPauseFunction();
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction);
131};
132
133class DownloadsResumeFunction : public SyncExtensionFunction {
134 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000135 DECLARE_EXTENSION_FUNCTION("downloads.resume", DOWNLOADS_RESUME)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000136 DownloadsResumeFunction();
137 virtual bool RunImpl() OVERRIDE;
138
139 protected:
140 virtual ~DownloadsResumeFunction();
141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction);
144};
145
146class DownloadsCancelFunction : public SyncExtensionFunction {
147 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000148 DECLARE_EXTENSION_FUNCTION("downloads.cancel", DOWNLOADS_CANCEL)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000149 DownloadsCancelFunction();
150 virtual bool RunImpl() OVERRIDE;
151
152 protected:
153 virtual ~DownloadsCancelFunction();
154
155 private:
156 DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction);
157};
158
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000159class DownloadsEraseFunction : public SyncExtensionFunction {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000160 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000161 DECLARE_EXTENSION_FUNCTION("downloads.erase", DOWNLOADS_ERASE)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000162 DownloadsEraseFunction();
163 virtual bool RunImpl() OVERRIDE;
164
165 protected:
166 virtual ~DownloadsEraseFunction();
167
168 private:
169 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction);
170};
171
Ben Murdoch558790d2013-07-30 15:19:42 +0100172class DownloadsRemoveFileFunction : public AsyncExtensionFunction,
173 public content::DownloadItem::Observer {
174 public:
175 DECLARE_EXTENSION_FUNCTION("downloads.removeFile", DOWNLOADS_REMOVEFILE)
176 DownloadsRemoveFileFunction();
177 virtual bool RunImpl() OVERRIDE;
178
179 protected:
180 virtual ~DownloadsRemoveFileFunction();
181
182 private:
183 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE;
184 virtual void OnDownloadDestroyed(content::DownloadItem* item) OVERRIDE;
185
186 content::DownloadItem* item_;
187
188 DISALLOW_COPY_AND_ASSIGN(DownloadsRemoveFileFunction);
189};
190
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000191class DownloadsAcceptDangerFunction : public AsyncExtensionFunction {
192 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000193 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000194 DownloadsAcceptDangerFunction();
195 virtual bool RunImpl() OVERRIDE;
196
197 protected:
198 virtual ~DownloadsAcceptDangerFunction();
Ben Murdoch558790d2013-07-30 15:19:42 +0100199 void DangerPromptCallback(int download_id,
200 DownloadDangerPrompt::Action action);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000201
202 private:
203 DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction);
204};
205
206class DownloadsShowFunction : public AsyncExtensionFunction {
207 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000208 DECLARE_EXTENSION_FUNCTION("downloads.show", DOWNLOADS_SHOW)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000209 DownloadsShowFunction();
210 virtual bool RunImpl() OVERRIDE;
211
212 protected:
213 virtual ~DownloadsShowFunction();
214
215 private:
216 DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction);
217};
218
Ben Murdoch558790d2013-07-30 15:19:42 +0100219class DownloadsShowDefaultFolderFunction : public AsyncExtensionFunction {
220 public:
221 DECLARE_EXTENSION_FUNCTION(
222 "downloads.showDefaultFolder", DOWNLOADS_SHOWDEFAULTFOLDER)
223 DownloadsShowDefaultFolderFunction();
224 virtual bool RunImpl() OVERRIDE;
225
226 protected:
227 virtual ~DownloadsShowDefaultFolderFunction();
228
229 private:
230 DISALLOW_COPY_AND_ASSIGN(DownloadsShowDefaultFolderFunction);
231};
232
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000233class DownloadsOpenFunction : public SyncExtensionFunction {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000234 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000235 DECLARE_EXTENSION_FUNCTION("downloads.open", DOWNLOADS_OPEN)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000236 DownloadsOpenFunction();
237 virtual bool RunImpl() OVERRIDE;
238
239 protected:
240 virtual ~DownloadsOpenFunction();
241
242 private:
243 DISALLOW_COPY_AND_ASSIGN(DownloadsOpenFunction);
244};
245
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100246class DownloadsSetShelfEnabledFunction : public SyncExtensionFunction {
247 public:
248 DECLARE_EXTENSION_FUNCTION("downloads.setShelfEnabled",
249 DOWNLOADS_SETSHELFENABLED)
250 DownloadsSetShelfEnabledFunction();
251 virtual bool RunImpl() OVERRIDE;
252
253 protected:
254 virtual ~DownloadsSetShelfEnabledFunction();
255
256 private:
257 DISALLOW_COPY_AND_ASSIGN(DownloadsSetShelfEnabledFunction);
258};
259
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000260class DownloadsDragFunction : public AsyncExtensionFunction {
261 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000262 DECLARE_EXTENSION_FUNCTION("downloads.drag", DOWNLOADS_DRAG)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000263 DownloadsDragFunction();
264 virtual bool RunImpl() OVERRIDE;
265
266 protected:
267 virtual ~DownloadsDragFunction();
268
269 private:
270 DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction);
271};
272
273class DownloadsGetFileIconFunction : public AsyncExtensionFunction {
274 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000275 DECLARE_EXTENSION_FUNCTION("downloads.getFileIcon", DOWNLOADS_GETFILEICON)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000276 DownloadsGetFileIconFunction();
277 virtual bool RunImpl() OVERRIDE;
278 void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor);
279
280 protected:
281 virtual ~DownloadsGetFileIconFunction();
282
283 private:
284 void OnIconURLExtracted(const std::string& url);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000285 base::FilePath path_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000286 scoped_ptr<DownloadFileIconExtractor> icon_extractor_;
287 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction);
288};
289
290// Observes a single DownloadManager and many DownloadItems and dispatches
291// onCreated and onErased events.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000292class ExtensionDownloadsEventRouter : public extensions::EventRouter::Observer,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100293 public content::NotificationObserver,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000294 public AllDownloadItemNotifier::Observer {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000295 public:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100296 typedef base::Callback<void(
297 const base::FilePath& changed_filename,
298 DownloadPathReservationTracker::FilenameConflictAction)>
299 FilenameChangedCallback;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000300
301 // A downloads.onDeterminingFilename listener has returned. If the extension
302 // wishes to override the download's filename, then |filename| will be
303 // non-empty. |filename| will be interpreted as a relative path, appended to
304 // the default downloads directory. If the extension wishes to overwrite any
305 // existing files, then |overwrite| will be true. Returns true on success,
306 // false otherwise.
307 static bool DetermineFilename(
308 Profile* profile,
309 bool include_incognito,
310 const std::string& ext_id,
311 int download_id,
312 const base::FilePath& filename,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100313 extensions::api::downloads::FilenameConflictAction conflict_action,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000314 std::string* error);
315
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000316 explicit ExtensionDownloadsEventRouter(
317 Profile* profile, content::DownloadManager* manager);
318 virtual ~ExtensionDownloadsEventRouter();
319
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100320 void SetShelfEnabled(const extensions::Extension* extension, bool enabled);
321 bool IsShelfEnabled() const;
322
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000323 // Called by ChromeDownloadManagerDelegate during the filename determination
324 // process, allows extensions to change the item's target filename. If no
325 // extension wants to change the target filename, then |no_change| will be
326 // called and the filename determination process will continue as normal. If
327 // an extension wants to change the target filename, then |change| will be
328 // called with the new filename and a flag indicating whether the new file
329 // should overwrite any old files of the same name.
330 void OnDeterminingFilename(
331 content::DownloadItem* item,
332 const base::FilePath& suggested_path,
333 const base::Closure& no_change,
334 const FilenameChangedCallback& change);
335
336 // AllDownloadItemNotifier::Observer
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000337 virtual void OnDownloadCreated(
338 content::DownloadManager* manager,
339 content::DownloadItem* download_item) OVERRIDE;
340 virtual void OnDownloadUpdated(
341 content::DownloadManager* manager,
342 content::DownloadItem* download_item) OVERRIDE;
343 virtual void OnDownloadRemoved(
344 content::DownloadManager* manager,
345 content::DownloadItem* download_item) OVERRIDE;
346
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000347 // extensions::EventRouter::Observer
348 virtual void OnListenerRemoved(
349 const extensions::EventListenerInfo& details) OVERRIDE;
350
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000351 // Used for testing.
352 struct DownloadsNotificationSource {
353 std::string event_name;
354 Profile* profile;
355 };
356
357 private:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000358 void DispatchEvent(
359 const char* event_name,
360 bool include_incognito,
361 const extensions::Event::WillDispatchCallback& will_dispatch_callback,
362 base::Value* json_arg);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000363
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100364 // content::NotificationObserver
365 virtual void Observe(int type,
366 const content::NotificationSource& source,
367 const content::NotificationDetails& details) OVERRIDE;
368
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000369 Profile* profile_;
370 AllDownloadItemNotifier notifier_;
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100371 std::set<const extensions::Extension*> shelf_disabling_extensions_;
372 content::NotificationRegistrar registrar_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000373
374 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter);
375};
376
377#endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_