| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/chromeos/cros/update_library.h" | 5 #include "chrome/browser/chromeos/cros/update_library.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 | 11 |
| 12 namespace chromeos { |
| 13 |
| 14 class UpdateLibraryImpl : public UpdateLibrary { |
| 15 public: |
| 16 UpdateLibraryImpl() |
| 17 : status_connection_(NULL) { |
| 18 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 19 Init(); |
| 20 } |
| 21 } |
| 22 |
| 23 ~UpdateLibraryImpl() { |
| 24 if (status_connection_) { |
| 25 DisconnectUpdateProgress(status_connection_); |
| 26 } |
| 27 } |
| 28 |
| 29 void AddObserver(Observer* observer) { |
| 30 observers_.AddObserver(observer); |
| 31 } |
| 32 |
| 33 void RemoveObserver(Observer* observer) { |
| 34 observers_.RemoveObserver(observer); |
| 35 } |
| 36 |
| 37 bool CheckForUpdate() { |
| 38 if (!CrosLibrary::Get()->EnsureLoaded()) |
| 39 return false; |
| 40 |
| 41 return InitiateUpdateCheck(); |
| 42 } |
| 43 |
| 44 bool RebootAfterUpdate() { |
| 45 if (!CrosLibrary::Get()->EnsureLoaded()) |
| 46 return false; |
| 47 |
| 48 return RebootIfUpdated(); |
| 49 } |
| 50 |
| 51 const UpdateLibrary::Status& status() const { |
| 52 return status_; |
| 53 } |
| 54 |
| 55 private: |
| 56 static void ChangedHandler(void* object, |
| 57 const UpdateProgress& status) { |
| 58 UpdateLibraryImpl* updater = static_cast<UpdateLibraryImpl*>(object); |
| 59 updater->UpdateStatus(Status(status)); |
| 60 } |
| 61 |
| 62 void Init() { |
| 63 status_connection_ = MonitorUpdateStatus(&ChangedHandler, this); |
| 64 } |
| 65 |
| 66 void UpdateStatus(const Status& status) { |
| 67 // Make sure we run on UI thread. |
| 68 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 69 ChromeThread::PostTask( |
| 70 ChromeThread::UI, FROM_HERE, |
| 71 NewRunnableMethod(this, &UpdateLibraryImpl::UpdateStatus, status)); |
| 72 return; |
| 73 } |
| 74 |
| 75 status_ = status; |
| 76 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(this)); |
| 77 } |
| 78 |
| 79 ObserverList<Observer> observers_; |
| 80 |
| 81 // A reference to the update api, to allow callbacks when the update |
| 82 // status changes. |
| 83 UpdateStatusConnection status_connection_; |
| 84 |
| 85 // The latest power status. |
| 86 Status status_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryImpl); |
| 89 }; |
| 90 |
| 91 class UpdateLibraryStubImpl : public UpdateLibrary { |
| 92 public: |
| 93 UpdateLibraryStubImpl() {} |
| 94 ~UpdateLibraryStubImpl() {} |
| 95 void AddObserver(Observer* observer) {} |
| 96 void RemoveObserver(Observer* observer) {} |
| 97 bool CheckForUpdate() { return false; } |
| 98 bool RebootAfterUpdate() { return false; } |
| 99 const UpdateLibrary::Status& status() const { |
| 100 return status_; |
| 101 } |
| 102 |
| 103 private: |
| 104 Status status_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryStubImpl); |
| 107 }; |
| 108 |
| 109 // static |
| 110 UpdateLibrary* UpdateLibrary::GetImpl(bool stub) { |
| 111 if (stub) |
| 112 return new UpdateLibraryStubImpl(); |
| 113 else |
| 114 return new UpdateLibraryImpl(); |
| 115 } |
| 116 |
| 117 } // namespace chromeos |
| 118 |
| 12 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 119 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 13 // won't be deleted until it's last InvokeLater is run. | 120 // won't be deleted until it's last InvokeLater is run. |
| 14 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::UpdateLibraryImpl); | 121 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::UpdateLibraryImpl); |
| 15 | 122 |
| 16 namespace chromeos { | |
| 17 | |
| 18 UpdateLibraryImpl::UpdateLibraryImpl() | |
| 19 : status_connection_(NULL) { | |
| 20 if (CrosLibrary::Get()->EnsureLoaded()) { | |
| 21 Init(); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 UpdateLibraryImpl::~UpdateLibraryImpl() { | |
| 26 if (status_connection_) { | |
| 27 DisconnectUpdateProgress(status_connection_); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 void UpdateLibraryImpl::AddObserver(Observer* observer) { | |
| 32 observers_.AddObserver(observer); | |
| 33 } | |
| 34 | |
| 35 void UpdateLibraryImpl::RemoveObserver(Observer* observer) { | |
| 36 observers_.RemoveObserver(observer); | |
| 37 } | |
| 38 | |
| 39 bool UpdateLibraryImpl::CheckForUpdate() { | |
| 40 if (!CrosLibrary::Get()->EnsureLoaded()) | |
| 41 return false; | |
| 42 | |
| 43 return InitiateUpdateCheck(); | |
| 44 } | |
| 45 | |
| 46 bool UpdateLibraryImpl::RebootAfterUpdate() { | |
| 47 if (!CrosLibrary::Get()->EnsureLoaded()) | |
| 48 return false; | |
| 49 | |
| 50 return RebootIfUpdated(); | |
| 51 } | |
| 52 | |
| 53 const UpdateLibrary::Status& UpdateLibraryImpl::status() const { | |
| 54 return status_; | |
| 55 } | |
| 56 | |
| 57 // static | |
| 58 void UpdateLibraryImpl::ChangedHandler(void* object, | |
| 59 const UpdateProgress& status) { | |
| 60 UpdateLibraryImpl* updater = static_cast<UpdateLibraryImpl*>(object); | |
| 61 updater->UpdateStatus(Status(status)); | |
| 62 } | |
| 63 | |
| 64 void UpdateLibraryImpl::Init() { | |
| 65 status_connection_ = MonitorUpdateStatus(&ChangedHandler, this); | |
| 66 } | |
| 67 | |
| 68 void UpdateLibraryImpl::UpdateStatus(const Status& status) { | |
| 69 // Make sure we run on UI thread. | |
| 70 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | |
| 71 ChromeThread::PostTask( | |
| 72 ChromeThread::UI, FROM_HERE, | |
| 73 NewRunnableMethod(this, &UpdateLibraryImpl::UpdateStatus, status)); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 status_ = status; | |
| 78 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(this)); | |
| 79 } | |
| 80 | |
| 81 } // namespace chromeos | |
| 82 | |
| OLD | NEW |