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