customization_document.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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_CUSTOMIZATION_DOCUMENT_H_
6#define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/singleton.h"
15#include "base/timer/timer.h"
16#include "base/values.h"
17#include "net/url_request/url_fetcher_delegate.h"
18#include "url/gurl.h"
19
20class PrefRegistrySimple;
21
22namespace base {
23class DictionaryValue;
24class FilePath;
25}
26
27namespace net {
28class URLFetcher;
29}
30
31// This test is in global namespace so it must be declared here.
32void Test__InitStartupCustomizationDocument(const std::string& manifest);
33
34namespace chromeos {
35
36namespace system {
37class StatisticsProvider;
38}  // system
39
40// Base class for OEM customization document classes.
41class CustomizationDocument {
42 public:
43  virtual ~CustomizationDocument();
44
45  // Return true if the document was successfully fetched and parsed.
46  bool IsReady() const { return root_.get(); }
47
48 protected:
49  explicit CustomizationDocument(const std::string& accepted_version);
50
51  virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
52  virtual bool LoadManifestFromString(const std::string& manifest);
53
54  std::string GetLocaleSpecificString(const std::string& locale,
55                                      const std::string& dictionary_name,
56                                      const std::string& entry_name) const;
57
58  scoped_ptr<base::DictionaryValue> root_;
59
60  // Value of the "version" attribute that is supported.
61  // Otherwise config is not loaded.
62  std::string accepted_version_;
63
64 private:
65  DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
66};
67
68// OEM startup customization document class.
69// Now StartupCustomizationDocument is loaded in c-tor so just after create it
70// may be ready or not (if manifest is missing or corrupted) and this state
71// won't be changed later (i.e. IsReady() always return the same value).
72class StartupCustomizationDocument : public CustomizationDocument {
73 public:
74  static StartupCustomizationDocument* GetInstance();
75
76  std::string GetHelpPage(const std::string& locale) const;
77  std::string GetEULAPage(const std::string& locale) const;
78
79  const std::string& registration_url() const { return registration_url_; }
80
81  // These methods can be called even if !IsReady(), in this case VPD values
82  // will be returned.
83  //
84  // Raw value of "initial_locale" like initial_locale="en-US,sv,da,fi,no" .
85  const std::string& initial_locale() const { return initial_locale_; }
86
87  // Vector of individual locale values.
88  const std::vector<std::string>& configured_locales() const;
89
90  // Default locale value (first value in initial_locale list).
91  const std::string& initial_locale_default() const;
92  const std::string& initial_timezone() const { return initial_timezone_; }
93  const std::string& keyboard_layout() const { return keyboard_layout_; }
94
95 private:
96  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic);
97  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD);
98  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest);
99  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, MultiLanguage);
100  friend class OobeLocalizationTest;
101  friend void ::Test__InitStartupCustomizationDocument(
102      const std::string& manifest);
103  friend struct DefaultSingletonTraits<StartupCustomizationDocument>;
104
105  // C-tor for singleton construction.
106  StartupCustomizationDocument();
107
108  // C-tor for test construction.
109  StartupCustomizationDocument(system::StatisticsProvider* provider,
110                               const std::string& manifest);
111
112  virtual ~StartupCustomizationDocument();
113
114  void Init(system::StatisticsProvider* provider);
115
116  // If |attr| exists in machine stat, assign it to |value|.
117  void InitFromMachineStatistic(const char* attr, std::string* value);
118
119  std::string initial_locale_;
120  std::vector<std::string> configured_locales_;
121  std::string initial_timezone_;
122  std::string keyboard_layout_;
123  std::string registration_url_;
124
125  DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
126};
127
128// OEM services customization document class.
129// ServicesCustomizationDocument is fetched from network or local file but on
130// FILE thread therefore it may not be ready just after creation. Fetching of
131// the manifest should be initiated outside this class by calling
132// StartFetching() method. User of the file should check IsReady before use it.
133class ServicesCustomizationDocument : public CustomizationDocument,
134                                      private net::URLFetcherDelegate {
135 public:
136  static ServicesCustomizationDocument* GetInstance();
137
138  // Registers preferences.
139  static void RegisterPrefs(PrefRegistrySimple* registry);
140
141  // Return true if the customization was applied. Customization is applied only
142  // once per machine.
143  static bool WasApplied();
144
145  // Start fetching customization document.
146  void StartFetching();
147
148  // Apply customization and save in machine options that customization was
149  // applied successfully. Return true if customization was applied.
150  bool ApplyCustomization();
151
152  std::string GetInitialStartPage(const std::string& locale) const;
153  std::string GetSupportPage(const std::string& locale) const;
154
155 private:
156  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, Basic);
157  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, BadManifest);
158  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, MultiLanguage);
159  friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
160
161  // C-tor for singleton construction.
162  ServicesCustomizationDocument();
163
164  // C-tor for test construction.
165  explicit ServicesCustomizationDocument(const std::string& manifest);
166
167  virtual ~ServicesCustomizationDocument();
168
169  // Save applied state in machine settings.
170  static void SetApplied(bool val);
171
172  // Overriden from net::URLFetcherDelegate:
173  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
174
175  // Initiate file fetching.
176  void StartFileFetch();
177
178  // Executes on FILE thread and reads file to string.
179  void ReadFileInBackground(const base::FilePath& file);
180
181  // Services customization manifest URL.
182  GURL url_;
183
184  // URLFetcher instance.
185  scoped_ptr<net::URLFetcher> url_fetcher_;
186
187  // Timer to retry fetching file if network is not available.
188  base::OneShotTimer<ServicesCustomizationDocument> retry_timer_;
189
190  // How many times we already tried to fetch customization manifest file.
191  int num_retries_;
192
193  DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
194};
195
196}  // namespace chromeos
197
198#endif  // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
199