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

Unified 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: @r177662 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/downloads/downloads_api.h
diff --git a/chrome/browser/extensions/api/downloads/downloads_api.h b/chrome/browser/extensions/api/downloads/downloads_api.h
index 0fc6a63e7d779ac789f5467f5bf4e06505e5870c..45fd0dda5946722213a0f63568b7d193e24c8380 100644
--- a/chrome/browser/extensions/api/downloads/downloads_api.h
+++ b/chrome/browser/extensions/api/downloads/downloads_api.h
@@ -8,6 +8,7 @@
#include <string>
#include "base/file_path.h"
+#include "base/hash_tables.h"
#include "base/memory/singleton.h"
#include "base/string16.h"
#include "base/values.h"
@@ -126,19 +127,6 @@ class DownloadsEraseFunction : public SyncExtensionFunction {
DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction);
};
-class DownloadsSetDestinationFunction : public AsyncExtensionFunction {
- public:
- DECLARE_EXTENSION_FUNCTION_NAME("downloads.setDestination");
- DownloadsSetDestinationFunction();
- virtual bool RunImpl() OVERRIDE;
-
- protected:
- virtual ~DownloadsSetDestinationFunction();
-
- private:
- DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction);
-};
-
class DownloadsAcceptDangerFunction : public AsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME("downloads.acceptDanger");
@@ -210,13 +198,61 @@ class DownloadsGetFileIconFunction : public AsyncExtensionFunction {
// Observes a single DownloadManager and many DownloadItems and dispatches
// onCreated and onErased events.
-class ExtensionDownloadsEventRouter
- : public AllDownloadItemNotifier::Observer {
+class ExtensionDownloadsEventRouter : public content::NotificationObserver,
+ public AllDownloadItemNotifier::Observer {
public:
+ typedef base::Callback<void(const FilePath& changed_filename,
+ bool overwrite)> FilenameChangedCallback;
+
+ // An extension has added a listener for downloads.onDeterminingFilename. When
+ // that event is dispatched, the filename determination process will be
+ // blocked until all listeners return. Returns true on success, false
+ // otherwise.
+ static bool AddFilenameDeterminer(
+ Profile* profile,
+ const std::string& ext_id,
+ const std::string& sub_event_id);
+
+ // An extension has removed a listener for downloads.onDeterminingFilename.
+ // The filename determination process should not wait for this listener,
+ // because it won't be run. Returns true on success, false otherwise.
+ static bool RemoveFilenameDeterminer(
+ Profile* profile,
+ const std::string& ext_id,
+ const std::string& sub_event_id);
+
+ // A downloads.onDeterminingFilename listener has returned. If the extension
+ // wishes to override the download's filename, then |filename| will be
+ // non-empty. |filename| will be interpreted as a relative path, appended to
+ // the default downloads directory. If the extension wishes to overwrite any
+ // existing files, then |overwrite| will be true. Returns true on success,
+ // false otherwise.
+ static bool DetermineFilename(
+ Profile* profile,
+ bool include_incognito,
+ const std::string& ext_id,
+ const std::string& sub_event_id,
+ int download_id,
+ const FilePath& filename,
+ bool overwrite);
+
explicit ExtensionDownloadsEventRouter(
Profile* profile, content::DownloadManager* manager);
virtual ~ExtensionDownloadsEventRouter();
+ // Called by ChromeDownloadManagerDelegate during the filename determination
+ // process, allows extensions to change the item's target filename. If no
+ // extension wants to change the target filename, then |no_change| will be
+ // called and the filename determination process will continue as normal. If
+ // an extension wants to change the target filename, then |change| will be
+ // called with the new filename and a flag indicating whether the new file
+ // should overwrite any old files of the same name.
+ void OnDownloadFilenameDetermined(
+ content::DownloadItem* item,
+ const FilePath& suggested_path,
+ const base::Closure& no_change,
+ const FilenameChangedCallback& change);
+
// AllDownloadItemNotifier::Observer interface
virtual void OnDownloadCreated(
content::DownloadManager* manager,
@@ -235,11 +271,46 @@ class ExtensionDownloadsEventRouter
};
private:
- void DispatchEvent(const char* event_name, base::Value* json_arg);
+ // first: extension id; second: sub event id from GetFilenameDeterminerId
+ struct DeterminerId {
+ DeterminerId(
+ const std::string& ext_id,
+ const std::string& sub_id);
+ ~DeterminerId();
+
+ std::string extension_id;
+ std::string sub_event_id;
+ base::Time installed;
+ };
+
+ typedef std::vector<DeterminerId> DeterminerVector;
+
+ static void GetInstallTime(
+ const std::string& ext_id,
+ ExtensionInfoMap* info_map,
+ base::Time* install_time);
+
+ void DispatchEvent(
+ const char* event_name,
+ bool include_incognito,
+ base::Value* json_arg);
+
+ virtual void Observe(
+ int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) OVERRIDE;
Profile* profile_;
+ content::NotificationRegistrar registrar_;
AllDownloadItemNotifier notifier_;
+ // AddFilenameDeterminer appends to this; RemoveFilenameDeterminer removes
+ // from this. When onDeterminingFilename is dispatched for a DownloadItem,
+ // that item's EDERD UserData is given a copy of this so that it can know when
+ // all determiners have reported. The last determiner added has the highest
+ // priority.
+ DeterminerVector determiners_;
+
DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter);
};

Powered by Google App Engine
This is Rietveld 408576698