summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_extension_api.h
blob: 9cf95f4f7dfdef600c59268c3480b20cfbe4638a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// 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_DOWNLOAD_DOWNLOAD_EXTENSION_API_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_EXTENSION_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.experimental.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 DownloadsFunctionInterface {
 public:
  enum DownloadsFunctionName {
    DOWNLOADS_FUNCTION_DOWNLOAD = 0,
    DOWNLOADS_FUNCTION_SEARCH = 1,
    DOWNLOADS_FUNCTION_PAUSE = 2,
    DOWNLOADS_FUNCTION_RESUME = 3,
    DOWNLOADS_FUNCTION_CANCEL = 4,
    DOWNLOADS_FUNCTION_ERASE = 5,
    DOWNLOADS_FUNCTION_SET_DESTINATION = 6,
    DOWNLOADS_FUNCTION_ACCEPT_DANGER = 7,
    DOWNLOADS_FUNCTION_SHOW = 8,
    DOWNLOADS_FUNCTION_DRAG = 9,
    DOWNLOADS_FUNCTION_GET_FILE_ICON = 10,
    // Insert new values here, not at the beginning.
    DOWNLOADS_FUNCTION_LAST
  };

 protected:
  // Return true if args_ is well-formed, otherwise set error_ and return false.
  virtual bool ParseArgs() = 0;

  // Implementation-specific logic. "Do the thing that you do."  Should return
  // true if the call succeeded and false otherwise.
  virtual bool RunInternal() = 0;

  // Which subclass is this.
  virtual DownloadsFunctionName function() const = 0;

  // Wrap ParseArgs(), RunInternal().
  static bool RunImplImpl(DownloadsFunctionInterface* pimpl);
};

class SyncDownloadsFunction : public SyncExtensionFunction,
                              public DownloadsFunctionInterface {
 protected:
  explicit SyncDownloadsFunction(DownloadsFunctionName function);
  virtual ~SyncDownloadsFunction();

  // ExtensionFunction:
  virtual bool RunImpl() OVERRIDE;

  // DownloadsFunctionInterface:
  virtual DownloadsFunctionName function() const OVERRIDE;

  content::DownloadItem* GetActiveItem(int download_id);

 private:
  DownloadsFunctionName function_;

  DISALLOW_COPY_AND_ASSIGN(SyncDownloadsFunction);
};

class AsyncDownloadsFunction : public AsyncExtensionFunction,
                               public DownloadsFunctionInterface {
 protected:
  explicit AsyncDownloadsFunction(DownloadsFunctionName function);
  virtual ~AsyncDownloadsFunction();

  // ExtensionFunction:
  virtual bool RunImpl() OVERRIDE;

  // DownloadsFunctionInterface:
  virtual DownloadsFunctionName function() const OVERRIDE;

  content::DownloadItem* GetActiveItem(int download_id);

 private:
  DownloadsFunctionName function_;

  DISALLOW_COPY_AND_ASSIGN(AsyncDownloadsFunction);
};

class DownloadsDownloadFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.download");

  DownloadsDownloadFunction();

 protected:
  virtual ~DownloadsDownloadFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  struct IOData {
   public:
    IOData();
    ~IOData();

    GURL url;
    string16 filename;
    bool save_as;
    base::ListValue* extra_headers;
    std::string method;
    std::string post_body;
    content::ResourceDispatcherHost* rdh;
    content::ResourceContext* resource_context;
    int render_process_host_id;
    int render_view_host_routing_id;
  };

  void BeginDownloadOnIOThread();
  void OnStarted(content::DownloadId dl_id, net::Error error);

  scoped_ptr<IOData> iodata_;

  DISALLOW_COPY_AND_ASSIGN(DownloadsDownloadFunction);
};

class DownloadsSearchFunction : public SyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.search");

  DownloadsSearchFunction();

 protected:
  virtual ~DownloadsSearchFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  bool ParseOrderBy(const base::Value& order_by_value);

  scoped_ptr<DownloadQuery> query_;
  int get_id_;
  bool has_get_id_;

  DISALLOW_COPY_AND_ASSIGN(DownloadsSearchFunction);
};

class DownloadsPauseFunction : public SyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.pause");

  DownloadsPauseFunction();

 protected:
  virtual ~DownloadsPauseFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  int download_id_;
  DISALLOW_COPY_AND_ASSIGN(DownloadsPauseFunction);
};

class DownloadsResumeFunction : public SyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.resume");

  DownloadsResumeFunction();

 protected:
  virtual ~DownloadsResumeFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  int download_id_;
  DISALLOW_COPY_AND_ASSIGN(DownloadsResumeFunction);
};

class DownloadsCancelFunction : public SyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.cancel");

  DownloadsCancelFunction();

 protected:
  virtual ~DownloadsCancelFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  int download_id_;
  DISALLOW_COPY_AND_ASSIGN(DownloadsCancelFunction);
};

class DownloadsEraseFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.erase");

  DownloadsEraseFunction();

 protected:
  virtual ~DownloadsEraseFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(DownloadsEraseFunction);
};

class DownloadsSetDestinationFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.setDestination");

  DownloadsSetDestinationFunction();

 protected:
  virtual ~DownloadsSetDestinationFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(DownloadsSetDestinationFunction);
};

class DownloadsAcceptDangerFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.acceptDanger");

  DownloadsAcceptDangerFunction();

 protected:
  virtual ~DownloadsAcceptDangerFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(DownloadsAcceptDangerFunction);
};

class DownloadsShowFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.show");

  DownloadsShowFunction();

 protected:
  virtual ~DownloadsShowFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(DownloadsShowFunction);
};

class DownloadsDragFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.drag");

  DownloadsDragFunction();

 protected:
  virtual ~DownloadsDragFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(DownloadsDragFunction);
};

class DownloadsGetFileIconFunction : public AsyncDownloadsFunction {
 public:
  DECLARE_EXTENSION_FUNCTION_NAME("experimental.downloads.getFileIcon");

  DownloadsGetFileIconFunction();
  void SetIconExtractorForTesting(DownloadFileIconExtractor* extractor);

 protected:
  virtual ~DownloadsGetFileIconFunction();

  // DownloadsFunctionInterface:
  virtual bool ParseArgs() OVERRIDE;
  virtual bool RunInternal() OVERRIDE;

 private:
  void OnIconURLExtracted(const std::string& url);
  FilePath path_;
  int icon_size_;
  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_DOWNLOAD_DOWNLOAD_EXTENSION_API_H_