summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/update_library.cc
blob: c59bc49d871305124d5b9d704c2d17630eab52e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2011 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/update_library.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "content/browser/browser_thread.h"

namespace chromeos {

class UpdateLibraryImpl : public UpdateLibrary {
 public:
  UpdateLibraryImpl()
    : status_connection_(NULL) {
    if (CrosLibrary::Get()->EnsureLoaded()) {
      Init();
    }
  }

  virtual ~UpdateLibraryImpl() {
    if (status_connection_) {
      DisconnectUpdateProgress(status_connection_);
    }
  }

  void AddObserver(Observer* observer) {
    observers_.AddObserver(observer);
  }

  void RemoveObserver(Observer* observer) {
    observers_.RemoveObserver(observer);
  }

  bool HasObserver(Observer* observer) {
    return observers_.HasObserver(observer);
  }

  void RequestUpdateCheck(chromeos::UpdateCallback callback, void* user_data) {
    if (CrosLibrary::Get()->EnsureLoaded())
      chromeos::RequestUpdateCheck(callback, user_data);
  }

  bool RebootAfterUpdate() {
    if (!CrosLibrary::Get()->EnsureLoaded())
      return false;

    return RebootIfUpdated();
  }

  void SetReleaseTrack(const std::string& track) {
    if (CrosLibrary::Get()->EnsureLoaded())
      chromeos::SetUpdateTrack(track);
  }

  void GetReleaseTrack(chromeos::UpdateTrackCallback callback,
                       void* user_data) {
    if (CrosLibrary::Get()->EnsureLoaded())
      chromeos::RequestUpdateTrack(callback, user_data);
  }

  const UpdateLibrary::Status& status() const {
    return status_;
  }

 private:
  static void ChangedHandler(void* object,
      const UpdateProgress& status) {
    UpdateLibraryImpl* updater = static_cast<UpdateLibraryImpl*>(object);
    updater->UpdateStatus(Status(status));
  }

  void Init() {
    status_connection_ = MonitorUpdateStatus(&ChangedHandler, this);
    // Asynchronously load the initial state.
    RequestUpdateStatus(&ChangedHandler, this);
  }

  void UpdateStatus(const Status& status) {
    // Make sure we run on UI thread.
    if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
      BrowserThread::PostTask(
          BrowserThread::UI, FROM_HERE,
          NewRunnableMethod(this, &UpdateLibraryImpl::UpdateStatus, status));
      return;
    }

    status_ = status;
    FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(this));
  }

  ObserverList<Observer> observers_;

  // A reference to the update api, to allow callbacks when the update
  // status changes.
  UpdateStatusConnection status_connection_;

  // The latest power status.
  Status status_;

  DISALLOW_COPY_AND_ASSIGN(UpdateLibraryImpl);
};

class UpdateLibraryStubImpl : public UpdateLibrary {
 public:
  UpdateLibraryStubImpl() {}
  virtual ~UpdateLibraryStubImpl() {}
  void AddObserver(Observer* observer) {}
  void RemoveObserver(Observer* observer) {}
  bool HasObserver(Observer* observer) { return false; }
  void RequestUpdateCheck(chromeos::UpdateCallback callback, void* user_data) {
    if (callback)
      callback(user_data, UPDATE_RESULT_FAILED, "stub update");
  }
  bool RebootAfterUpdate() { return false; }
  void SetReleaseTrack(const std::string& track) { }
  void GetReleaseTrack(chromeos::UpdateTrackCallback callback,
                       void* user_data) {
    if (callback)
      callback(user_data, "beta-channel");
  }
  const UpdateLibrary::Status& status() const {
    return status_;
  }

 private:
  Status status_;

  DISALLOW_COPY_AND_ASSIGN(UpdateLibraryStubImpl);
};

// static
UpdateLibrary* UpdateLibrary::GetImpl(bool stub) {
  if (stub)
    return new UpdateLibraryStubImpl();
  else
    return new UpdateLibraryImpl();
}

}  // namespace chromeos

// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::UpdateLibraryImpl);