OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <set> |
| 11 #include <string> |
| 12 |
| 13 #include "base/file_path.h" |
| 14 #include "base/memory/singleton.h" |
| 15 #include "base/stl_util.h" |
| 16 #include "base/string16.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/extensions/extension_function.h" |
| 19 #include "content/public/browser/download_id.h" |
| 20 #include "content/public/browser/download_item.h" |
| 21 #include "content/public/browser/download_manager.h" |
| 22 |
| 23 class DownloadFileIconExtractor; |
| 24 class DownloadQuery; |
| 25 |
| 26 namespace content { |
| 27 class ResourceContext; |
| 28 class ResourceDispatcherHost; |
| 29 } |
| 30 |
| 31 // Functions in the chrome.downloads namespace facilitate |
| 32 // controlling downloads from extensions. See the full API doc at |
| 33 // http://goo.gl/6hO1n |
| 34 |
| 35 namespace download_extension_errors { |
| 36 |
| 37 // Errors that can be returned through chrome.extension.lastError.message. |
| 38 extern const char kGenericError[]; |
| 39 extern const char kIconNotFoundError[]; |
| 40 extern const char kInvalidDangerTypeError[]; |
| 41 extern const char kInvalidFilterError[]; |
| 42 extern const char kInvalidOperationError[]; |
| 43 extern const char kInvalidOrderByError[]; |
| 44 extern const char kInvalidQueryLimit[]; |
| 45 extern const char kInvalidStateError[]; |
| 46 extern const char kInvalidUrlError[]; |
| 47 extern const char kNotImplementedError[]; |
| 48 |
| 49 } // namespace download_extension_errors |
| 50 |
| 51 class DownloadsDownloadFunction : public AsyncExtensionFunction { |
| 52 public: |
| 53 DECLARE_EXTENSION_FUNCTION_NAME("downloads.download"); |
| 54 DownloadsDownloadFunction(); |
| 55 virtual bool RunImpl() OVERRIDE; |
| 56 |
| 57 protected: |
| 58 virtual ~DownloadsDownloadFunction(); |
| 59 |
| 60 private: |
| 61 void OnStarted(content::DownloadId dl_id, net::Error error); |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction); |
| 64 }; |
| 65 |
| 66 class DownloadsSearchFunction : public SyncExtensionFunction { |
| 67 public: |
| 68 DECLARE_EXTENSION_FUNCTION_NAME("downloads.search"); |
| 69 DownloadsSearchFunction(); |
| 70 virtual bool RunImpl() OVERRIDE; |
| 71 |
| 72 protected: |
| 73 virtual ~DownloadsSearchFunction(); |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction); |
| 77 }; |
| 78 |
| 79 class DownloadsPauseFunction : public SyncExtensionFunction { |
| 80 public: |
| 81 DECLARE_EXTENSION_FUNCTION_NAME("downloads.pause"); |
| 82 DownloadsPauseFunction(); |
| 83 virtual bool RunImpl() OVERRIDE; |
| 84 |
| 85 protected: |
| 86 virtual ~DownloadsPauseFunction(); |
| 87 |
| 88 private: |
| 89 DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction); |
| 90 }; |
| 91 |
| 92 class DownloadsResumeFunction : public SyncExtensionFunction { |
| 93 public: |
| 94 DECLARE_EXTENSION_FUNCTION_NAME("downloads.resume"); |
| 95 DownloadsResumeFunction(); |
| 96 virtual bool RunImpl() OVERRIDE; |
| 97 |
| 98 protected: |
| 99 virtual ~DownloadsResumeFunction(); |
| 100 |
| 101 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction); |
| 103 }; |
| 104 |
| 105 class DownloadsCancelFunction : public SyncExtensionFunction { |
| 106 public: |
| 107 DECLARE_EXTENSION_FUNCTION_NAME("downloads.cancel"); |
| 108 DownloadsCancelFunction(); |
| 109 virtual bool RunImpl() OVERRIDE; |
| 110 |
| 111 protected: |
| 112 virtual ~DownloadsCancelFunction(); |
| 113 |
| 114 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction); |
| 116 }; |
| 117 |
| 118 class DownloadsEraseFunction : public AsyncExtensionFunction { |
| 119 public: |
| 120 DECLARE_EXTENSION_FUNCTION_NAME("downloads.erase"); |
| 121 DownloadsEraseFunction(); |
| 122 virtual bool RunImpl() OVERRIDE; |
| 123 |
| 124 protected: |
| 125 virtual ~DownloadsEraseFunction(); |
| 126 |
| 127 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction); |
| 129 }; |
| 130 |
| 131 class DownloadsSetDestinationFunction : public AsyncExtensionFunction { |
| 132 public: |
| 133 DECLARE_EXTENSION_FUNCTION_NAME("downloads.setDestination"); |
| 134 DownloadsSetDestinationFunction(); |
| 135 virtual bool RunImpl() OVERRIDE; |
| 136 |
| 137 protected: |
| 138 virtual ~DownloadsSetDestinationFunction(); |
| 139 |
| 140 private: |
| 141 DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction); |
| 142 }; |
| 143 |
| 144 class DownloadsAcceptDangerFunction : public AsyncExtensionFunction { |
| 145 public: |
| 146 DECLARE_EXTENSION_FUNCTION_NAME("downloads.acceptDanger"); |
| 147 DownloadsAcceptDangerFunction(); |
| 148 virtual bool RunImpl() OVERRIDE; |
| 149 |
| 150 protected: |
| 151 virtual ~DownloadsAcceptDangerFunction(); |
| 152 |
| 153 private: |
| 154 DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction); |
| 155 }; |
| 156 |
| 157 class DownloadsShowFunction : public AsyncExtensionFunction { |
| 158 public: |
| 159 DECLARE_EXTENSION_FUNCTION_NAME("downloads.show"); |
| 160 DownloadsShowFunction(); |
| 161 virtual bool RunImpl() OVERRIDE; |
| 162 |
| 163 protected: |
| 164 virtual ~DownloadsShowFunction(); |
| 165 |
| 166 private: |
| 167 DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction); |
| 168 }; |
| 169 |
| 170 class DownloadsOpenFunction : public AsyncExtensionFunction { |
| 171 public: |
| 172 DECLARE_EXTENSION_FUNCTION_NAME("downloads.open"); |
| 173 DownloadsOpenFunction(); |
| 174 virtual bool RunImpl() OVERRIDE; |
| 175 |
| 176 protected: |
| 177 virtual ~DownloadsOpenFunction(); |
| 178 |
| 179 private: |
| 180 DISALLOW_COPY_AND_ASSIGN(DownloadsOpenFunction); |
| 181 }; |
| 182 |
| 183 class DownloadsDragFunction : public AsyncExtensionFunction { |
| 184 public: |
| 185 DECLARE_EXTENSION_FUNCTION_NAME("downloads.drag"); |
| 186 DownloadsDragFunction(); |
| 187 virtual bool RunImpl() OVERRIDE; |
| 188 |
| 189 protected: |
| 190 virtual ~DownloadsDragFunction(); |
| 191 |
| 192 private: |
| 193 DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction); |
| 194 }; |
| 195 |
| 196 class DownloadsGetFileIconFunction : public AsyncExtensionFunction { |
| 197 public: |
| 198 DECLARE_EXTENSION_FUNCTION_NAME("downloads.getFileIcon"); |
| 199 DownloadsGetFileIconFunction(); |
| 200 virtual bool RunImpl() OVERRIDE; |
| 201 void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor); |
| 202 |
| 203 protected: |
| 204 virtual ~DownloadsGetFileIconFunction(); |
| 205 |
| 206 private: |
| 207 void OnIconURLExtracted(const std::string& url); |
| 208 |
| 209 scoped_ptr<DownloadFileIconExtractor> icon_extractor_; |
| 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(DownloadsGetFileIconFunction); |
| 212 }; |
| 213 |
| 214 // Observes a single DownloadManager and many DownloadItems and dispatches |
| 215 // onCreated and onErased events. |
| 216 class ExtensionDownloadsEventRouter : public content::DownloadManager::Observer, |
| 217 public content::DownloadItem::Observer { |
| 218 public: |
| 219 explicit ExtensionDownloadsEventRouter(Profile* profile); |
| 220 virtual ~ExtensionDownloadsEventRouter(); |
| 221 |
| 222 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
| 223 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; |
| 224 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 225 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE; |
| 226 |
| 227 private: |
| 228 struct OnChangedStat { |
| 229 OnChangedStat(); |
| 230 ~OnChangedStat(); |
| 231 int fires; |
| 232 int total; |
| 233 }; |
| 234 |
| 235 typedef std::map<int, content::DownloadItem*> ItemMap; |
| 236 typedef std::map<int, base::DictionaryValue*> ItemJsonMap; |
| 237 typedef std::map<int, OnChangedStat*> OnChangedStatMap; |
| 238 |
| 239 void Init(content::DownloadManager* manager); |
| 240 void DispatchEvent(const char* event_name, base::Value* json_arg); |
| 241 |
| 242 Profile* profile_; |
| 243 content::DownloadManager* manager_; |
| 244 ItemMap downloads_; |
| 245 ItemJsonMap item_jsons_; |
| 246 STLValueDeleter<ItemJsonMap> delete_item_jsons_; |
| 247 OnChangedStatMap on_changed_stats_; |
| 248 STLValueDeleter<OnChangedStatMap> delete_on_changed_stats_; |
| 249 |
| 250 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloadsEventRouter); |
| 251 }; |
| 252 |
| 253 #endif // CHROME_BROWSER_EXTENSIONS_API_DOWNLOADS_DOWNLOADS_API_H_ |
OLD | NEW |