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/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/hash_tables.h" |
11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
12 #include "base/string16.h" | 13 #include "base/string16.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/download/all_download_item_notifier.h" | 15 #include "chrome/browser/download/all_download_item_notifier.h" |
| 16 #include "chrome/browser/extensions/event_router.h" |
15 #include "chrome/browser/extensions/extension_function.h" | 17 #include "chrome/browser/extensions/extension_function.h" |
16 #include "content/public/browser/download_id.h" | 18 #include "content/public/browser/download_id.h" |
17 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
18 #include "content/public/browser/download_manager.h" | 20 #include "content/public/browser/download_manager.h" |
19 | 21 |
20 class DownloadFileIconExtractor; | 22 class DownloadFileIconExtractor; |
21 class DownloadQuery; | 23 class DownloadQuery; |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 class ResourceContext; | 26 class ResourceContext; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 DownloadsEraseFunction(); | 121 DownloadsEraseFunction(); |
120 virtual bool RunImpl() OVERRIDE; | 122 virtual bool RunImpl() OVERRIDE; |
121 | 123 |
122 protected: | 124 protected: |
123 virtual ~DownloadsEraseFunction(); | 125 virtual ~DownloadsEraseFunction(); |
124 | 126 |
125 private: | 127 private: |
126 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); | 128 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); |
127 }; | 129 }; |
128 | 130 |
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 { | 131 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { |
144 public: | 132 public: |
145 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER) | 133 DECLARE_EXTENSION_FUNCTION("downloads.acceptDanger", DOWNLOADS_ACCEPTDANGER) |
146 DownloadsAcceptDangerFunction(); | 134 DownloadsAcceptDangerFunction(); |
147 virtual bool RunImpl() OVERRIDE; | 135 virtual bool RunImpl() OVERRIDE; |
148 | 136 |
149 protected: | 137 protected: |
150 virtual ~DownloadsAcceptDangerFunction(); | 138 virtual ~DownloadsAcceptDangerFunction(); |
151 void DangerPromptCallback(bool accept, int download_id); | 139 void DangerPromptCallback(bool accept, int download_id); |
152 | 140 |
(...skipping 52 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 base::FilePath path_; | 196 base::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 extensions::EventRouter::Observer, |
216 : public AllDownloadItemNotifier::Observer { | 204 public AllDownloadItemNotifier::Observer { |
217 public: | 205 public: |
| 206 typedef base::Callback<void(const base::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 int sub_event_id); |
| 218 |
| 219 // A downloads.onDeterminingFilename listener has returned. If the extension |
| 220 // wishes to override the download's filename, then |filename| will be |
| 221 // non-empty. |filename| will be interpreted as a relative path, appended to |
| 222 // the default downloads directory. If the extension wishes to overwrite any |
| 223 // existing files, then |overwrite| will be true. Returns true on success, |
| 224 // false otherwise. |
| 225 static bool DetermineFilename( |
| 226 Profile* profile, |
| 227 bool include_incognito, |
| 228 const std::string& ext_id, |
| 229 int sub_event_id, |
| 230 int download_id, |
| 231 const base::FilePath& filename, |
| 232 bool overwrite); |
| 233 |
218 explicit ExtensionDownloadsEventRouter( | 234 explicit ExtensionDownloadsEventRouter( |
219 Profile* profile, content::DownloadManager* manager); | 235 Profile* profile, content::DownloadManager* manager); |
220 virtual ~ExtensionDownloadsEventRouter(); | 236 virtual ~ExtensionDownloadsEventRouter(); |
221 | 237 |
222 // AllDownloadItemNotifier::Observer interface | 238 // Called by ChromeDownloadManagerDelegate during the filename determination |
| 239 // process, allows extensions to change the item's target filename. If no |
| 240 // extension wants to change the target filename, then |no_change| will be |
| 241 // called and the filename determination process will continue as normal. If |
| 242 // an extension wants to change the target filename, then |change| will be |
| 243 // called with the new filename and a flag indicating whether the new file |
| 244 // should overwrite any old files of the same name. |
| 245 void OnDeterminingFilename( |
| 246 content::DownloadItem* item, |
| 247 const base::FilePath& suggested_path, |
| 248 const base::Closure& no_change, |
| 249 const FilenameChangedCallback& change); |
| 250 |
| 251 // AllDownloadItemNotifier::Observer |
223 virtual void OnDownloadCreated( | 252 virtual void OnDownloadCreated( |
224 content::DownloadManager* manager, | 253 content::DownloadManager* manager, |
225 content::DownloadItem* download_item) OVERRIDE; | 254 content::DownloadItem* download_item) OVERRIDE; |
226 virtual void OnDownloadUpdated( | 255 virtual void OnDownloadUpdated( |
227 content::DownloadManager* manager, | 256 content::DownloadManager* manager, |
228 content::DownloadItem* download_item) OVERRIDE; | 257 content::DownloadItem* download_item) OVERRIDE; |
229 virtual void OnDownloadRemoved( | 258 virtual void OnDownloadRemoved( |
230 content::DownloadManager* manager, | 259 content::DownloadManager* manager, |
231 content::DownloadItem* download_item) OVERRIDE; | 260 content::DownloadItem* download_item) OVERRIDE; |
232 | 261 |
| 262 // extensions::EventRouter::Observer |
| 263 virtual void OnListenerAdded( |
| 264 const extensions::EventListenerInfo& details) OVERRIDE; |
| 265 virtual void OnListenerRemoved( |
| 266 const extensions::EventListenerInfo& details) OVERRIDE; |
| 267 |
233 // Used for testing. | 268 // Used for testing. |
234 struct DownloadsNotificationSource { | 269 struct DownloadsNotificationSource { |
235 std::string event_name; | 270 std::string event_name; |
236 Profile* profile; | 271 Profile* profile; |
237 }; | 272 }; |
238 | 273 |
239 private: | 274 private: |
240 void DispatchEvent(const char* event_name, base::Value* json_arg); | 275 struct DeterminerId { |
| 276 DeterminerId( |
| 277 const std::string& ext_id, |
| 278 int sub_id, |
| 279 bool incognito, |
| 280 const base::Time& install); |
| 281 ~DeterminerId(); |
| 282 |
| 283 std::string extension_id; |
| 284 int sub_event_id; |
| 285 bool include_incognito; |
| 286 base::Time installed; |
| 287 }; |
| 288 |
| 289 typedef std::vector<DeterminerId> DeterminerVector; |
| 290 |
| 291 void DispatchEvent( |
| 292 const char* event_name, |
| 293 bool include_incognito, |
| 294 base::Value* json_arg); |
241 | 295 |
242 Profile* profile_; | 296 Profile* profile_; |
243 AllDownloadItemNotifier notifier_; | 297 AllDownloadItemNotifier notifier_; |
244 | 298 |
| 299 // AddFilenameDeterminer appends to this; RemoveFilenameDeterminer removes |
| 300 // from this. When onDeterminingFilename is dispatched for a DownloadItem, |
| 301 // that item's EDERD UserData is given a copy of this so that it can know when |
| 302 // all determiners have reported. The last determiner added has the highest |
| 303 // priority. |
| 304 DeterminerVector determiners_; |
| 305 |
245 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); | 306 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); |
246 }; | 307 }; |
247 | 308 |
248 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ | 309 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
OLD | NEW |