Home | History | Annotate | Download | only in chromeos
      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_VERSION_LOADER_H_
      6 #define CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/gtest_prod_util.h"
     12 #include "chrome/common/cancelable_task_tracker.h"
     13 
     14 namespace chromeos {
     15 
     16 // ChromeOSVersionLoader loads the version of Chrome OS from the file system.
     17 // Loading is done asynchronously in the blocking thread pool. Once loaded,
     18 // ChromeOSVersionLoader callback to a method of your choice with the version
     19 // (or an empty string if the version couldn't be found).
     20 // To use ChromeOSVersionLoader do the following:
     21 //
     22 // . In your class define a member field of type chromeos::VersionLoader and
     23 //   CancelableTaskTracker.
     24 // . Define the callback method, something like:
     25 //   void OnGetChromeOSVersion(const std::string& version);
     26 // . When you want the version invoke:
     27 //   VersionLoader::GetVersion()
     28 //
     29 // This class also provides the ability to load the bios firmware using
     30 //   VersionLoader::GetFirmware()
     31 class VersionLoader {
     32  public:
     33   VersionLoader();
     34   virtual ~VersionLoader();
     35 
     36   enum VersionFormat {
     37     VERSION_SHORT,
     38     VERSION_SHORT_WITH_DATE,
     39     VERSION_FULL,
     40   };
     41 
     42   // Signature
     43   typedef base::Callback<void(const std::string&)> GetVersionCallback;
     44   typedef base::Callback<void(const std::string&)> GetFirmwareCallback;
     45 
     46   // Asynchronously requests the version.
     47   // If |full_version| is true version string with extra info is extracted,
     48   // otherwise it's in short format x.x.xx.x.
     49   CancelableTaskTracker::TaskId GetVersion(VersionFormat format,
     50                                            const GetVersionCallback& callback,
     51                                            CancelableTaskTracker* tracker);
     52 
     53   CancelableTaskTracker::TaskId GetFirmware(const GetFirmwareCallback& callback,
     54                                             CancelableTaskTracker* tracker);
     55 
     56   static const char kFullVersionPrefix[];
     57   static const char kVersionPrefix[];
     58   static const char kFirmwarePrefix[];
     59 
     60  private:
     61   FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFullVersion);
     62   FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseVersion);
     63   FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFirmware);
     64 
     65   // VersionLoader calls into the Backend in the blocking thread pool to load
     66   // and extract the version.
     67   class Backend : public base::RefCountedThreadSafe<Backend> {
     68    public:
     69     Backend() {}
     70 
     71     // Calls ParseVersion to get the version # and notifies request.
     72     // This is invoked in the blocking thread pool.
     73     // If |full_version| is true then extra info is passed in version string.
     74     void GetVersion(VersionFormat format, std::string* version);
     75 
     76     // Calls ParseFirmware to get the firmware # and notifies request.
     77     // This is invoked in the blocking thread pool.
     78     void GetFirmware(std::string* firmware);
     79 
     80    private:
     81     friend class base::RefCountedThreadSafe<Backend>;
     82 
     83     ~Backend() {}
     84 
     85     DISALLOW_COPY_AND_ASSIGN(Backend);
     86   };
     87 
     88   // Extracts the version from the file.
     89   // |prefix| specifies what key defines version data.
     90   static std::string ParseVersion(const std::string& contents,
     91                                   const std::string& prefix);
     92 
     93   // Extracts the firmware from the file.
     94   static std::string ParseFirmware(const std::string& contents);
     95 
     96   scoped_refptr<Backend> backend_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(VersionLoader);
     99 };
    100 
    101 }  // namespace chromeos
    102 
    103 #endif  // CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
    104