OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #include "chrome/browser/chromeos/cros/burn_library.h" |
| 6 |
| 7 #include <cstring> |
| 8 #include "base/linked_ptr.h" |
| 9 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 10 #include "chrome/browser/chrome_thread.h" |
| 11 |
| 12 namespace chromeos { |
| 13 |
| 14 BurnLibraryImpl::BurnLibraryImpl() { |
| 15 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 16 Init(); |
| 17 } else { |
| 18 LOG(ERROR) << "Cros Library has not been loaded"; |
| 19 } |
| 20 } |
| 21 |
| 22 BurnLibraryImpl::~BurnLibraryImpl() { |
| 23 if (burn_status_connection_) { |
| 24 DisconnectBurnStatus(burn_status_connection_); |
| 25 } |
| 26 } |
| 27 |
| 28 void BurnLibraryImpl::AddObserver(Observer* observer) { |
| 29 observers_.AddObserver(observer); |
| 30 } |
| 31 |
| 32 void BurnLibraryImpl::RemoveObserver(Observer* observer) { |
| 33 observers_.RemoveObserver(observer); |
| 34 } |
| 35 |
| 36 bool BurnLibraryImpl::DoBurn(const FilePath& from_path, |
| 37 const FilePath& to_path) { |
| 38 BurnLibraryTaskProxy* task = new BurnLibraryTaskProxy(AsWeakPtr()); |
| 39 task->AddRef(); |
| 40 task->BurnImage(from_path, to_path); |
| 41 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 42 NewRunnableMethod(task, &BurnLibraryTaskProxy::BurnImage, |
| 43 from_path, to_path)); |
| 44 return true; |
| 45 } |
| 46 |
| 47 bool BurnLibraryImpl::BurnImage(const FilePath& from_path, |
| 48 const FilePath& to_path) { |
| 49 // Make sure we run on file thread. |
| 50 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 51 |
| 52 // Check if there is a target path already being burnt to. |
| 53 if (target_path_ == "") { |
| 54 target_path_ = to_path.value(); |
| 55 } else { |
| 56 return false; |
| 57 } |
| 58 |
| 59 StartBurn(from_path.value().c_str(), to_path.value().c_str(), |
| 60 burn_status_connection_); |
| 61 return true; |
| 62 } |
| 63 |
| 64 void BurnLibraryImpl::BurnStatusChangedHandler(void* object, |
| 65 const BurnStatus& status, |
| 66 BurnEventType evt) { |
| 67 BurnLibraryImpl* burn = static_cast<BurnLibraryImpl*>(object); |
| 68 |
| 69 // Copy burn status because it will be freed after returning from this method. |
| 70 ImageBurnStatus* status_copy = new ImageBurnStatus(status); |
| 71 |
| 72 BurnLibraryTaskProxy* task = new BurnLibraryTaskProxy(burn->AsWeakPtr()); |
| 73 task->AddRef(); |
| 74 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 75 NewRunnableMethod(task, &BurnLibraryTaskProxy::UpdateBurnStatus, |
| 76 status_copy, evt)); |
| 77 } |
| 78 |
| 79 void BurnLibraryImpl::Init() { |
| 80 burn_status_connection_ = MonitorBurnStatus(&BurnStatusChangedHandler, this); |
| 81 } |
| 82 |
| 83 void BurnLibraryImpl::UpdateBurnStatus(const ImageBurnStatus& status, |
| 84 BurnEventType evt) { |
| 85 // Make sure we run on UI thread. |
| 86 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 87 |
| 88 // If burn is finished, remove target paths from paths being burnt to. |
| 89 // This has to be done in thread-safe way, hence using task proxy class. |
| 90 if ((evt == BURN_CANCELED || evt == BURN_COMPLETE) && |
| 91 target_path_ == status.target_path) |
| 92 target_path_ = ""; |
| 93 |
| 94 FOR_EACH_OBSERVER(Observer, observers_, ProgressUpdated(this, evt, status)); |
| 95 } |
| 96 |
| 97 BurnLibraryTaskProxy::BurnLibraryTaskProxy( |
| 98 const base::WeakPtr<BurnLibraryImpl>& library) |
| 99 : library_(library) { |
| 100 } |
| 101 |
| 102 void BurnLibraryTaskProxy::BurnImage(const FilePath& from_path, |
| 103 const FilePath& to_path) { |
| 104 library_->BurnImage(from_path, to_path); |
| 105 } |
| 106 |
| 107 void BurnLibraryTaskProxy::UpdateBurnStatus(ImageBurnStatus* status, |
| 108 BurnEventType evt) { |
| 109 library_->UpdateBurnStatus(*status, evt); |
| 110 delete status; |
| 111 } |
| 112 |
| 113 |
| 114 class BurnLibraryStubImpl : public BurnLibrary { |
| 115 public: |
| 116 BurnLibraryStubImpl() {} |
| 117 virtual ~BurnLibraryStubImpl() {} |
| 118 |
| 119 // BurnLibrary overrides. |
| 120 virtual void AddObserver(Observer* observer) {} |
| 121 virtual void RemoveObserver(Observer* observer) {} |
| 122 virtual bool DoBurn(const FilePath& from_path, const FilePath& to_path) { |
| 123 return false; |
| 124 } |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(BurnLibraryStubImpl); |
| 127 }; |
| 128 |
| 129 // static |
| 130 BurnLibrary* BurnLibrary::GetImpl(bool stub) { |
| 131 if (stub) |
| 132 return new BurnLibraryStubImpl(); |
| 133 else |
| 134 return new BurnLibraryImpl(); |
| 135 } |
| 136 |
| 137 } // namespace chromeos |
| 138 |
| 139 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 140 // won't be deleted until it's last InvokeLater is run. |
| 141 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::BurnLibraryImpl); |
| 142 |
OLD | NEW |