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