summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/version_loader.h
blob: 1560b18e0897294f08c72816af2c0b773e9872b1 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2011 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.

#ifndef CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
#define CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
#pragma once

#include <string>

#include "base/callback_old.h"
#include "base/gtest_prod_util.h"
#include "content/browser/cancelable_request.h"

class FilePath;

namespace chromeos {

// ChromeOSVersionLoader loads the version of Chrome OS from the file system.
// Loading is done asynchronously on the file thread. Once loaded,
// ChromeOSVersionLoader callback to a method of your choice with the version
// (or an empty string if the version couldn't be found).
// To use ChromeOSVersionLoader do the following:
//
// . In your class define a member field of type chromeos::VersionLoader and
//   CancelableRequestConsumerBase.
// . Define the callback method, something like:
//   void OnGetChromeOSVersion(chromeos::VersionLoader::Handle,
//                             std::string version);
// . When you want the version invoke:  loader.GetVersion(&consumer, callback);
//
// This class also provides the ability to load the bios firmware using
//   loader.GetFirmware(&consumer, callback);
class VersionLoader : public CancelableRequestProvider {
 public:
  VersionLoader();
  virtual ~VersionLoader();

  enum VersionFormat {
    VERSION_SHORT,
    VERSION_SHORT_WITH_DATE,
    VERSION_FULL,
  };

  // Signature
  typedef Callback2<Handle, std::string>::Type GetVersionCallback;
  typedef CancelableRequest<GetVersionCallback> GetVersionRequest;

  typedef Callback2<Handle, std::string>::Type GetFirmwareCallback;
  typedef CancelableRequest<GetFirmwareCallback> GetFirmwareRequest;

  // Asynchronously requests the version.
  // If |full_version| is true version string with extra info is extracted,
  // otherwise it's in short format x.x.xx.x.
  Handle GetVersion(CancelableRequestConsumerBase* consumer,
                    GetVersionCallback* callback,
                    VersionFormat format);

  Handle GetFirmware(CancelableRequestConsumerBase* consumer,
                     GetFirmwareCallback* callback);

  // Parse the version information as a Chrome platfrom, not Chrome OS
  // TODO(rkc): Change this and everywhere it is used once we switch Chrome OS
  // over to xx.yyy.zz version numbers instead of 0.xx.yyy.zz
  // Refer to http://code.google.com/p/chromium-os/issues/detail?id=15789
  void EnablePlatformVersions(bool enable);

  static const char kFullVersionPrefix[];
  static const char kVersionPrefix[];
  static const char kFirmwarePrefix[];

 private:
  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFullVersion);
  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseVersion);
  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFirmware);

  // VersionLoader calls into the Backend on the file thread to load
  // and extract the version.
  class Backend : public base::RefCountedThreadSafe<Backend> {
   public:
    Backend() {}

    // Calls ParseVersion to get the version # and notifies request.
    // This is invoked on the file thread.
    // If |full_version| is true then extra info is passed in version string.
    void GetVersion(scoped_refptr<GetVersionRequest> request,
                    VersionFormat format);

    // Calls ParseFirmware to get the firmware # and notifies request.
    // This is invoked on the file thread.
    void GetFirmware(scoped_refptr<GetFirmwareRequest> request);

   private:
    friend class base::RefCountedThreadSafe<Backend>;

    ~Backend() {}

    DISALLOW_COPY_AND_ASSIGN(Backend);
  };

  // Extracts the version from the file.
  // |prefix| specifies what key defines version data.
  static std::string ParseVersion(const std::string& contents,
                                  const std::string& prefix);

  // Extracts the firmware from the file.
  static std::string ParseFirmware(const std::string& contents);

  scoped_refptr<Backend> backend_;

  DISALLOW_COPY_AND_ASSIGN(VersionLoader);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_