OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/cros/update_library.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/observer_list.h" | |
12 #include "chrome/browser/chromeos/cros/cros_library.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 | |
15 using content::BrowserThread; | |
16 | |
17 namespace chromeos { | |
18 | |
19 class UpdateLibraryImpl : public UpdateLibrary { | |
20 public: | |
21 UpdateLibraryImpl() : status_connection_(NULL) {} | |
22 | |
23 virtual ~UpdateLibraryImpl() { | |
24 if (status_connection_) { | |
25 chromeos::DisconnectUpdateProgress(status_connection_); | |
26 status_connection_ = NULL; | |
27 } | |
28 } | |
29 | |
30 // Begin UpdateLibrary implementation. | |
31 virtual void Init() OVERRIDE { | |
32 DCHECK(CrosLibrary::Get()->libcros_loaded()); | |
33 CHECK(!status_connection_) << "Already initialized"; | |
34 status_connection_ = | |
35 chromeos::MonitorUpdateStatus(&UpdateStatusHandler, this); | |
36 // Asynchronously load the initial state. | |
37 chromeos::RequestUpdateStatus(&UpdateStatusHandler, this); | |
38 } | |
39 | |
40 virtual void AddObserver(Observer* observer) OVERRIDE { | |
41 observers_.AddObserver(observer); | |
42 } | |
43 | |
44 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
45 observers_.RemoveObserver(observer); | |
46 } | |
47 | |
48 virtual bool HasObserver(Observer* observer) OVERRIDE { | |
49 return observers_.HasObserver(observer); | |
50 } | |
51 | |
52 virtual void RequestUpdateCheck(chromeos::UpdateCallback callback, | |
53 void* user_data) OVERRIDE { | |
54 chromeos::RequestUpdateCheck(callback, user_data); | |
55 } | |
56 | |
57 virtual void RebootAfterUpdate() OVERRIDE { | |
58 chromeos::RebootIfUpdated(); | |
59 } | |
60 | |
61 virtual void SetReleaseTrack(const std::string& track) OVERRIDE { | |
62 chromeos::SetUpdateTrack(track); | |
63 } | |
64 | |
65 virtual void GetReleaseTrack(chromeos::UpdateTrackCallback callback, | |
66 void* user_data) OVERRIDE { | |
67 chromeos::RequestUpdateTrack(callback, user_data); | |
68 } | |
69 // End UpdateLibrary implementation. | |
70 | |
71 virtual const UpdateLibrary::Status& status() const OVERRIDE{ | |
72 return status_; | |
73 } | |
74 | |
75 private: | |
76 static void UpdateStatusHandler(void* object, const UpdateProgress& status) { | |
77 UpdateLibraryImpl* impl = static_cast<UpdateLibraryImpl*>(object); | |
78 impl->UpdateStatus(Status(status)); | |
79 } | |
80 | |
81 void UpdateStatus(const Status& status) { | |
82 // Called from UpdateStatusHandler, a libcros callback which should | |
83 // always run on UI thread. | |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
85 | |
86 status_ = status; | |
87 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(status)); | |
88 } | |
89 | |
90 ObserverList<Observer> observers_; | |
91 | |
92 // A reference to the update api, to allow callbacks when the update | |
93 // status changes. | |
94 UpdateStatusConnection status_connection_; | |
95 | |
96 // The latest power status. | |
97 Status status_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryImpl); | |
100 }; | |
101 | |
102 class UpdateLibraryStubImpl : public UpdateLibrary { | |
103 public: | |
104 UpdateLibraryStubImpl() {} | |
105 virtual ~UpdateLibraryStubImpl() {} | |
106 | |
107 // Begin UpdateLibrary implementation. | |
108 virtual void Init() OVERRIDE {} | |
109 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
110 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
111 virtual bool HasObserver(Observer* observer) OVERRIDE { return false; } | |
112 virtual void RequestUpdateCheck(chromeos::UpdateCallback callback, | |
113 void* user_data) OVERRIDE { | |
114 if (callback) | |
115 callback(user_data, UPDATE_RESULT_FAILED, "stub update"); | |
116 } | |
117 virtual void RebootAfterUpdate() OVERRIDE {} | |
118 virtual void SetReleaseTrack(const std::string& track) OVERRIDE {} | |
119 virtual void GetReleaseTrack(chromeos::UpdateTrackCallback callback, | |
120 void* user_data) OVERRIDE { | |
121 if (callback) | |
122 callback(user_data, "beta-channel"); | |
123 } | |
124 // End UpdateLibrary implementation. | |
125 | |
126 virtual const UpdateLibrary::Status& status() const OVERRIDE { | |
127 return status_; | |
128 } | |
129 | |
130 private: | |
131 Status status_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(UpdateLibraryStubImpl); | |
134 }; | |
135 | |
136 // static | |
137 UpdateLibrary* UpdateLibrary::GetImpl(bool stub) { | |
138 UpdateLibrary* impl; | |
139 if (stub) | |
140 impl = new UpdateLibraryStubImpl(); | |
141 else | |
142 impl = new UpdateLibraryImpl(); | |
143 impl->Init(); | |
144 return impl; | |
145 } | |
146 | |
147 } // namespace chromeos | |
OLD | NEW |