update_library.h revision 513209b27ff55e2841eac0e4120199c23acce758
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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_CHROMEOS_CROS_UPDATE_LIBRARY_H_
6#define CHROME_BROWSER_CHROMEOS_CROS_UPDATE_LIBRARY_H_
7#pragma once
8
9#include <string>
10
11#include "base/observer_list.h"
12#include "base/singleton.h"
13#include "base/time.h"
14#include "cros/chromeos_update_engine.h"
15
16namespace chromeos {
17
18// This interface defines interaction with the ChromeOS update library APIs.
19// Classes can add themselves as observers. Users can get an instance of this
20// library class like this: chromeos::CrosLibrary::Get()->GetUpdateLibrary()
21
22class UpdateLibrary {
23 public:
24  // TODO(seanparent): Should make the UpdateProgress type copyable.
25  // We need to copy it to bind it for a deferred notification.
26  // Modifying the cros library just for that, for a single use case,
27  // isn't worth it. Instead we define this a local Status struct that
28  // is copyable.
29
30  struct Status {
31    Status()
32        : status(UPDATE_STATUS_IDLE),
33          download_progress(0.0),
34          last_checked_time(0),
35          new_size(0) {
36    }
37
38    explicit Status(const UpdateProgress& x) :
39        status(x.status_),
40        download_progress(x.download_progress_),
41        last_checked_time(x.last_checked_time_),
42        new_version(x.new_version_),
43        new_size(x.new_size_) {
44    }
45
46    UpdateStatusOperation status;
47    double download_progress;  // 0.0 - 1.0
48    int64_t last_checked_time;  // As reported by std::time().
49    std::string new_version;
50    int64_t new_size;  // Valid during DOWNLOADING, in bytes.
51  };
52
53  class Observer {
54   public:
55    virtual ~Observer() { }
56    virtual void UpdateStatusChanged(UpdateLibrary* library) = 0;
57  };
58
59//  static UpdateLibrary* GetStubImplementation();
60
61  virtual ~UpdateLibrary() {}
62  virtual void AddObserver(Observer* observer) = 0;
63  virtual void RemoveObserver(Observer* observer) = 0;
64
65  // Initiates update check and returns true if check was initiated.
66  virtual bool CheckForUpdate() = 0;
67
68  // Reboots if update has been performed.
69  virtual bool RebootAfterUpdate() = 0;
70
71  // Sets the release track (channel). |track| should look like
72  // "beta-channel" and "dev-channel". Returns true on success.
73  virtual bool SetReleaseTrack(const std::string& track) = 0;
74
75  // Returns the release track (channel). On error, returns an empty
76  // string.
77  virtual std::string GetReleaseTrack() = 0;
78
79  virtual const Status& status() const = 0;
80
81  // Factory function, creates a new instance and returns ownership.
82  // For normal usage, access the singleton via CrosLibrary::Get().
83  static UpdateLibrary* GetImpl(bool stub);
84};
85
86}  // namespace chromeos
87
88#endif  // CHROME_BROWSER_CHROMEOS_CROS_UPDATE_LIBRARY_H_
89