summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/update_library.cc
blob: 0ea5f2cba1f6a3595fb188f0a958e159b4ba08df (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
// Copyright (c) 2009 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/chrome_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"

// 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);

namespace chromeos {

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

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

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

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

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

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

void UpdateLibraryImpl::Init() {
  status_connection_ = MonitorUpdateStatus(&ChangedHandler, this);
}

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

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

}  // namespace chromeos