| 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 <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/hash_tables.h" |
| 13 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 14 #include "base/string16.h" | 15 #include "base/string16.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/download/all_download_item_notifier.h" | 17 #include "chrome/browser/download/all_download_item_notifier.h" |
| 17 #include "chrome/browser/extensions/extension_function.h" | 18 #include "chrome/browser/extensions/extension_function.h" |
| 18 #include "content/public/browser/download_id.h" | 19 #include "content/public/browser/download_id.h" |
| 19 #include "content/public/browser/download_item.h" | 20 #include "content/public/browser/download_item.h" |
| 20 #include "content/public/browser/download_manager.h" | 21 #include "content/public/browser/download_manager.h" |
| 21 | 22 |
| 22 class DownloadFileIconExtractor; | 23 class DownloadFileIconExtractor; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 DownloadsEraseFunction(); | 122 DownloadsEraseFunction(); |
| 122 virtual bool RunImpl() OVERRIDE; | 123 virtual bool RunImpl() OVERRIDE; |
| 123 | 124 |
| 124 protected: | 125 protected: |
| 125 virtual ~DownloadsEraseFunction(); | 126 virtual ~DownloadsEraseFunction(); |
| 126 | 127 |
| 127 private: | 128 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); | 129 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 class DownloadsSetDestinationFunction : public AsyncExtensionFunction { | |
| 132 public: | |
| 133 DECLARE_EXTENSION_FUNCTION_NAME("downloads.setDestination"); | |
| 134 DownloadsSetDestinationFunction(); | |
| 135 virtual bool RunImpl() OVERRIDE; | |
| 136 | |
| 137 protected: | |
| 138 virtual ~DownloadsSetDestinationFunction(); | |
| 139 | |
| 140 private: | |
| 141 DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction); | |
| 142 }; | |
| 143 | |
| 144 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { | 132 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { |
| 145 public: | 133 public: |
| 146 DECLARE_EXTENSION_FUNCTION_NAME("downloads.acceptDanger"); | 134 DECLARE_EXTENSION_FUNCTION_NAME("downloads.acceptDanger"); |
| 147 DownloadsAcceptDangerFunction(); | 135 DownloadsAcceptDangerFunction(); |
| 148 virtual bool RunImpl() OVERRIDE; | 136 virtual bool RunImpl() OVERRIDE; |
| 149 | 137 |
| 150 protected: | 138 protected: |
| 151 virtual ~DownloadsAcceptDangerFunction(); | 139 virtual ~DownloadsAcceptDangerFunction(); |
| 152 | 140 |
| 153 private: | 141 private: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 193 |
| 206 private: | 194 private: |
| 207 void OnIconURLExtracted(const std::string& url); | 195 void OnIconURLExtracted(const std::string& url); |
| 208 FilePath path_; | 196 FilePath path_; |
| 209 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; | 197 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; |
| 210 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); | 198 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); |
| 211 }; | 199 }; |
| 212 | 200 |
| 213 // Observes a single DownloadManager and many DownloadItems and dispatches | 201 // Observes a single DownloadManager and many DownloadItems and dispatches |
| 214 // onCreated and onErased events. | 202 // onCreated and onErased events. |
| 215 class ExtensionDownloadsEventRouter | 203 class ExtensionDownloadsEventRouter : public content::NotificationObserver, |
| 216 : public AllDownloadItemNotifier::Observer { | 204 public AllDownloadItemNotifier::Observer { |
| 217 public: | 205 public: |
| 206 typedef base::Callback<void(const FilePath& changed_filename, |
| 207 bool overwrite)> FilenameChangedCallback; |
| 208 |
| 209 // An extension has added a listener for downloads.onDeterminingFilename. When |
| 210 // that event is dispatched, the filename determination process will be |
| 211 // blocked until all listeners return. Returns true on success, false |
| 212 // otherwise. |
| 213 static bool AddFilenameDeterminer( |
| 214 Profile* profile, |
| 215 bool include_incognito, |
| 216 const std::string& ext_id, |
| 217 const std::string& sub_event_id); |
| 218 |
| 219 // An extension has removed a listener for downloads.onDeterminingFilename. |
| 220 // The filename determination process should not wait for this listener, |
| 221 // because it won't be run. Returns true on success, false otherwise. |
| 222 static bool RemoveFilenameDeterminer( |
| 223 Profile* profile, |
| 224 bool include_incognito, |
| 225 const std::string& ext_id, |
| 226 const std::string& sub_event_id); |
| 227 |
| 228 // A downloads.onDeterminingFilename listener has returned. If the extension |
| 229 // wishes to override the download's filename, then |filename| will be |
| 230 // non-empty. |filename| will be interpreted as a relative path, appended to |
| 231 // the default downloads directory. If the extension wishes to overwrite any |
| 232 // existing files, then |overwrite| will be true. Returns true on success, |
| 233 // false otherwise. |
| 234 static bool DetermineFilename( |
| 235 Profile* profile, |
| 236 bool include_incognito, |
| 237 const std::string& ext_id, |
| 238 const std::string& sub_event_id, |
| 239 int download_id, |
| 240 const FilePath& filename, |
| 241 bool overwrite); |
| 242 |
| 218 explicit ExtensionDownloadsEventRouter( | 243 explicit ExtensionDownloadsEventRouter( |
| 219 Profile* profile, content::DownloadManager* manager); | 244 Profile* profile, content::DownloadManager* manager); |
| 220 virtual ~ExtensionDownloadsEventRouter(); | 245 virtual ~ExtensionDownloadsEventRouter(); |
| 221 | 246 |
| 247 // Called by ChromeDownloadManagerDelegate during the filename determination |
| 248 // process, allows extensions to change the item's target filename. If no |
| 249 // extension wants to change the target filename, then |no_change| will be |
| 250 // called and the filename determination process will continue as normal. If |
| 251 // an extension wants to change the target filename, then |change| will be |
| 252 // called with the new filename and a flag indicating whether the new file |
| 253 // should overwrite any old files of the same name. |
| 254 void OnDownloadFilenameDetermined( |
| 255 content::DownloadItem* item, |
| 256 const FilePath& suggested_path, |
| 257 const base::Closure& no_change, |
| 258 const FilenameChangedCallback& change); |
| 259 |
| 222 // AllDownloadItemNotifier::Observer interface | 260 // AllDownloadItemNotifier::Observer interface |
| 223 virtual void OnDownloadCreated( | 261 virtual void OnDownloadCreated( |
| 224 content::DownloadManager* manager, | 262 content::DownloadManager* manager, |
| 225 content::DownloadItem* download_item) OVERRIDE; | 263 content::DownloadItem* download_item) OVERRIDE; |
| 226 virtual void OnDownloadUpdated( | 264 virtual void OnDownloadUpdated( |
| 227 content::DownloadManager* manager, | 265 content::DownloadManager* manager, |
| 228 content::DownloadItem* download_item) OVERRIDE; | 266 content::DownloadItem* download_item) OVERRIDE; |
| 229 virtual void OnDownloadRemoved( | 267 virtual void OnDownloadRemoved( |
| 230 content::DownloadManager* manager, | 268 content::DownloadManager* manager, |
| 231 content::DownloadItem* download_item) OVERRIDE; | 269 content::DownloadItem* download_item) OVERRIDE; |
| 232 | 270 |
| 233 // Used for testing. | 271 // Used for testing. |
| 234 struct DownloadsNotificationSource { | 272 struct DownloadsNotificationSource { |
| 235 std::string event_name; | 273 std::string event_name; |
| 236 Profile* profile; | 274 Profile* profile; |
| 237 }; | 275 }; |
| 238 | 276 |
| 239 private: | 277 private: |
| 278 // first: extension id; second: sub event id from GetFilenameDeterminerId |
| 279 typedef std::pair<std::string, std::string> DeterminerId; |
| 280 |
| 281 typedef std::vector<DeterminerId> DeterminerVector; |
| 282 |
| 240 void DispatchEvent(const char* event_name, base::Value* json_arg); | 283 void DispatchEvent(const char* event_name, base::Value* json_arg); |
| 284 virtual void Observe( |
| 285 int type, |
| 286 const content::NotificationSource& source, |
| 287 const content::NotificationDetails& details) OVERRIDE; |
| 241 | 288 |
| 242 Profile* profile_; | 289 Profile* profile_; |
| 290 content::NotificationRegistrar registrar_; |
| 243 AllDownloadItemNotifier notifier_; | 291 AllDownloadItemNotifier notifier_; |
| 244 | 292 |
| 293 // AddFilenameDeterminer appends to this; RemoveFilenameDeterminer removes |
| 294 // from this. When onDeterminingFilename is dispatched for a DownloadItem, |
| 295 // that item's EDERD UserData is given a copy of this so that it can know when |
| 296 // all determiners have reported. The last determiner added has the highest |
| 297 // priority. |
| 298 DeterminerVector determiners_; |
| 299 |
| 245 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); | 300 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); |
| 246 }; | 301 }; |
| 247 | 302 |
| 248 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ | 303 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
| OLD | NEW |