customization_document.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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/memory/weak_ptr.h"
16#include "base/values.h"
17#include "net/url_request/url_fetcher_delegate.h"
18#include "url/gurl.h"
19
20class PrefRegistrySimple;
21class Profile;
22
23namespace base {
24class DictionaryValue;
25class FilePath;
26}
27
28namespace extensions {
29class ExternalLoader;
30}
31
32namespace net {
33class URLFetcher;
34}
35
36namespace user_prefs {
37class PrefRegistrySyncable;
38}
39
40// This test is in global namespace so it must be declared here.
41void Test__InitStartupCustomizationDocument(const std::string& manifest);
42
43namespace chromeos {
44
45class ServicesCustomizationExternalLoader;
46
47namespace system {
48class StatisticsProvider;
49}  // system
50
51// Base class for OEM customization document classes.
52class CustomizationDocument {
53 public:
54  virtual ~CustomizationDocument();
55
56  // Return true if the document was successfully fetched and parsed.
57  bool IsReady() const { return root_.get(); }
58
59 protected:
60  explicit CustomizationDocument(const std::string& accepted_version);
61
62  virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
63  virtual bool LoadManifestFromString(const std::string& manifest);
64
65  std::string GetLocaleSpecificString(const std::string& locale,
66                                      const std::string& dictionary_name,
67                                      const std::string& entry_name) const;
68
69  scoped_ptr<base::DictionaryValue> root_;
70
71  // Value of the "version" attribute that is supported.
72  // Otherwise config is not loaded.
73  std::string accepted_version_;
74
75 private:
76  DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
77};
78
79// OEM startup customization document class.
80// Now StartupCustomizationDocument is loaded in c-tor so just after create it
81// may be ready or not (if manifest is missing or corrupted) and this state
82// won't be changed later (i.e. IsReady() always return the same value).
83class StartupCustomizationDocument : public CustomizationDocument {
84 public:
85  static StartupCustomizationDocument* GetInstance();
86
87  std::string GetEULAPage(const std::string& locale) const;
88
89  // These methods can be called even if !IsReady(), in this case VPD values
90  // will be returned.
91  //
92  // Raw value of "initial_locale" like initial_locale="en-US,sv,da,fi,no" .
93  const std::string& initial_locale() const { return initial_locale_; }
94
95  // Vector of individual locale values.
96  const std::vector<std::string>& configured_locales() const;
97
98  // Default locale value (first value in initial_locale list).
99  const std::string& initial_locale_default() const;
100  const std::string& initial_timezone() const { return initial_timezone_; }
101  const std::string& keyboard_layout() const { return keyboard_layout_; }
102
103 private:
104  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic);
105  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD);
106  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest);
107  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, MultiLanguage);
108  friend class OobeLocalizationTest;
109  friend void ::Test__InitStartupCustomizationDocument(
110      const std::string& manifest);
111  friend struct DefaultSingletonTraits<StartupCustomizationDocument>;
112
113  // C-tor for singleton construction.
114  StartupCustomizationDocument();
115
116  // C-tor for test construction.
117  StartupCustomizationDocument(system::StatisticsProvider* provider,
118                               const std::string& manifest);
119
120  virtual ~StartupCustomizationDocument();
121
122  void Init(system::StatisticsProvider* provider);
123
124  // If |attr| exists in machine stat, assign it to |value|.
125  void InitFromMachineStatistic(const char* attr, std::string* value);
126
127  std::string initial_locale_;
128  std::vector<std::string> configured_locales_;
129  std::string initial_timezone_;
130  std::string keyboard_layout_;
131
132  DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
133};
134
135// OEM services customization document class.
136// ServicesCustomizationDocument is fetched from network therefore it is not
137// ready just after creation. Fetching of the manifest should be initiated
138// outside this class by calling StartFetching() method.
139// User of the file should check IsReady before use it.
140class ServicesCustomizationDocument : public CustomizationDocument,
141                                      private net::URLFetcherDelegate {
142 public:
143  static ServicesCustomizationDocument* GetInstance();
144
145  // Registers preferences.
146  static void RegisterPrefs(PrefRegistrySimple* registry);
147  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
148
149  static const char kManifestUrl[];
150
151  // Return true if the customization was applied. Customization is applied only
152  // once per machine.
153  static bool WasOOBECustomizationApplied();
154
155  // Start fetching customization document.
156  void StartFetching();
157
158  // Apply customization and save in machine options that customization was
159  // applied successfully. Return true if customization was applied.
160  bool ApplyOOBECustomization();
161
162  // Returns default wallpaper URL.
163  GURL GetDefaultWallpaperUrl() const;
164
165  // Returns list of default apps.
166  bool GetDefaultApps(std::vector<std::string>* ids) const;
167
168  // Creates an extensions::ExternalLoader that will provide OEM default apps.
169  // Cache of OEM default apps stored in profile preferences.
170  extensions::ExternalLoader* CreateExternalLoader(Profile* profile);
171
172  // Returns the name of the folder for OEM apps for given |locale|.
173  std::string GetOemAppsFolderName(const std::string& locale) const;
174
175  // Initialize instance of ServicesCustomizationDocument for tests that will
176  // override singleton until ShutdownForTesting is called.
177  static void InitializeForTesting();
178
179  // Remove instance of ServicesCustomizationDocument for tests.
180  static void ShutdownForTesting();
181
182 private:
183  friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
184
185  typedef std::vector<base::WeakPtr<ServicesCustomizationExternalLoader> >
186      ExternalLoaders;
187
188  // C-tor for singleton construction.
189  ServicesCustomizationDocument();
190
191  // C-tor for test construction.
192  explicit ServicesCustomizationDocument(const std::string& manifest);
193
194  virtual ~ServicesCustomizationDocument();
195
196  // Save applied state in machine settings.
197  static void SetApplied(bool val);
198
199  // Overriden from CustomizationDocument:
200  virtual bool LoadManifestFromString(const std::string& manifest) OVERRIDE;
201
202  // Overriden from net::URLFetcherDelegate:
203  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
204
205  // Initiate file fetching. Wait for online status.
206  void StartFileFetch();
207
208  // Initiate file fetching. Don't wait for online status.
209  void DoStartFileFetch();
210
211  // Executes on FILE thread and reads file to string.
212  static void ReadFileInBackground(
213      base::WeakPtr<ServicesCustomizationDocument> self,
214      const base::FilePath& file);
215
216  // Called on UI thread with results of ReadFileInBackground.
217  void OnManifesteRead(const std::string& manifest);
218
219  // Method called when manifest was successfully loaded.
220  void OnManifestLoaded();
221
222  // Returns list of default apps in ExternalProvider format.
223  static scoped_ptr<base::DictionaryValue> GetDefaultAppsInProviderFormat(
224      const base::DictionaryValue& root);
225
226  // Update cached manifest for |profile|.
227  void UpdateCachedManifest(Profile* profile);
228
229  // Customization document not found for give ID.
230  void OnCustomizationNotFound();
231
232  // Set OEM apps folder name for AppListSyncableService for |profile|.
233  void SetOemFolderName(Profile* profile, const base::DictionaryValue& root);
234
235  // Returns the name of the folder for OEM apps for given |locale|.
236  std::string GetOemAppsFolderNameImpl(
237      const std::string& locale,
238      const base::DictionaryValue& root) const;
239
240  // Services customization manifest URL.
241  GURL url_;
242
243  // URLFetcher instance.
244  scoped_ptr<net::URLFetcher> url_fetcher_;
245
246  // How many times we already tried to fetch customization manifest file.
247  int num_retries_;
248
249  // Manifest fetch is already in progress.
250  bool fetch_started_;
251
252  // Delay between checks for network online state.
253  base::TimeDelta network_delay_;
254
255  // Known external loaders.
256  ExternalLoaders external_loaders_;
257
258  // Weak factory for callbacks.
259  base::WeakPtrFactory<ServicesCustomizationDocument> weak_ptr_factory_;
260
261  DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
262};
263
264}  // namespace chromeos
265
266#endif  // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
267