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_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/string16.h" | 12 #include "base/string16.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/download/all_download_item_notifier.h" | 14 #include "chrome/browser/download/all_download_item_notifier.h" |
| 15 #include "chrome/browser/extensions/event_router.h" |
15 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
16 #include "content/public/browser/download_id.h" | 17 #include "content/public/browser/download_id.h" |
17 #include "content/public/browser/download_item.h" | 18 #include "content/public/browser/download_item.h" |
18 #include "content/public/browser/download_manager.h" | 19 #include "content/public/browser/download_manager.h" |
19 | 20 |
20 class DownloadFileIconExtractor; | 21 class DownloadFileIconExtractor; |
21 class DownloadQuery; | 22 class DownloadQuery; |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 class ResourceContext; | 25 class ResourceContext; |
25 class ResourceDispatcherHost; | 26 class ResourceDispatcherHost; |
26 } | 27 } |
27 | 28 |
28 // Functions in the chrome.downloads namespace facilitate | 29 // Functions in the chrome.downloads namespace facilitate |
29 // controlling downloads from extensions. See the full API doc at | 30 // controlling downloads from extensions. See the full API doc at |
30 // http://goo.gl/6hO1n | 31 // http://goo.gl/6hO1n |
31 | 32 |
32 namespace download_extension_errors { | 33 namespace download_extension_errors { |
33 | 34 |
34 // Errors that can be returned through chrome.runtime.lastError.message. | 35 // Errors that can be returned through chrome.runtime.lastError.message. |
35 extern const char kGenericError[]; | 36 extern const char kGenericError[]; |
36 extern const char kIconNotFoundError[]; | 37 extern const char kIconNotFoundError[]; |
37 extern const char kInvalidDangerTypeError[]; | 38 extern const char kInvalidDangerTypeError[]; |
| 39 extern const char kInvalidFilenameError[]; |
38 extern const char kInvalidFilterError[]; | 40 extern const char kInvalidFilterError[]; |
39 extern const char kInvalidOperationError[]; | 41 extern const char kInvalidOperationError[]; |
40 extern const char kInvalidOrderByError[]; | 42 extern const char kInvalidOrderByError[]; |
41 extern const char kInvalidQueryLimit[]; | 43 extern const char kInvalidQueryLimit[]; |
42 extern const char kInvalidStateError[]; | 44 extern const char kInvalidStateError[]; |
43 extern const char kInvalidURLError[]; | 45 extern const char kInvalidURLError[]; |
44 extern const char kNotImplementedError[]; | 46 extern const char kNotImplementedError[]; |
| 47 extern const char kTooManyListenersError[]; |
45 | 48 |
46 } // namespace download_extension_errors | 49 } // namespace download_extension_errors |
47 | 50 |
48 | 51 |
49 class DownloadsDownloadFunction : public AsyncExtensionFunction { | 52 class DownloadsDownloadFunction : public AsyncExtensionFunction { |
50 public: | 53 public: |
51 DECLARE_EXTENSION_FUNCTION("downloads.download", DOWNLOADS_DOWNLOAD) | 54 DECLARE_EXTENSION_FUNCTION("downloads.download", DOWNLOADS_DOWNLOAD) |
52 DownloadsDownloadFunction(); | 55 DownloadsDownloadFunction(); |
53 virtual bool RunImpl() OVERRIDE; | 56 virtual bool RunImpl() OVERRIDE; |
54 | 57 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 DownloadsEraseFunction(); | 122 DownloadsEraseFunction(); |
120 virtual bool RunImpl() OVERRIDE; | 123 virtual bool RunImpl() OVERRIDE; |
121 | 124 |
122 protected: | 125 protected: |
123 virtual ~DownloadsEraseFunction(); | 126 virtual ~DownloadsEraseFunction(); |
124 | 127 |
125 private: | 128 private: |
126 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); | 129 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); |
127 }; | 130 }; |
128 | 131 |
129 class DownloadsSetDestinationFunction : public AsyncExtensionFunction { | |
130 public: | |
131 DECLARE_EXTENSION_FUNCTION("downloads.setDestination", | |
132 DOWNLOADS_SETDESTINATION) | |
133 DownloadsSetDestinationFunction(); | |
134 virtual bool RunImpl() OVERRIDE; | |
135 | |
136 protected: | |
137 virtual ~DownloadsSetDestinationFunction(); | |
138 | |
139 private: | |
140 DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction); | |
141 }; | |
142 | |
143 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { | 132 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { |
144 public: | 133 public: |
145 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER) | 134 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER) |
146 DownloadsAcceptDangerFunction(); | 135 DownloadsAcceptDangerFunction(); |
147 virtual bool RunImpl() OVERRIDE; | 136 virtual bool RunImpl() OVERRIDE; |
148 | 137 |
149 protected: | 138 protected: |
150 virtual ~DownloadsAcceptDangerFunction(); | 139 virtual ~DownloadsAcceptDangerFunction(); |
151 void DangerPromptCallback(bool accept, int download_id); | 140 void DangerPromptCallback(bool accept, int download_id); |
152 | 141 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 194 |
206 private: | 195 private: |
207 void OnIconURLExtracted(const std::string& url); | 196 void OnIconURLExtracted(const std::string& url); |
208 base::FilePath path_; | 197 base::FilePath path_; |
209 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; | 198 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; |
210 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); | 199 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); |
211 }; | 200 }; |
212 | 201 |
213 // Observes a single DownloadManager and many DownloadItems and dispatches | 202 // Observes a single DownloadManager and many DownloadItems and dispatches |
214 // onCreated and onErased events. | 203 // onCreated and onErased events. |
215 class ExtensionDownloadsEventRouter | 204 class ExtensionDownloadsEventRouter : public extensions::EventRouter::Observer, |
216 : public AllDownloadItemNotifier::Observer { | 205 public AllDownloadItemNotifier::Observer { |
217 public: | 206 public: |
| 207 typedef base::Callback<void(const base::FilePath& changed_filename, |
| 208 bool overwrite)> FilenameChangedCallback; |
| 209 |
| 210 // A downloads.onDeterminingFilename listener has returned. If the extension |
| 211 // wishes to override the download's filename, then |filename| will be |
| 212 // non-empty. |filename| will be interpreted as a relative path, appended to |
| 213 // the default downloads directory. If the extension wishes to overwrite any |
| 214 // existing files, then |overwrite| will be true. Returns true on success, |
| 215 // false otherwise. |
| 216 static bool DetermineFilename( |
| 217 Profile* profile, |
| 218 bool include_incognito, |
| 219 const std::string& ext_id, |
| 220 int download_id, |
| 221 const base::FilePath& filename, |
| 222 bool overwrite, |
| 223 std::string* error); |
| 224 |
218 explicit ExtensionDownloadsEventRouter( | 225 explicit ExtensionDownloadsEventRouter( |
219 Profile* profile, content::DownloadManager* manager); | 226 Profile* profile, content::DownloadManager* manager); |
220 virtual ~ExtensionDownloadsEventRouter(); | 227 virtual ~ExtensionDownloadsEventRouter(); |
221 | 228 |
222 // AllDownloadItemNotifier::Observer interface | 229 // Called by ChromeDownloadManagerDelegate during the filename determination |
| 230 // process, allows extensions to change the item's target filename. If no |
| 231 // extension wants to change the target filename, then |no_change| will be |
| 232 // called and the filename determination process will continue as normal. If |
| 233 // an extension wants to change the target filename, then |change| will be |
| 234 // called with the new filename and a flag indicating whether the new file |
| 235 // should overwrite any old files of the same name. |
| 236 void OnDeterminingFilename( |
| 237 content::DownloadItem* item, |
| 238 const base::FilePath& suggested_path, |
| 239 const base::Closure& no_change, |
| 240 const FilenameChangedCallback& change); |
| 241 |
| 242 // AllDownloadItemNotifier::Observer |
223 virtual void OnDownloadCreated( | 243 virtual void OnDownloadCreated( |
224 content::DownloadManager* manager, | 244 content::DownloadManager* manager, |
225 content::DownloadItem* download_item) OVERRIDE; | 245 content::DownloadItem* download_item) OVERRIDE; |
226 virtual void OnDownloadUpdated( | 246 virtual void OnDownloadUpdated( |
227 content::DownloadManager* manager, | 247 content::DownloadManager* manager, |
228 content::DownloadItem* download_item) OVERRIDE; | 248 content::DownloadItem* download_item) OVERRIDE; |
229 virtual void OnDownloadRemoved( | 249 virtual void OnDownloadRemoved( |
230 content::DownloadManager* manager, | 250 content::DownloadManager* manager, |
231 content::DownloadItem* download_item) OVERRIDE; | 251 content::DownloadItem* download_item) OVERRIDE; |
232 | 252 |
| 253 // extensions::EventRouter::Observer |
| 254 virtual void OnListenerRemoved( |
| 255 const extensions::EventListenerInfo& details) OVERRIDE; |
| 256 |
233 // Used for testing. | 257 // Used for testing. |
234 struct DownloadsNotificationSource { | 258 struct DownloadsNotificationSource { |
235 std::string event_name; | 259 std::string event_name; |
236 Profile* profile; | 260 Profile* profile; |
237 }; | 261 }; |
238 | 262 |
239 private: | 263 private: |
240 void DispatchEvent(const char* event_name, base::Value* json_arg); | 264 void DispatchEvent( |
| 265 const char* event_name, |
| 266 bool include_incognito, |
| 267 const extensions::Event::WillDispatchCallback& will_dispatch_callback, |
| 268 base::Value* json_arg); |
241 | 269 |
242 Profile* profile_; | 270 Profile* profile_; |
243 AllDownloadItemNotifier notifier_; | 271 AllDownloadItemNotifier notifier_; |
244 | 272 |
245 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); | 273 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); |
246 }; | 274 }; |
247 | 275 |
248 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ | 276 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
OLD | NEW |