1731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/callback.h"
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/gtest_prod_util.h"
13dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/cancelable_request.h"
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass FilePath;
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace chromeos {
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// ChromeOSVersionLoader loads the version of Chrome OS from the file system.
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Loading is done asynchronously on the file thread. Once loaded,
21731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// ChromeOSVersionLoader callback to a method of your choice with the version
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// (or an empty string if the version couldn't be found).
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// To use ChromeOSVersionLoader do the following:
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// . In your class define a member field of type chromeos::VersionLoader and
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//   CancelableRequestConsumerBase.
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// . Define the callback method, something like:
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//   void OnGetChromeOSVersion(chromeos::VersionLoader::Handle,
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//                             std::string version);
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// . When you want the version invoke:  loader.GetVersion(&consumer, callback);
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// This class also provides the ability to load the bios firmware using
33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   loader.GetFirmware(&consumer, callback);
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass VersionLoader : public CancelableRequestProvider {
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  VersionLoader();
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  enum VersionFormat {
39201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    VERSION_SHORT,
40201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    VERSION_SHORT_WITH_DATE,
41201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    VERSION_FULL,
42201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  };
43201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Signature
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef Callback2<Handle, std::string>::Type GetVersionCallback;
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef CancelableRequest<GetVersionCallback> GetVersionRequest;
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
48ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef Callback2<Handle, std::string>::Type GetFirmwareCallback;
49ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef CancelableRequest<GetFirmwareCallback> GetFirmwareRequest;
50ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Asynchronously requests the version.
52731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  // If |full_version| is true version string with extra info is extracted,
53731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  // otherwise it's in short format x.x.xx.x.
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Handle GetVersion(CancelableRequestConsumerBase* consumer,
55731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick                    GetVersionCallback* callback,
56201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch                    VersionFormat format);
57731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  Handle GetFirmware(CancelableRequestConsumerBase* consumer,
59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                     GetFirmwareCallback* callback);
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Parse the version information as a Chrome platfrom, not Chrome OS
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // TODO(rkc): Change this and everywhere it is used once we switch Chrome OS
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // over to xx.yyy.zz version numbers instead of 0.xx.yyy.zz
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Refer to http://code.google.com/p/chromium-os/issues/detail?id=15789
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void EnablePlatformVersions(bool enable);
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
67731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  static const char kFullVersionPrefix[];
68731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  static const char kVersionPrefix[];
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  static const char kFirmwarePrefix[];
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
72731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFullVersion);
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseVersion);
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  FRIEND_TEST_ALL_PREFIXES(VersionLoaderTest, ParseFirmware);
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // VersionLoader calls into the Backend on the file thread to load
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // and extract the version.
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  class Backend : public base::RefCountedThreadSafe<Backend> {
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch   public:
80ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    Backend() : parse_as_platform_(false) {}
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // Calls ParseVersion to get the version # and notifies request.
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // This is invoked on the file thread.
84731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick    // If |full_version| is true then extra info is passed in version string.
85731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick    void GetVersion(scoped_refptr<GetVersionRequest> request,
86201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch                    VersionFormat format);
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
88ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // Calls ParseFirmware to get the firmware # and notifies request.
89ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // This is invoked on the file thread.
90ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    void GetFirmware(scoped_refptr<GetFirmwareRequest> request);
91ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
92ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    void set_parse_as_platform(bool value) { parse_as_platform_ = value; }
93ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch   private:
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    friend class base::RefCountedThreadSafe<Backend>;
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    bool parse_as_platform_;
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    ~Backend() {}
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    DISALLOW_COPY_AND_ASSIGN(Backend);
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Extracts the version from the file.
105731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  // |prefix| specifies what key defines version data.
106731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  static std::string ParseVersion(const std::string& contents,
107731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick                                  const std::string& prefix);
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
109ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Extracts the firmware from the file.
110ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  static std::string ParseFirmware(const std::string& contents);
111ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  scoped_refptr<Backend> backend_;
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(VersionLoader);
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace chromeos
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_CHROMEOS_VERSION_LOADER_H_
120