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

Side by Side Diff: chrome/browser/download/download_extension_api.cc

Issue 8451019: Move download extension api implementation to download/ dir. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa review Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_downloads_api.h" 5 #include "chrome/browser/download/download_extension_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cctype> 8 #include <cctype>
9 #include <iterator> 9 #include <iterator>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/json/json_writer.h" 15 #include "base/json/json_writer.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/metrics/histogram.h" 17 #include "base/metrics/histogram.h"
18 #include "base/stl_util.h" 18 #include "base/stl_util.h"
19 #include "base/string16.h" 19 #include "base/string16.h"
20 #include "base/string_util.h" 20 #include "base/string_util.h"
21 #include "base/stringprintf.h" 21 #include "base/stringprintf.h"
22 #include "base/values.h" 22 #include "base/values.h"
23 #include "chrome/browser/browser_process.h" 23 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/download/download_service.h" 24 #include "chrome/browser/download/download_service.h"
25 #include "chrome/browser/download/download_service_factory.h" 25 #include "chrome/browser/download/download_service_factory.h"
26 #include "chrome/browser/download/download_util.h" 26 #include "chrome/browser/download/download_util.h"
27 #include "chrome/browser/extensions/extension_downloads_api_constants.h"
28 #include "chrome/browser/extensions/extension_event_names.h" 27 #include "chrome/browser/extensions/extension_event_names.h"
29 #include "chrome/browser/extensions/extension_event_router.h" 28 #include "chrome/browser/extensions/extension_event_router.h"
30 #include "chrome/browser/icon_loader.h" 29 #include "chrome/browser/icon_loader.h"
31 #include "chrome/browser/icon_manager.h" 30 #include "chrome/browser/icon_manager.h"
32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" 31 #include "chrome/browser/renderer_host/chrome_render_message_filter.h"
33 #include "chrome/browser/ui/browser_list.h" 32 #include "chrome/browser/ui/browser_list.h"
34 #include "content/browser/download/download_file_manager.h" 33 #include "content/browser/download/download_file_manager.h"
35 #include "content/browser/download/download_item.h" 34 #include "content/browser/download/download_item.h"
36 #include "content/browser/download/download_types.h" 35 #include "content/browser/download/download_types.h"
37 #include "content/browser/download/interrupt_reasons.h" 36 #include "content/browser/download/interrupt_reasons.h"
38 #include "content/browser/renderer_host/render_process_host.h" 37 #include "content/browser/renderer_host/render_process_host.h"
39 #include "content/browser/renderer_host/render_view_host.h" 38 #include "content/browser/renderer_host/render_view_host.h"
40 #include "content/browser/renderer_host/resource_dispatcher_host.h" 39 #include "content/browser/renderer_host/resource_dispatcher_host.h"
41 #include "net/http/http_util.h" 40 #include "net/http/http_util.h"
42 #include "net/url_request/url_request.h" 41 #include "net/url_request/url_request.h"
43 42
44 using content::BrowserThread; 43 using content::BrowserThread;
45 44
46 namespace constants = extension_downloads_api_constants; 45 namespace {
46
47 // Error messages
48 const char kNotImplemented[] = "NotImplemented";
49 const char kGenericError[] = "I'm afraid I can't do that.";
50 const char kInvalidURL[] = "Invalid URL";
51
52 // Parameter keys
53 const char kBodyKey[] = "body";
54 const char kBytesReceivedKey[] = "bytesReceived";
55 const char kDangerAcceptedKey[] = "dangerAccepted";
56 const char kDangerFile[] = "file";
57 const char kDangerKey[] = "danger";
58 const char kDangerSafe[] = "safe";
59 const char kDangerUrl[] = "url";
60 const char kEndTimeKey[] = "endTime";
61 const char kErrorKey[] = "error";
62 const char kFileSizeKey[] = "fileSize";
63 const char kFilenameKey[] = "filename";
64 const char kHeaderNameKey[] = "name";
65 const char kHeaderValueKey[] = "value";
66 const char kHeadersKey[] = "headers";
67 const char kIdKey[] = "id";
68 const char kMethodKey[] = "method";
69 const char kMimeKey[] = "mime";
70 const char kPausedKey[] = "paused";
71 const char kSaveAsKey[] = "saveAs";
72 const char kStartTimeKey[] = "startTime";
73 const char kStateComplete[] = "complete";
74 const char kStateInProgress[] = "in_progress";
75 const char kStateInterrupted[] = "interrupted";
76 const char kStateKey[] = "state";
77 const char kTotalBytesKey[] = "totalBytes";
78 const char kUrlKey[] = "url";
79
80 const char* DangerString(DownloadItem::DangerType danger) {
81 switch (danger) {
82 case DownloadItem::NOT_DANGEROUS: return kDangerSafe;
83 case DownloadItem::DANGEROUS_FILE: return kDangerFile;
84 case DownloadItem::DANGEROUS_URL: return kDangerUrl;
85 default:
86 NOTREACHED();
87 return "";
88 }
89 }
90
91 const char* StateString(DownloadItem::DownloadState state) {
92 switch (state) {
93 case DownloadItem::IN_PROGRESS: return kStateInProgress;
94 case DownloadItem::COMPLETE: return kStateComplete;
95 case DownloadItem::INTERRUPTED: // fall through
96 case DownloadItem::CANCELLED: return kStateInterrupted;
97 case DownloadItem::REMOVING: // fall through
98 default:
99 NOTREACHED();
100 return "";
101 }
102 }
103
104 } // namespace
47 105
48 bool DownloadsFunctionInterface::RunImplImpl( 106 bool DownloadsFunctionInterface::RunImplImpl(
49 DownloadsFunctionInterface* pimpl) { 107 DownloadsFunctionInterface* pimpl) {
50 CHECK(pimpl); 108 CHECK(pimpl);
51 if (!pimpl->ParseArgs()) return false; 109 if (!pimpl->ParseArgs()) return false;
52 UMA_HISTOGRAM_ENUMERATION( 110 UMA_HISTOGRAM_ENUMERATION(
53 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); 111 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST);
54 pimpl->RunInternal(); 112 pimpl->RunInternal();
55 return true; 113 return true;
56 } 114 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 render_view_host_routing_id(0) { 161 render_view_host_routing_id(0) {
104 } 162 }
105 163
106 DownloadsDownloadFunction::IOData::~IOData() {} 164 DownloadsDownloadFunction::IOData::~IOData() {}
107 165
108 bool DownloadsDownloadFunction::ParseArgs() { 166 bool DownloadsDownloadFunction::ParseArgs() {
109 base::DictionaryValue* options = NULL; 167 base::DictionaryValue* options = NULL;
110 std::string url; 168 std::string url;
111 iodata_.reset(new IOData()); 169 iodata_.reset(new IOData());
112 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); 170 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options));
113 EXTENSION_FUNCTION_VALIDATE(options->GetString(constants::kUrlKey, &url)); 171 EXTENSION_FUNCTION_VALIDATE(options->GetString(kUrlKey, &url));
114 iodata_->url = GURL(url); 172 iodata_->url = GURL(url);
115 if (!iodata_->url.is_valid()) { 173 if (!iodata_->url.is_valid()) {
116 error_ = constants::kInvalidURL; 174 error_ = kInvalidURL;
117 return false; 175 return false;
118 } 176 }
119 if (options->HasKey(constants::kFilenameKey)) 177 if (options->HasKey(kFilenameKey))
120 EXTENSION_FUNCTION_VALIDATE(options->GetString( 178 EXTENSION_FUNCTION_VALIDATE(options->GetString(
121 constants::kFilenameKey, &iodata_->filename)); 179 kFilenameKey, &iodata_->filename));
122 // TODO(benjhayden): More robust validation of filename. 180 // TODO(benjhayden): More robust validation of filename.
123 if (((iodata_->filename[0] == L'.') && (iodata_->filename[1] == L'.')) || 181 if (((iodata_->filename[0] == L'.') && (iodata_->filename[1] == L'.')) ||
124 (iodata_->filename[0] == L'/')) { 182 (iodata_->filename[0] == L'/')) {
125 error_ = constants::kGenericError; 183 error_ = kGenericError;
126 return false; 184 return false;
127 } 185 }
128 if (options->HasKey(constants::kSaveAsKey)) 186 if (options->HasKey(kSaveAsKey))
129 EXTENSION_FUNCTION_VALIDATE(options->GetBoolean( 187 EXTENSION_FUNCTION_VALIDATE(options->GetBoolean(
130 constants::kSaveAsKey, &iodata_->save_as)); 188 kSaveAsKey, &iodata_->save_as));
131 if (options->HasKey(constants::kMethodKey)) 189 if (options->HasKey(kMethodKey))
132 EXTENSION_FUNCTION_VALIDATE(options->GetString( 190 EXTENSION_FUNCTION_VALIDATE(options->GetString(
133 constants::kMethodKey, &iodata_->method)); 191 kMethodKey, &iodata_->method));
134 // It's ok to use a pointer to extra_headers without DeepCopy()ing because 192 // It's ok to use a pointer to extra_headers without DeepCopy()ing because
135 // |args_| (which owns *extra_headers) is guaranteed to live as long as 193 // |args_| (which owns *extra_headers) is guaranteed to live as long as
136 // |this|. 194 // |this|.
137 if (options->HasKey(constants::kHeadersKey)) 195 if (options->HasKey(kHeadersKey))
138 EXTENSION_FUNCTION_VALIDATE(options->GetList( 196 EXTENSION_FUNCTION_VALIDATE(options->GetList(
139 constants::kHeadersKey, &iodata_->extra_headers)); 197 kHeadersKey, &iodata_->extra_headers));
140 if (options->HasKey(constants::kBodyKey)) 198 if (options->HasKey(kBodyKey))
141 EXTENSION_FUNCTION_VALIDATE(options->GetString( 199 EXTENSION_FUNCTION_VALIDATE(options->GetString(
142 constants::kBodyKey, &iodata_->post_body)); 200 kBodyKey, &iodata_->post_body));
143 if (iodata_->extra_headers != NULL) { 201 if (iodata_->extra_headers != NULL) {
144 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { 202 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) {
145 base::DictionaryValue* header = NULL; 203 base::DictionaryValue* header = NULL;
146 std::string name, value; 204 std::string name, value;
147 EXTENSION_FUNCTION_VALIDATE(iodata_->extra_headers->GetDictionary( 205 EXTENSION_FUNCTION_VALIDATE(iodata_->extra_headers->GetDictionary(
148 index, &header)); 206 index, &header));
149 EXTENSION_FUNCTION_VALIDATE(header->GetString( 207 EXTENSION_FUNCTION_VALIDATE(header->GetString(
150 constants::kHeaderNameKey, &name)); 208 kHeaderNameKey, &name));
151 EXTENSION_FUNCTION_VALIDATE(header->GetString( 209 EXTENSION_FUNCTION_VALIDATE(header->GetString(
152 constants::kHeaderValueKey, &value)); 210 kHeaderValueKey, &value));
153 if (!net::HttpUtil::IsSafeHeader(name)) { 211 if (!net::HttpUtil::IsSafeHeader(name)) {
154 error_ = constants::kGenericError; 212 error_ = kGenericError;
155 return false; 213 return false;
156 } 214 }
157 } 215 }
158 } 216 }
159 iodata_->rdh = g_browser_process->resource_dispatcher_host(); 217 iodata_->rdh = g_browser_process->resource_dispatcher_host();
160 iodata_->resource_context = &profile()->GetResourceContext(); 218 iodata_->resource_context = &profile()->GetResourceContext();
161 iodata_->render_process_host_id = render_view_host()->process()->id(); 219 iodata_->render_process_host_id = render_view_host()->process()->id();
162 iodata_->render_view_host_routing_id = render_view_host()->routing_id(); 220 iodata_->render_view_host_routing_id = render_view_host()->routing_id();
163 return true; 221 return true;
164 } 222 }
165 223
166 void DownloadsDownloadFunction::RunInternal() { 224 void DownloadsDownloadFunction::RunInternal() {
167 VLOG(1) << __FUNCTION__ << " " << iodata_->url.spec(); 225 VLOG(1) << __FUNCTION__ << " " << iodata_->url.spec();
168 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 226 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
169 &DownloadsDownloadFunction::BeginDownloadOnIOThread, this))) { 227 &DownloadsDownloadFunction::BeginDownloadOnIOThread, this))) {
170 error_ = constants::kGenericError; 228 error_ = kGenericError;
171 SendResponse(error_.empty()); 229 SendResponse(error_.empty());
172 } 230 }
173 } 231 }
174 232
175 void DownloadsDownloadFunction::BeginDownloadOnIOThread() { 233 void DownloadsDownloadFunction::BeginDownloadOnIOThread() {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
177 DVLOG(1) << __FUNCTION__ << " " << iodata_->url.spec(); 235 DVLOG(1) << __FUNCTION__ << " " << iodata_->url.spec();
178 DownloadSaveInfo save_info; 236 DownloadSaveInfo save_info;
179 // TODO(benjhayden) Ensure that this filename is interpreted as a path 237 // TODO(benjhayden) Ensure that this filename is interpreted as a path
180 // relative to the default downloads directory without allowing '..'. 238 // relative to the default downloads directory without allowing '..'.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 285
228 DownloadsSearchFunction::DownloadsSearchFunction() 286 DownloadsSearchFunction::DownloadsSearchFunction()
229 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) { 287 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) {
230 } 288 }
231 289
232 DownloadsSearchFunction::~DownloadsSearchFunction() {} 290 DownloadsSearchFunction::~DownloadsSearchFunction() {}
233 291
234 bool DownloadsSearchFunction::ParseArgs() { 292 bool DownloadsSearchFunction::ParseArgs() {
235 base::DictionaryValue* query_json = NULL; 293 base::DictionaryValue* query_json = NULL;
236 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); 294 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json));
237 error_ = constants::kNotImplemented; 295 error_ = kNotImplemented;
238 return false; 296 return false;
239 } 297 }
240 298
241 void DownloadsSearchFunction::RunInternal() { 299 void DownloadsSearchFunction::RunInternal() {
242 NOTIMPLEMENTED(); 300 NOTIMPLEMENTED();
243 } 301 }
244 302
245 DownloadsPauseFunction::DownloadsPauseFunction() 303 DownloadsPauseFunction::DownloadsPauseFunction()
246 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) { 304 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) {
247 } 305 }
248 306
249 DownloadsPauseFunction::~DownloadsPauseFunction() {} 307 DownloadsPauseFunction::~DownloadsPauseFunction() {}
250 308
251 bool DownloadsPauseFunction::ParseArgs() { 309 bool DownloadsPauseFunction::ParseArgs() {
252 int dl_id = 0; 310 int dl_id = 0;
253 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 311 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
254 VLOG(1) << __FUNCTION__ << " " << dl_id; 312 VLOG(1) << __FUNCTION__ << " " << dl_id;
255 error_ = constants::kNotImplemented; 313 error_ = kNotImplemented;
256 return false; 314 return false;
257 } 315 }
258 316
259 void DownloadsPauseFunction::RunInternal() { 317 void DownloadsPauseFunction::RunInternal() {
260 NOTIMPLEMENTED(); 318 NOTIMPLEMENTED();
261 } 319 }
262 320
263 DownloadsResumeFunction::DownloadsResumeFunction() 321 DownloadsResumeFunction::DownloadsResumeFunction()
264 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME) { 322 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME) {
265 } 323 }
266 324
267 DownloadsResumeFunction::~DownloadsResumeFunction() {} 325 DownloadsResumeFunction::~DownloadsResumeFunction() {}
268 326
269 bool DownloadsResumeFunction::ParseArgs() { 327 bool DownloadsResumeFunction::ParseArgs() {
270 int dl_id = 0; 328 int dl_id = 0;
271 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 329 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
272 VLOG(1) << __FUNCTION__ << " " << dl_id; 330 VLOG(1) << __FUNCTION__ << " " << dl_id;
273 error_ = constants::kNotImplemented; 331 error_ = kNotImplemented;
274 return false; 332 return false;
275 } 333 }
276 334
277 void DownloadsResumeFunction::RunInternal() { 335 void DownloadsResumeFunction::RunInternal() {
278 NOTIMPLEMENTED(); 336 NOTIMPLEMENTED();
279 } 337 }
280 338
281 DownloadsCancelFunction::DownloadsCancelFunction() 339 DownloadsCancelFunction::DownloadsCancelFunction()
282 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL) { 340 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL) {
283 } 341 }
284 342
285 DownloadsCancelFunction::~DownloadsCancelFunction() {} 343 DownloadsCancelFunction::~DownloadsCancelFunction() {}
286 344
287 bool DownloadsCancelFunction::ParseArgs() { 345 bool DownloadsCancelFunction::ParseArgs() {
288 int dl_id = 0; 346 int dl_id = 0;
289 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 347 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
290 VLOG(1) << __FUNCTION__ << " " << dl_id; 348 VLOG(1) << __FUNCTION__ << " " << dl_id;
291 error_ = constants::kNotImplemented; 349 error_ = kNotImplemented;
292 return false; 350 return false;
293 } 351 }
294 352
295 void DownloadsCancelFunction::RunInternal() { 353 void DownloadsCancelFunction::RunInternal() {
296 NOTIMPLEMENTED(); 354 NOTIMPLEMENTED();
297 } 355 }
298 356
299 DownloadsEraseFunction::DownloadsEraseFunction() 357 DownloadsEraseFunction::DownloadsEraseFunction()
300 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { 358 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) {
301 } 359 }
302 360
303 DownloadsEraseFunction::~DownloadsEraseFunction() {} 361 DownloadsEraseFunction::~DownloadsEraseFunction() {}
304 362
305 bool DownloadsEraseFunction::ParseArgs() { 363 bool DownloadsEraseFunction::ParseArgs() {
306 base::DictionaryValue* query_json = NULL; 364 base::DictionaryValue* query_json = NULL;
307 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); 365 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json));
308 error_ = constants::kNotImplemented; 366 error_ = kNotImplemented;
309 return false; 367 return false;
310 } 368 }
311 369
312 void DownloadsEraseFunction::RunInternal() { 370 void DownloadsEraseFunction::RunInternal() {
313 NOTIMPLEMENTED(); 371 NOTIMPLEMENTED();
314 } 372 }
315 373
316 DownloadsSetDestinationFunction::DownloadsSetDestinationFunction() 374 DownloadsSetDestinationFunction::DownloadsSetDestinationFunction()
317 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SET_DESTINATION) { 375 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SET_DESTINATION) {
318 } 376 }
319 377
320 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {} 378 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {}
321 379
322 bool DownloadsSetDestinationFunction::ParseArgs() { 380 bool DownloadsSetDestinationFunction::ParseArgs() {
323 int dl_id = 0; 381 int dl_id = 0;
324 std::string path; 382 std::string path;
325 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 383 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
326 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &path)); 384 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &path));
327 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << &path; 385 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << &path;
328 error_ = constants::kNotImplemented; 386 error_ = kNotImplemented;
329 return false; 387 return false;
330 } 388 }
331 389
332 void DownloadsSetDestinationFunction::RunInternal() { 390 void DownloadsSetDestinationFunction::RunInternal() {
333 NOTIMPLEMENTED(); 391 NOTIMPLEMENTED();
334 } 392 }
335 393
336 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction() 394 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction()
337 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ACCEPT_DANGER) { 395 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ACCEPT_DANGER) {
338 } 396 }
339 397
340 DownloadsAcceptDangerFunction::~DownloadsAcceptDangerFunction() {} 398 DownloadsAcceptDangerFunction::~DownloadsAcceptDangerFunction() {}
341 399
342 bool DownloadsAcceptDangerFunction::ParseArgs() { 400 bool DownloadsAcceptDangerFunction::ParseArgs() {
343 int dl_id = 0; 401 int dl_id = 0;
344 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 402 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
345 VLOG(1) << __FUNCTION__ << " " << dl_id; 403 VLOG(1) << __FUNCTION__ << " " << dl_id;
346 error_ = constants::kNotImplemented; 404 error_ = kNotImplemented;
347 return false; 405 return false;
348 } 406 }
349 407
350 void DownloadsAcceptDangerFunction::RunInternal() { 408 void DownloadsAcceptDangerFunction::RunInternal() {
351 NOTIMPLEMENTED(); 409 NOTIMPLEMENTED();
352 } 410 }
353 411
354 DownloadsShowFunction::DownloadsShowFunction() 412 DownloadsShowFunction::DownloadsShowFunction()
355 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SHOW) { 413 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SHOW) {
356 } 414 }
357 415
358 DownloadsShowFunction::~DownloadsShowFunction() {} 416 DownloadsShowFunction::~DownloadsShowFunction() {}
359 417
360 bool DownloadsShowFunction::ParseArgs() { 418 bool DownloadsShowFunction::ParseArgs() {
361 int dl_id = 0; 419 int dl_id = 0;
362 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 420 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
363 VLOG(1) << __FUNCTION__ << " " << dl_id; 421 VLOG(1) << __FUNCTION__ << " " << dl_id;
364 error_ = constants::kNotImplemented; 422 error_ = kNotImplemented;
365 return false; 423 return false;
366 } 424 }
367 425
368 void DownloadsShowFunction::RunInternal() { 426 void DownloadsShowFunction::RunInternal() {
369 NOTIMPLEMENTED(); 427 NOTIMPLEMENTED();
370 } 428 }
371 429
372 DownloadsDragFunction::DownloadsDragFunction() 430 DownloadsDragFunction::DownloadsDragFunction()
373 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DRAG) { 431 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DRAG) {
374 } 432 }
375 433
376 DownloadsDragFunction::~DownloadsDragFunction() {} 434 DownloadsDragFunction::~DownloadsDragFunction() {}
377 435
378 bool DownloadsDragFunction::ParseArgs() { 436 bool DownloadsDragFunction::ParseArgs() {
379 int dl_id = 0; 437 int dl_id = 0;
380 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); 438 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id));
381 VLOG(1) << __FUNCTION__ << " " << dl_id; 439 VLOG(1) << __FUNCTION__ << " " << dl_id;
382 error_ = constants::kNotImplemented; 440 error_ = kNotImplemented;
383 return false; 441 return false;
384 } 442 }
385 443
386 void DownloadsDragFunction::RunInternal() { 444 void DownloadsDragFunction::RunInternal() {
387 NOTIMPLEMENTED(); 445 NOTIMPLEMENTED();
388 } 446 }
389 447
390 namespace { 448 namespace {
391 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { 449 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) {
392 base::DictionaryValue* json = new base::DictionaryValue(); 450 base::DictionaryValue* json = new base::DictionaryValue();
393 json->SetInteger(constants::kIdKey, item->id()); 451 json->SetInteger(kIdKey, item->id());
394 json->SetString(constants::kUrlKey, item->original_url().spec()); 452 json->SetString(kUrlKey, item->original_url().spec());
395 json->SetString(constants::kFilenameKey, 453 json->SetString(kFilenameKey, item->full_path().LossyDisplayName());
396 item->full_path().LossyDisplayName()); 454 json->SetString(kDangerKey, DangerString(item->GetDangerType()));
397 json->SetString(constants::kDangerKey, 455 json->SetBoolean(kDangerAcceptedKey,
398 constants::DangerString(item->GetDangerType()));
399 json->SetBoolean(constants::kDangerAcceptedKey,
400 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); 456 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED);
401 json->SetString(constants::kStateKey, 457 json->SetString(kStateKey, StateString(item->state()));
402 constants::StateString(item->state())); 458 json->SetBoolean(kPausedKey, item->is_paused());
403 json->SetBoolean(constants::kPausedKey, item->is_paused()); 459 json->SetString(kMimeKey, item->mime_type());
404 json->SetString(constants::kMimeKey, item->mime_type()); 460 json->SetInteger(kStartTimeKey,
405 json->SetInteger(constants::kStartTimeKey,
406 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); 461 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds());
407 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); 462 json->SetInteger(kBytesReceivedKey, item->received_bytes());
408 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); 463 json->SetInteger(kTotalBytesKey, item->total_bytes());
409 if (item->state() == DownloadItem::INTERRUPTED) 464 if (item->state() == DownloadItem::INTERRUPTED)
410 json->SetInteger(constants::kErrorKey, 465 json->SetInteger(kErrorKey, static_cast<int>(item->last_reason()));
411 static_cast<int>(item->last_reason()));
412 // TODO(benjhayden): Implement endTime and fileSize. 466 // TODO(benjhayden): Implement endTime and fileSize.
413 // json->SetInteger(constants::kEndTimeKey, -1); 467 // json->SetInteger(kEndTimeKey, -1);
414 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); 468 json->SetInteger(kFileSizeKey, item->total_bytes());
415 return json; 469 return json;
416 } 470 }
417 } // anonymous namespace 471 } // anonymous namespace
418 472
419 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( 473 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(
420 Profile* profile) 474 Profile* profile)
421 : profile_(profile), 475 : profile_(profile),
422 manager_( 476 manager_(
423 profile ? 477 profile ?
424 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : 478 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() :
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 ListValue args; 542 ListValue args;
489 args.Append(arg); 543 args.Append(arg);
490 std::string json_args; 544 std::string json_args;
491 base::JSONWriter::Write(&args, false, &json_args); 545 base::JSONWriter::Write(&args, false, &json_args);
492 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 546 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
493 event_name, 547 event_name,
494 json_args, 548 json_args,
495 profile_, 549 profile_,
496 GURL()); 550 GURL());
497 } 551 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_extension_api.h ('k') | chrome/browser/download/download_extension_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698