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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7408ca152d5ae2d1d83abea551c4ecd1a13cd601 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/downloads/downloads_api.h |
@@ -0,0 +1,253 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
+#define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
+#pragma once |
+ |
+#include <map> |
+#include <set> |
+#include <string> |
+ |
+#include "base/file_path.h" |
+#include "base/memory/singleton.h" |
+#include "base/stl_util.h" |
+#include "base/string16.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/extension_function.h" |
+#include "content/public/browser/download_id.h" |
+#include "content/public/browser/download_item.h" |
+#include "content/public/browser/download_manager.h" |
+ |
+class DownloadFileIconExtractor; |
+class DownloadQuery; |
+ |
+namespace content { |
+class ResourceContext; |
+class ResourceDispatcherHost; |
+} |
+ |
+// Functions in the chrome.downloads namespace facilitate |
+// controlling downloads from extensions. See the full API doc at |
+// http://goo.gl/6hO1n |
+ |
+namespace download_extension_errors { |
+ |
+// Errors that can be returned through chrome.extension.lastError.message. |
+extern const char kGenericError[]; |
+extern const char kIconNotFoundError[]; |
+extern const char kInvalidDangerTypeError[]; |
+extern const char kInvalidFilterError[]; |
+extern const char kInvalidOperationError[]; |
+extern const char kInvalidOrderByError[]; |
+extern const char kInvalidQueryLimit[]; |
+extern const char kInvalidStateError[]; |
+extern const char kInvalidUrlError[]; |
+extern const char kNotImplementedError[]; |
+ |
+} // namespace download_extension_errors |
+ |
+class DownloadsDownloadFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.download"); |
+ DownloadsDownloadFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsDownloadFunction(); |
+ |
+ private: |
+ void OnStarted(content::DownloadId dl_id, net::Error error); |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction); |
+}; |
+ |
+class DownloadsSearchFunction : public SyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.search"); |
+ DownloadsSearchFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsSearchFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction); |
+}; |
+ |
+class DownloadsPauseFunction : public SyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.pause"); |
+ DownloadsPauseFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsPauseFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction); |
+}; |
+ |
+class DownloadsResumeFunction : public SyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.resume"); |
+ DownloadsResumeFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsResumeFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction); |
+}; |
+ |
+class DownloadsCancelFunction : public SyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.cancel"); |
+ DownloadsCancelFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsCancelFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction); |
+}; |
+ |
+class DownloadsEraseFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.erase"); |
+ DownloadsEraseFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsEraseFunction(); |
+ |
+ private: |
+ 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"); |
+ DownloadsAcceptDangerFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsAcceptDangerFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction); |
+}; |
+ |
+class DownloadsShowFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.show"); |
+ DownloadsShowFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsShowFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction); |
+}; |
+ |
+class DownloadsOpenFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.open"); |
+ DownloadsOpenFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsOpenFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsOpenFunction); |
+}; |
+ |
+class DownloadsDragFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.drag"); |
+ DownloadsDragFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ |
+ protected: |
+ virtual ~DownloadsDragFunction(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction); |
+}; |
+ |
+class DownloadsGetFileIconFunction : public AsyncExtensionFunction { |
+ public: |
+ DECLARE_EXTENSION_FUNCTION_NAME("downloads.getFileIcon"); |
+ DownloadsGetFileIconFunction(); |
+ virtual bool RunImpl() OVERRIDE; |
+ void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor); |
+ |
+ protected: |
+ virtual ~DownloadsGetFileIconFunction(); |
+ |
+ private: |
+ void OnIconURLExtracted(const std::string& url); |
+ |
+ scoped_ptr<DownloadFileIconExtractor> icon_extractor_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); |
+}; |
+ |
+// Observes a single DownloadManager and many DownloadItems and dispatches |
+// onCreated and onErased events. |
+class ExtensionDownloadsEventRouter : public content::DownloadManager::Observer, |
+ public content::DownloadItem::Observer { |
+ public: |
+ explicit ExtensionDownloadsEventRouter(Profile* profile); |
+ virtual ~ExtensionDownloadsEventRouter(); |
+ |
+ virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
+ virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; |
+ virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
+ virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE; |
+ |
+ private: |
+ struct OnChangedStat { |
+ OnChangedStat(); |
+ ~OnChangedStat(); |
+ int fires; |
+ int total; |
+ }; |
+ |
+ typedef std::map<int, content::DownloadItem*> ItemMap; |
+ typedef std::map<int, base::DictionaryValue*> ItemJsonMap; |
+ typedef std::map<int, OnChangedStat*> OnChangedStatMap; |
+ |
+ void Init(content::DownloadManager* manager); |
+ void DispatchEvent(const char* event_name, base::Value* json_arg); |
+ |
+ Profile* profile_; |
+ content::DownloadManager* manager_; |
+ ItemMap downloads_; |
+ ItemJsonMap item_jsons_; |
+ STLValueDeleter<ItemJsonMap> delete_item_jsons_; |
+ OnChangedStatMap on_changed_stats_; |
+ STLValueDeleter<OnChangedStatMap> delete_on_changed_stats_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); |
+}; |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |