version_loader.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2010 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#pragma once
8
9#include <string>
10
11#include "base/callback.h"
12#include "base/gtest_prod_util.h"
13#include "content/browser/cancelable_request.h"
14
15class FilePath;
16
17namespace chromeos {
18
19// ChromeOSVersionLoader loads the version of Chrome OS from the file system.
20// Loading is done asynchronously on the file thread. Once loaded,
21// ChromeOSVersionLoader callback to a method of your choice with the version
22// (or an empty string if the version couldn't be found).
23// To use ChromeOSVersionLoader do the following:
24//
25// . In your class define a member field of type chromeos::VersionLoader and
26//   CancelableRequestConsumerBase.
27// . Define the callback method, something like:
28//   void OnGetChromeOSVersion(chromeos::VersionLoader::Handle,
29//                             std::string version);
30// . When you want the version invoke:  loader.GetVersion(&consumer, callback);
31//
32// This class also provides the ability to load the bios firmware using
33//   loader.GetFirmware(&consumer, callback);
34class VersionLoader : public CancelableRequestProvider {
35 public:
36  VersionLoader();
37
38  enum VersionFormat {
39    VERSION_SHORT,
40    VERSION_SHORT_WITH_DATE,
41    VERSION_FULL,
42  };
43
44  // Signature
45  typedef Callback2<Handle, std::string>::Type GetVersionCallback;
46  typedef CancelableRequest<GetVersionCallback> GetVersionRequest;
47
48  typedef Callback2<Handle, std::string>::Type GetFirmwareCallback;
49  typedef CancelableRequest<GetFirmwareCallback> GetFirmwareRequest;
50
51  // Asynchronously requests the version.
52  // If |full_version| is true version string with extra info is extracted,
53  // otherwise it's in short format x.x.xx.x.
54  Handle GetVersion(CancelableRequestConsumerBase* consumer,
55                    GetVersionCallback* callback,
56                    VersionFormat format);
57
58  Handle GetFirmware(CancelableRequestConsumerBase* consumer,
59                     GetFirmwareCallback* callback);
60
61  // Parse the version information as a Chrome platfrom, not Chrome OS
62  // TODO(rkc): Change this and everywhere it is used once we switch Chrome OS
63  // over to xx.yyy.zz version numbers instead of 0.xx.yyy.zz
64  // Refer to http://code.google.com/p/chromium-os/issues/detail?id=15789
65  void EnablePlatformVersions(bool enable);
66
67  static const char kFullVersionPrefix[];
68  static const char kVersionPrefix[];
69  static const char kFirmwarePrefix[];
70
71 private:
72  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFullVersion);
73  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseVersion);
74  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFirmware);
75
76  // VersionLoader calls into the Backend on the file thread to load
77  // and extract the version.
78  class Backend : public base::RefCountedThreadSafe<Backend> {
79   public:
80    Backend() : parse_as_platform_(false) {}
81
82    // Calls ParseVersion to get the version # and notifies request.
83    // This is invoked on the file thread.
84    // If |full_version| is true then extra info is passed in version string.
85    void GetVersion(scoped_refptr<GetVersionRequest> request,
86                    VersionFormat format);
87
88    // Calls ParseFirmware to get the firmware # and notifies request.
89    // This is invoked on the file thread.
90    void GetFirmware(scoped_refptr<GetFirmwareRequest> request);
91
92    void set_parse_as_platform(bool value) { parse_as_platform_ = value; }
93
94   private:
95    friend class base::RefCountedThreadSafe<Backend>;
96
97    bool parse_as_platform_;
98
99    ~Backend() {}
100
101    DISALLOW_COPY_AND_ASSIGN(Backend);
102  };
103
104  // Extracts the version from the file.
105  // |prefix| specifies what key defines version data.
106  static std::string ParseVersion(const std::string& contents,
107                                  const std::string& prefix);
108
109  // Extracts the firmware from the file.
110  static std::string ParseFirmware(const std::string& contents);
111
112  scoped_refptr<Backend> backend_;
113
114  DISALLOW_COPY_AND_ASSIGN(VersionLoader);
115};
116
117}  // namespace chromeos
118
119#endif  // CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
120