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