Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(628)

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api.h

Issue 11574006: Implement chrome.downloads.onDeterminingFilename() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r176665 Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 bool include_incognito,
214 const std::string& ext_id,
215 const std::string& sub_event_id);
216
217 // An extension has removed a listener for downloads.onDeterminingFilename.
218 // The filename determination process should not wait for this listener,
219 // because it won't be run. Returns true on success, false otherwise.
220 static bool RemoveFilenameDeterminer(
221 Profile* profile,
222 bool include_incognito,
223 const std::string& ext_id,
224 const std::string& 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 const std::string& sub_event_id,
237 int download_id,
238 const FilePath& filename,
239 bool overwrite);
240
216 explicit ExtensionDownloadsEventRouter( 241 explicit ExtensionDownloadsEventRouter(
217 Profile* profile, content::DownloadManager* manager); 242 Profile* profile, content::DownloadManager* manager);
218 virtual ~ExtensionDownloadsEventRouter(); 243 virtual ~ExtensionDownloadsEventRouter();
219 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 OnDownloadFilenameDetermined(
253 content::DownloadItem* item,
254 const FilePath& suggested_path,
255 const base::Closure& no_change,
256 const FilenameChangedCallback& change);
257
220 // AllDownloadItemNotifier::Observer interface 258 // AllDownloadItemNotifier::Observer interface
221 virtual void OnDownloadCreated( 259 virtual void OnDownloadCreated(
222 content::DownloadManager* manager, 260 content::DownloadManager* manager,
223 content::DownloadItem* download_item) OVERRIDE; 261 content::DownloadItem* download_item) OVERRIDE;
224 virtual void OnDownloadUpdated( 262 virtual void OnDownloadUpdated(
225 content::DownloadManager* manager, 263 content::DownloadManager* manager,
226 content::DownloadItem* download_item) OVERRIDE; 264 content::DownloadItem* download_item) OVERRIDE;
227 virtual void OnDownloadRemoved( 265 virtual void OnDownloadRemoved(
228 content::DownloadManager* manager, 266 content::DownloadManager* manager,
229 content::DownloadItem* download_item) OVERRIDE; 267 content::DownloadItem* download_item) OVERRIDE;
230 268
231 // Used for testing. 269 // Used for testing.
232 struct DownloadsNotificationSource { 270 struct DownloadsNotificationSource {
233 std::string event_name; 271 std::string event_name;
234 Profile* profile; 272 Profile* profile;
235 }; 273 };
236 274
237 private: 275 private:
276 // first: extension id; second: sub event id from GetFilenameDeterminerId
277 typedef std::pair<std::string, std::string> DeterminerId;
278
279 typedef std::vector<DeterminerId> DeterminerVector;
280
238 void DispatchEvent(const char* event_name, base::Value* json_arg); 281 void DispatchEvent(const char* event_name, base::Value* json_arg);
282 virtual void Observe(
283 int type,
284 const content::NotificationSource& source,
285 const content::NotificationDetails& details) OVERRIDE;
239 286
240 Profile* profile_; 287 Profile* profile_;
288 content::NotificationRegistrar registrar_;
241 AllDownloadItemNotifier notifier_; 289 AllDownloadItemNotifier notifier_;
242 290
291 // AddFilenameDeterminer appends to this; RemoveFilenameDeterminer removes
292 // from this. When onDeterminingFilename is dispatched for a DownloadItem,
293 // that item's EDERD UserData is given a copy of this so that it can know when
294 // all determiners have reported. The last determiner added has the highest
295 // priority.
296 DeterminerVector determiners_;
297
243 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); 298 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter);
244 }; 299 };
245 300
246 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ 301 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698