Home | History | Annotate | Download | only in cros
      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 #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/memory/singleton.h"
     12 #include "base/observer_list.h"
     13 #include "base/time.h"
     14 #include "third_party/cros/chromeos_update_engine.h"
     15 
     16 namespace 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 
     22 class 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   virtual bool HasObserver(Observer* observer) = 0;
     65 
     66   // Requests an update check and calls |callback| when completed.
     67   virtual void RequestUpdateCheck(chromeos::UpdateCallback callback,
     68                                   void* user_data) = 0;
     69 
     70   // Reboots if update has been performed.
     71   virtual bool RebootAfterUpdate() = 0;
     72 
     73   // Sets the release track (channel). |track| should look like
     74   // "beta-channel" and "dev-channel". Returns true on success.
     75   virtual void SetReleaseTrack(const std::string& track) = 0;
     76 
     77   // Calls |callback| with the release track (channel). On error, calls
     78   // |callback| with NULL.
     79   virtual void GetReleaseTrack(chromeos::UpdateTrackCallback callback,
     80                                void* user_data) = 0;
     81 
     82   virtual const Status& status() const = 0;
     83 
     84   // Factory function, creates a new instance and returns ownership.
     85   // For normal usage, access the singleton via CrosLibrary::Get().
     86   static UpdateLibrary* GetImpl(bool stub);
     87 };
     88 
     89 }  // namespace chromeos
     90 
     91 #endif  // CHROME_BROWSER_CHROMEOS_CROS_UPDATE_LIBRARY_H_
     92