Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
| 7 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | #include "base/memory/singleton.h" |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "base/values.h" |
| 14 | #include "chrome/browser/download/all_download_item_notifier.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 15 | #include "chrome/browser/download/download_danger_prompt.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 16 | #include "chrome/browser/download/download_path_reservation_tracker.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "chrome/browser/extensions/event_router.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_function.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 19 | #include "chrome/common/extensions/api/downloads.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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.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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 37 | // Errors that can be returned through chrome.runtime.lastError.message. |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 38 | extern const char kEmptyFile[]; |
| 39 | extern const char kFileAlreadyDeleted[]; |
| 40 | extern const char kIconNotFound[]; |
| 41 | extern const char kInvalidDangerType[]; |
| 42 | extern const char kInvalidFilename[]; |
| 43 | extern const char kInvalidFilter[]; |
| 44 | extern const char kInvalidHeader[]; |
| 45 | extern const char kInvalidId[]; |
| 46 | extern const char kInvalidOrderBy[]; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 47 | extern const char kInvalidQueryLimit[]; |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 48 | extern const char kInvalidState[]; |
| 49 | extern const char kInvalidURL[]; |
| 50 | extern const char kInvisibleContext[]; |
| 51 | extern const char kNotComplete[]; |
| 52 | extern const char kNotDangerous[]; |
| 53 | extern const char kNotInProgress[]; |
| 54 | extern const char kNotResumable[]; |
| 55 | extern const char kOpenPermission[]; |
| 56 | extern const char kTooManyListeners[]; |
| 57 | extern const char kUnexpectedDeterminer[]; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 58 | |
| 59 | } // namespace download_extension_errors |
| 60 | |
| 61 | |
| 62 | class DownloadsDownloadFunction : public AsyncExtensionFunction { |
| 63 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 64 | DECLARE_EXTENSION_FUNCTION("downloads.download", DOWNLOADS_DOWNLOAD) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | DownloadsDownloadFunction(); |
| 66 | virtual bool RunImpl() OVERRIDE; |
| 67 | |
| 68 | protected: |
| 69 | virtual ~DownloadsDownloadFunction(); |
| 70 | |
| 71 | private: |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 72 | 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction); |
| 80 | }; |
| 81 | |
| 82 | class DownloadsSearchFunction : public SyncExtensionFunction { |
| 83 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 84 | DECLARE_EXTENSION_FUNCTION("downloads.search", DOWNLOADS_SEARCH) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | DownloadsSearchFunction(); |
| 86 | virtual bool RunImpl() OVERRIDE; |
| 87 | |
| 88 | protected: |
| 89 | virtual ~DownloadsSearchFunction(); |
| 90 | |
| 91 | private: |
| 92 | DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction); |
| 93 | }; |
| 94 | |
| 95 | class DownloadsPauseFunction : public SyncExtensionFunction { |
| 96 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 97 | DECLARE_EXTENSION_FUNCTION("downloads.pause", DOWNLOADS_PAUSE) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 98 | DownloadsPauseFunction(); |
| 99 | virtual bool RunImpl() OVERRIDE; |
| 100 | |
| 101 | protected: |
| 102 | virtual ~DownloadsPauseFunction(); |
| 103 | |
| 104 | private: |
| 105 | DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction); |
| 106 | }; |
| 107 | |
| 108 | class DownloadsResumeFunction : public SyncExtensionFunction { |
| 109 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 110 | DECLARE_EXTENSION_FUNCTION("downloads.resume", DOWNLOADS_RESUME) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 111 | DownloadsResumeFunction(); |
| 112 | virtual bool RunImpl() OVERRIDE; |
| 113 | |
| 114 | protected: |
| 115 | virtual ~DownloadsResumeFunction(); |
| 116 | |
| 117 | private: |
| 118 | DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction); |
| 119 | }; |
| 120 | |
| 121 | class DownloadsCancelFunction : public SyncExtensionFunction { |
| 122 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 123 | DECLARE_EXTENSION_FUNCTION("downloads.cancel", DOWNLOADS_CANCEL) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 124 | 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 134 | class DownloadsEraseFunction : public SyncExtensionFunction { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 135 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 136 | DECLARE_EXTENSION_FUNCTION("downloads.erase", DOWNLOADS_ERASE) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 137 | DownloadsEraseFunction(); |
| 138 | virtual bool RunImpl() OVERRIDE; |
| 139 | |
| 140 | protected: |
| 141 | virtual ~DownloadsEraseFunction(); |
| 142 | |
| 143 | private: |
| 144 | DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); |
| 145 | }; |
| 146 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 147 | class 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 166 | class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { |
| 167 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 168 | DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 169 | DownloadsAcceptDangerFunction(); |
| 170 | virtual bool RunImpl() OVERRIDE; |
| 171 | |
| 172 | protected: |
| 173 | virtual ~DownloadsAcceptDangerFunction(); |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 174 | void DangerPromptCallback(int download_id, |
| 175 | DownloadDangerPrompt::Action action); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction); |
| 179 | }; |
| 180 | |
| 181 | class DownloadsShowFunction : public AsyncExtensionFunction { |
| 182 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 183 | DECLARE_EXTENSION_FUNCTION("downloads.show", DOWNLOADS_SHOW) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 184 | DownloadsShowFunction(); |
| 185 | virtual bool RunImpl() OVERRIDE; |
| 186 | |
| 187 | protected: |
| 188 | virtual ~DownloadsShowFunction(); |
| 189 | |
| 190 | private: |
| 191 | DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction); |
| 192 | }; |
| 193 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame^] | 194 | class 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 208 | class DownloadsOpenFunction : public SyncExtensionFunction { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 209 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 210 | DECLARE_EXTENSION_FUNCTION("downloads.open", DOWNLOADS_OPEN) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 211 | DownloadsOpenFunction(); |
| 212 | virtual bool RunImpl() OVERRIDE; |
| 213 | |
| 214 | protected: |
| 215 | virtual ~DownloadsOpenFunction(); |
| 216 | |
| 217 | private: |
| 218 | DISALLOW_COPY_AND_ASSIGN(DownloadsOpenFunction); |
| 219 | }; |
| 220 | |
| 221 | class DownloadsDragFunction : public AsyncExtensionFunction { |
| 222 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 223 | DECLARE_EXTENSION_FUNCTION("downloads.drag", DOWNLOADS_DRAG) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 224 | DownloadsDragFunction(); |
| 225 | virtual bool RunImpl() OVERRIDE; |
| 226 | |
| 227 | protected: |
| 228 | virtual ~DownloadsDragFunction(); |
| 229 | |
| 230 | private: |
| 231 | DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction); |
| 232 | }; |
| 233 | |
| 234 | class DownloadsGetFileIconFunction : public AsyncExtensionFunction { |
| 235 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 236 | DECLARE_EXTENSION_FUNCTION("downloads.getFileIcon", DOWNLOADS_GETFILEICON) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 237 | 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 246 | base::FilePath path_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 247 | 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 253 | class ExtensionDownloadsEventRouter : public extensions::EventRouter::Observer, |
| 254 | public AllDownloadItemNotifier::Observer { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 255 | public: |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 256 | typedef base::Callback<void( |
| 257 | const base::FilePath& changed_filename, |
| 258 | DownloadPathReservationTracker::FilenameConflictAction)> |
| 259 | FilenameChangedCallback; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 260 | |
| 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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 273 | extensions::api::downloads::FilenameConflictAction conflict_action, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 274 | std::string* error); |
| 275 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 276 | explicit ExtensionDownloadsEventRouter( |
| 277 | Profile* profile, content::DownloadManager* manager); |
| 278 | virtual ~ExtensionDownloadsEventRouter(); |
| 279 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 280 | // 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 294 | 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 304 | // extensions::EventRouter::Observer |
| 305 | virtual void OnListenerRemoved( |
| 306 | const extensions::EventListenerInfo& details) OVERRIDE; |
| 307 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 308 | // Used for testing. |
| 309 | struct DownloadsNotificationSource { |
| 310 | std::string event_name; |
| 311 | Profile* profile; |
| 312 | }; |
| 313 | |
| 314 | private: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 315 | 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 320 | |
| 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_ |