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/chromeos/cros/update_library.h" | 5 #include "chrome/browser/chromeos/cros/update_library.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
9 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
10 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
11 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
12 | 13 |
13 namespace chromeos { | 14 namespace chromeos { |
14 | 15 |
15 class UpdateLibraryImpl : public UpdateLibrary { | 16 class UpdateLibraryImpl : public UpdateLibrary { |
16 public: | 17 public: |
17 UpdateLibraryImpl() : status_connection_(NULL) { | 18 UpdateLibraryImpl() : status_connection_(NULL) {} |
18 if (CrosLibrary::Get()->EnsureLoaded()) | |
19 Init(); | |
20 } | |
21 | 19 |
22 virtual ~UpdateLibraryImpl() { | 20 virtual ~UpdateLibraryImpl() { |
23 if (status_connection_) { | 21 if (status_connection_) { |
24 chromeos::DisconnectUpdateProgress(status_connection_); | 22 chromeos::DisconnectUpdateProgress(status_connection_); |
25 status_connection_ = NULL; | 23 status_connection_ = NULL; |
26 } | 24 } |
27 } | 25 } |
28 | 26 |
| 27 void Init() { |
| 28 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 29 CHECK(!status_connection_) << "Already initialized"; |
| 30 status_connection_ = |
| 31 chromeos::MonitorUpdateStatus(&UpdateStatusHandler, this); |
| 32 // Asynchronously load the initial state. |
| 33 chromeos::RequestUpdateStatus(&UpdateStatusHandler, this); |
| 34 } |
| 35 } |
| 36 |
29 void AddObserver(Observer* observer) { | 37 void AddObserver(Observer* observer) { |
30 observers_.AddObserver(observer); | 38 observers_.AddObserver(observer); |
31 } | 39 } |
32 | 40 |
33 void RemoveObserver(Observer* observer) { | 41 void RemoveObserver(Observer* observer) { |
34 observers_.RemoveObserver(observer); | 42 observers_.RemoveObserver(observer); |
35 } | 43 } |
36 | 44 |
37 bool HasObserver(Observer* observer) { | 45 bool HasObserver(Observer* observer) { |
38 return observers_.HasObserver(observer); | 46 return observers_.HasObserver(observer); |
(...skipping 20 matching lines...) Expand all Loading... |
59 void* user_data) { | 67 void* user_data) { |
60 if (CrosLibrary::Get()->EnsureLoaded()) | 68 if (CrosLibrary::Get()->EnsureLoaded()) |
61 chromeos::RequestUpdateTrack(callback, user_data); | 69 chromeos::RequestUpdateTrack(callback, user_data); |
62 } | 70 } |
63 | 71 |
64 const UpdateLibrary::Status& status() const { | 72 const UpdateLibrary::Status& status() const { |
65 return status_; | 73 return status_; |
66 } | 74 } |
67 | 75 |
68 private: | 76 private: |
69 static void ChangedHandler(void* object, const UpdateProgress& status) { | 77 static void UpdateStatusHandler(void* object, const UpdateProgress& status) { |
70 UpdateLibraryImpl* impl = static_cast<UpdateLibraryImpl*>(object); | 78 UpdateLibraryImpl* impl = static_cast<UpdateLibraryImpl*>(object); |
71 impl->UpdateStatus(Status(status)); | 79 impl->UpdateStatus(Status(status)); |
72 } | 80 } |
73 | 81 |
74 void Init() { | |
75 status_connection_ = chromeos::MonitorUpdateStatus(&ChangedHandler, this); | |
76 // Asynchronously load the initial state. | |
77 RequestUpdateStatus(&ChangedHandler, this); | |
78 } | |
79 | |
80 void UpdateStatus(const Status& status) { | 82 void UpdateStatus(const Status& status) { |
81 // Make sure we run on UI thread. | 83 // Make sure we run on UI thread. |
82 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 84 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
83 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
84 BrowserThread::UI, FROM_HERE, | 86 BrowserThread::UI, FROM_HERE, |
85 NewRunnableMethod(this, &UpdateLibraryImpl::UpdateStatus, status)); | 87 NewRunnableMethod(this, &UpdateLibraryImpl::UpdateStatus, status)); |
86 return; | 88 return; |
87 } | 89 } |
88 | 90 |
89 status_ = status; | 91 status_ = status; |
90 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(this)); | 92 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(this)); |
91 } | 93 } |
92 | 94 |
93 ObserverList<Observer> observers_; | 95 ObserverList<Observer> observers_; |
94 | 96 |
95 // A reference to the update api, to allow callbacks when the update | 97 // A reference to the update api, to allow callbacks when the update |
96 // status changes. | 98 // status changes. |
97 UpdateStatusConnection status_connection_; | 99 UpdateStatusConnection status_connection_; |
98 | 100 |
99 // The latest power status. | 101 // The latest power status. |
100 Status status_; | 102 Status status_; |
101 | 103 |
102 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryImpl); | 104 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryImpl); |
103 }; | 105 }; |
104 | 106 |
105 class UpdateLibraryStubImpl : public UpdateLibrary { | 107 class UpdateLibraryStubImpl : public UpdateLibrary { |
106 public: | 108 public: |
107 UpdateLibraryStubImpl() {} | 109 UpdateLibraryStubImpl() {} |
108 virtual ~UpdateLibraryStubImpl() {} | 110 virtual ~UpdateLibraryStubImpl() {} |
| 111 void Init() {} |
109 void AddObserver(Observer* observer) {} | 112 void AddObserver(Observer* observer) {} |
110 void RemoveObserver(Observer* observer) {} | 113 void RemoveObserver(Observer* observer) {} |
111 bool HasObserver(Observer* observer) { return false; } | 114 bool HasObserver(Observer* observer) { return false; } |
112 void RequestUpdateCheck(chromeos::UpdateCallback callback, void* user_data) { | 115 void RequestUpdateCheck(chromeos::UpdateCallback callback, void* user_data) { |
113 if (callback) | 116 if (callback) |
114 callback(user_data, UPDATE_RESULT_FAILED, "stub update"); | 117 callback(user_data, UPDATE_RESULT_FAILED, "stub update"); |
115 } | 118 } |
116 bool RebootAfterUpdate() { return false; } | 119 bool RebootAfterUpdate() { return false; } |
117 void SetReleaseTrack(const std::string& track) { } | 120 void SetReleaseTrack(const std::string& track) { } |
118 void GetReleaseTrack(chromeos::UpdateTrackCallback callback, | 121 void GetReleaseTrack(chromeos::UpdateTrackCallback callback, |
119 void* user_data) { | 122 void* user_data) { |
120 if (callback) | 123 if (callback) |
121 callback(user_data, "beta-channel"); | 124 callback(user_data, "beta-channel"); |
122 } | 125 } |
123 const UpdateLibrary::Status& status() const { | 126 const UpdateLibrary::Status& status() const { return status_; } |
124 return status_; | |
125 } | |
126 | 127 |
127 private: | 128 private: |
128 Status status_; | 129 Status status_; |
129 | 130 |
130 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryStubImpl); | 131 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryStubImpl); |
131 }; | 132 }; |
132 | 133 |
133 // static | 134 // static |
134 UpdateLibrary* UpdateLibrary::GetImpl(bool stub) { | 135 UpdateLibrary* UpdateLibrary::GetImpl(bool stub) { |
| 136 UpdateLibrary* impl; |
135 if (stub) | 137 if (stub) |
136 return new UpdateLibraryStubImpl(); | 138 impl = new UpdateLibraryStubImpl(); |
137 else | 139 else |
138 return new UpdateLibraryImpl(); | 140 impl = new UpdateLibraryImpl(); |
| 141 impl->Init(); |
| 142 return impl; |
139 } | 143 } |
140 | 144 |
141 } // namespace chromeos | 145 } // namespace chromeos |
142 | 146 |
143 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 147 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
144 // won't be deleted until it's last InvokeLater is run. | 148 // won't be deleted until it's last InvokeLater is run. |
145 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::UpdateLibraryImpl); | 149 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::UpdateLibraryImpl); |
OLD | NEW |