| OLD | NEW |
| 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/download/download_extension_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/bind_helpers.h" |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 19 #include "base/string16.h" | 20 #include "base/string16.h" |
| 20 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 21 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 #include "chrome/browser/browser_process.h" | 24 #include "chrome/browser/browser_process.h" |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 json->SetInteger(kTotalBytesKey, item->GetTotalBytes()); | 468 json->SetInteger(kTotalBytesKey, item->GetTotalBytes()); |
| 468 if (item->GetState() == DownloadItem::INTERRUPTED) | 469 if (item->GetState() == DownloadItem::INTERRUPTED) |
| 469 json->SetInteger(kErrorKey, static_cast<int>(item->GetLastReason())); | 470 json->SetInteger(kErrorKey, static_cast<int>(item->GetLastReason())); |
| 470 // TODO(benjhayden): Implement endTime and fileSize. | 471 // TODO(benjhayden): Implement endTime and fileSize. |
| 471 // json->SetInteger(kEndTimeKey, -1); | 472 // json->SetInteger(kEndTimeKey, -1); |
| 472 json->SetInteger(kFileSizeKey, item->GetTotalBytes()); | 473 json->SetInteger(kFileSizeKey, item->GetTotalBytes()); |
| 473 return json; | 474 return json; |
| 474 } | 475 } |
| 475 } // anonymous namespace | 476 } // anonymous namespace |
| 476 | 477 |
| 477 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 478 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) |
| 478 Profile* profile) | |
| 479 : profile_(profile), | 479 : profile_(profile), |
| 480 manager_( | 480 manager_(NULL) { |
| 481 profile ? | |
| 482 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : | |
| 483 NULL) { | |
| 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 485 DCHECK(profile_); | 482 DCHECK(profile_); |
| 486 DCHECK(manager_); | 483 // Register a callback with the DownloadService for this profile to be called |
| 484 // when it creates the DownloadManager, or now if the manager already exists. |
| 485 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( |
| 486 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); |
| 487 } |
| 488 |
| 489 // The only public methods on this class are ModelChanged() and |
| 490 // ManagerGoingDown(), and they are only called by DownloadManager, so |
| 491 // there's no way for any methods on this class to be called before |
| 492 // DownloadService calls Init() via the OnManagerCreated Callback above. |
| 493 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { |
| 494 DCHECK(manager_ == NULL); |
| 495 manager_ = manager; |
| 487 manager_->AddObserver(this); | 496 manager_->AddObserver(this); |
| 488 } | 497 } |
| 489 | 498 |
| 490 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 499 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
| 491 if (manager_ != NULL) | 500 if (manager_ != NULL) |
| 492 manager_->RemoveObserver(this); | 501 manager_->RemoveObserver(this); |
| 493 } | 502 } |
| 494 | 503 |
| 495 void ExtensionDownloadsEventRouter::ModelChanged() { | 504 void ExtensionDownloadsEventRouter::ModelChanged() { |
| 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 ListValue args; | 555 ListValue args; |
| 547 args.Append(arg); | 556 args.Append(arg); |
| 548 std::string json_args; | 557 std::string json_args; |
| 549 base::JSONWriter::Write(&args, false, &json_args); | 558 base::JSONWriter::Write(&args, false, &json_args); |
| 550 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 559 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 551 event_name, | 560 event_name, |
| 552 json_args, | 561 json_args, |
| 553 profile_, | 562 profile_, |
| 554 GURL()); | 563 GURL()); |
| 555 } | 564 } |
| OLD | NEW |