diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-24 02:17:11 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-24 02:17:11 +0000 |
commit | 5f088d8d0f3e17d52c9812a904fd033281ff2e43 (patch) | |
tree | 0afea38ad9409038dd92579c341d5a432af50bdf | |
parent | 74fa5e9961083cb855c03ced620fc83e55174e45 (diff) | |
download | chromium_src-5f088d8d0f3e17d52c9812a904fd033281ff2e43.zip chromium_src-5f088d8d0f3e17d52c9812a904fd033281ff2e43.tar.gz chromium_src-5f088d8d0f3e17d52c9812a904fd033281ff2e43.tar.bz2 |
Use ImageBurnerClient
cros/burn_library is still there.
BUG=chromium-os:16553
TEST=chrome://imageburner works
Review URL: https://chromiumcodereview.appspot.com/9176016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118782 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/cros/burn_library.cc | 183 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/burn_library.h | 35 |
2 files changed, 87 insertions, 131 deletions
diff --git a/chrome/browser/chromeos/cros/burn_library.cc b/chrome/browser/chromeos/cros/burn_library.cc index c5e8332..7f2d8e8 100644 --- a/chrome/browser/chromeos/cros/burn_library.cc +++ b/chrome/browser/chromeos/cros/burn_library.cc @@ -1,14 +1,13 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. #include "chrome/browser/chromeos/cros/burn_library.h" -#include <cstring> - #include "base/bind.h" -#include "base/memory/linked_ptr.h" -#include "chrome/browser/chromeos/cros/cros_library.h" +#include "base/observer_list.h" +#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" +#include "chrome/browser/chromeos/dbus/image_burner_client.h" #include "chrome/browser/chromeos/disks/disk_mount_manager.h" #include "chrome/common/zip.h" #include "content/public/browser/browser_thread.h" @@ -17,8 +16,26 @@ using content::BrowserThread; namespace chromeos { -class BurnLibraryImpl : public BurnLibrary, - public base::SupportsWeakPtr<BurnLibraryImpl> { +namespace { + +// Unzips |source_zip_file| and calls |callback| with the filename of +// the unzipped image. +void UnzipImage( + const FilePath& source_zip_file, + const std::string& image_name, + base::Callback<void(const std::string& source_image_file)> callback) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); + + std::string source_image_file; + if (zip::Unzip(source_zip_file, source_zip_file.DirName())) { + source_image_file = + source_zip_file.DirName().Append(image_name).value(); + } + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, base::Bind(callback, source_image_file)); +} + +class BurnLibraryImpl : public BurnLibrary { public: BurnLibraryImpl(); virtual ~BurnLibraryImpl(); @@ -32,26 +49,26 @@ class BurnLibraryImpl : public BurnLibrary, const FilePath& target_device_path) OVERRIDE; virtual void CancelBurnImage() OVERRIDE; - // Called by BurnLibraryTaskProxy. - void UnzipImage(); - void OnImageUnzipped(); + void OnImageUnzipped(const std::string& source_image_file); private: void Init(); void BurnImage(); static void DevicesUnmountedCallback(void* object, bool success); void UpdateBurnStatus(const ImageBurnStatus& status, BurnEvent evt); - static void BurnStatusChangedHandler(void* object, - const BurnStatus& status, - BurnEventType evt); + void OnBurnFinished(const std::string& target_path, + bool success, + const std::string& error); + void OnBurnProgressUpdate(const std::string& target_path, + int64 num_bytes_burnt, + int64 total_size); + void OnBurnImageFail(); + + base::WeakPtrFactory<BurnLibraryImpl> weak_ptr_factory_; - private: ObserverList<BurnLibrary::Observer> observers_; - BurnStatusConnection burn_status_connection_; - FilePath source_zip_file_; std::string source_image_file_; - std::string source_image_name_; std::string target_file_path_; std::string target_device_path_; @@ -63,48 +80,24 @@ class BurnLibraryImpl : public BurnLibrary, DISALLOW_COPY_AND_ASSIGN(BurnLibraryImpl); }; -class BurnLibraryTaskProxy - : public base::RefCountedThreadSafe<BurnLibraryTaskProxy> { - public: - explicit BurnLibraryTaskProxy(const base::WeakPtr<BurnLibraryImpl>& library) - : library_(library) { - library_->DetachFromThread(); - } - - void UnzipImage() { - if (library_) - library_->UnzipImage(); - } - - void ImageUnzipped() { - if (library_) - library_->OnImageUnzipped(); - } - - private: - base::WeakPtr<BurnLibraryImpl> library_; - - friend class base::RefCountedThreadSafe<BurnLibraryTaskProxy>; - - DISALLOW_COPY_AND_ASSIGN(BurnLibraryTaskProxy); -}; - -BurnLibraryImpl::BurnLibraryImpl() : unzipping_(false), - cancelled_(false), - burning_(false), - block_burn_signals_(false) { +BurnLibraryImpl::BurnLibraryImpl() + : weak_ptr_factory_(this), + unzipping_(false), + cancelled_(false), + burning_(false), + block_burn_signals_(false) { } BurnLibraryImpl::~BurnLibraryImpl() { - if (burn_status_connection_) { - DisconnectBurnStatus(burn_status_connection_); - } + DBusThreadManager::Get()->GetImageBurnerClient()->ResetEventHandlers(); } void BurnLibraryImpl::Init() { - DCHECK(CrosLibrary::Get()->libcros_loaded()); - burn_status_connection_ = - chromeos::MonitorBurnStatus(&BurnStatusChangedHandler, this); + DBusThreadManager::Get()->GetImageBurnerClient()->SetEventHandlers( + base::Bind(&BurnLibraryImpl::OnBurnFinished, + weak_ptr_factory_.GetWeakPtr()), + base::Bind(&BurnLibraryImpl::OnBurnProgressUpdate, + weak_ptr_factory_.GetWeakPtr())); } void BurnLibraryImpl::AddObserver(Observer* observer) { @@ -137,9 +130,7 @@ void BurnLibraryImpl::DoBurn(const FilePath& source_path, return; } - source_zip_file_ = source_path; source_image_file_.clear(); - source_image_name_ = image_name; target_file_path_ = target_file_path.value(); target_device_path_ = target_device_path.value(); @@ -147,30 +138,19 @@ void BurnLibraryImpl::DoBurn(const FilePath& source_path, cancelled_ = false; UpdateBurnStatus(ImageBurnStatus(), UNZIP_STARTED); - scoped_refptr<BurnLibraryTaskProxy> task = - new BurnLibraryTaskProxy(AsWeakPtr()); - BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, - base::Bind(&BurnLibraryTaskProxy::UnzipImage, - task)); + BrowserThread::PostTask( + BrowserThread::FILE, + FROM_HERE, + base::Bind(UnzipImage, + source_path, + image_name, + base::Bind(&BurnLibraryImpl::OnImageUnzipped, + weak_ptr_factory_.GetWeakPtr()))); } -void BurnLibraryImpl::UnzipImage() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - - if (zip::Unzip(source_zip_file_, source_zip_file_.DirName())) { - source_image_file_ = - source_zip_file_.DirName().Append(source_image_name_).value(); - } - - scoped_refptr<BurnLibraryTaskProxy> task = - new BurnLibraryTaskProxy(AsWeakPtr()); - BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, - base::Bind(&BurnLibraryTaskProxy::ImageUnzipped, - task)); -} - -void BurnLibraryImpl::OnImageUnzipped() { +void BurnLibraryImpl::OnImageUnzipped(const std::string& source_image_file) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + source_image_file_ = source_image_file; bool success = !source_image_file_.empty(); UpdateBurnStatus(ImageBurnStatus(), (success ? UNZIP_COMPLETE : UNZIP_FAIL)); @@ -192,43 +172,38 @@ void BurnLibraryImpl::OnImageUnzipped() { this); } +void BurnLibraryImpl::OnBurnFinished(const std::string& target_path, + bool success, + const std::string& error) { + UpdateBurnStatus(ImageBurnStatus(0, 0), success ? BURN_SUCCESS : BURN_FAIL); +} + +void BurnLibraryImpl::OnBurnProgressUpdate(const std::string& target_path, + int64 amount_burnt, + int64 total_size) { + UpdateBurnStatus(ImageBurnStatus(amount_burnt, total_size), BURN_UPDATE); +} + +void BurnLibraryImpl::OnBurnImageFail() { + UpdateBurnStatus(ImageBurnStatus(0, 0), BURN_FAIL); +} + void BurnLibraryImpl::DevicesUnmountedCallback(void* object, bool success) { DCHECK(object); BurnLibraryImpl* self = static_cast<BurnLibraryImpl*>(object); if (success) { self->BurnImage(); } else { - self->UpdateBurnStatus(ImageBurnStatus(self->target_file_path_.c_str(), - 0, 0, "Error unmounting device"), BURN_FAIL); + self->UpdateBurnStatus(ImageBurnStatus(0, 0), BURN_FAIL); } } void BurnLibraryImpl::BurnImage() { - RequestBurn(source_image_file_.c_str(), target_file_path_.c_str(), - &BurnLibraryImpl::BurnStatusChangedHandler, this); -} - -void BurnLibraryImpl::BurnStatusChangedHandler(void* object, - const BurnStatus& status, BurnEventType evt) { - DCHECK(object); - BurnLibraryImpl* self = static_cast<BurnLibraryImpl*>(object); - -// Copy burn status because it will be freed after returning from this method. - ImageBurnStatus status_copy(status); - BurnEvent event = UNKNOWN; - switch (evt) { - case(BURN_CANCELED): - event = BURN_FAIL; - break; - case(BURN_COMPLETE): - event = BURN_SUCCESS; - break; - case(BURN_UPDATED): - case(BURN_STARTED): - event = BURN_UPDATE; - break; - } - self->UpdateBurnStatus(status_copy, event); + DBusThreadManager::Get()->GetImageBurnerClient()->BurnImage( + source_image_file_, + target_file_path_, + base::Bind(&BurnLibraryImpl::OnBurnImageFail, + weak_ptr_factory_.GetWeakPtr())); } void BurnLibraryImpl::UpdateBurnStatus(const ImageBurnStatus& status, @@ -272,6 +247,8 @@ class BurnLibraryStubImpl : public BurnLibrary { DISALLOW_COPY_AND_ASSIGN(BurnLibraryStubImpl); }; +} // namespace + // static BurnLibrary* BurnLibrary::GetImpl(bool stub) { BurnLibrary* impl; diff --git a/chrome/browser/chromeos/cros/burn_library.h b/chrome/browser/chromeos/cros/burn_library.h index 5f0ed58..b617aa4 100644 --- a/chrome/browser/chromeos/cros/burn_library.h +++ b/chrome/browser/chromeos/cros/burn_library.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -7,41 +7,19 @@ #define CHROME_BROWSER_CHROMEOS_CROS_BURN_LIBRARY_H_ #include <string> -#include <vector> #include "base/file_path.h" -#include "base/file_util.h" -#include "base/memory/weak_ptr.h" -#include "base/observer_list.h" -#include "third_party/cros/chromeos_imageburn.h" struct ImageBurnStatus { - ImageBurnStatus() : target_path(), - amount_burnt(0), - total_size(0), - error() { + ImageBurnStatus() : amount_burnt(0), total_size(0) { } - explicit ImageBurnStatus(const chromeos::BurnStatus& status) - : amount_burnt(status.amount_burnt), total_size(status.total_size) { - if (status.target_path) - target_path = status.target_path; - if (status.error) - error = status.error; + ImageBurnStatus(int64 burnt, int64 total) + : amount_burnt(burnt), total_size(total) { } - ImageBurnStatus(const char* path, int64 burnt, int64 total, const char* err) - : total_size(total) { - if (path) - target_path = path; - if (err) - error = err; - } - - std::string target_path; int64 amount_burnt; int64 total_size; - std::string error; }; namespace chromeos { @@ -60,8 +38,9 @@ class BurnLibrary { public: class Observer { public: - virtual void BurnProgressUpdated(BurnLibrary* object, BurnEvent evt, - const ImageBurnStatus& status) = 0; + virtual void BurnProgressUpdated(BurnLibrary* object, + BurnEvent evt, + const ImageBurnStatus& status) = 0; }; virtual ~BurnLibrary() {} |