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.
5 #ifndef CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
6 #define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
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"
20 class PrefRegistrySimple;
23 class DictionaryValue;
31 // This test is in global namespace so it must be declared here.
32 void Test__InitStartupCustomizationDocument(const std::string& manifest);
37 class StatisticsProvider;
40 // Base class for OEM customization document classes.
41 class CustomizationDocument {
43 virtual ~CustomizationDocument();
45 // Return true if the document was successfully fetched and parsed.
46 bool IsReady() const { return root_.get(); }
49 explicit CustomizationDocument(const std::string& accepted_version);
51 virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
52 virtual bool LoadManifestFromString(const std::string& manifest);
54 std::string GetLocaleSpecificString(const std::string& locale,
55 const std::string& dictionary_name,
56 const std::string& entry_name) const;
58 scoped_ptr<base::DictionaryValue> root_;
60 // Value of the "version" attribute that is supported.
61 // Otherwise config is not loaded.
62 std::string accepted_version_;
65 DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
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).
72 class StartupCustomizationDocument : public CustomizationDocument {
74 static StartupCustomizationDocument* GetInstance();
76 std::string GetHelpPage(const std::string& locale) const;
77 std::string GetEULAPage(const std::string& locale) const;
79 const std::string& registration_url() const { return registration_url_; }
81 // These methods can be called even if !IsReady(), in this case VPD values
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_; }
87 // Vector of individual locale values.
88 const std::vector<std::string>& configured_locales() const;
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_; }
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>;
105 // C-tor for singleton construction.
106 StartupCustomizationDocument();
108 // C-tor for test construction.
109 StartupCustomizationDocument(system::StatisticsProvider* provider,
110 const std::string& manifest);
112 virtual ~StartupCustomizationDocument();
114 void Init(system::StatisticsProvider* provider);
116 // If |attr| exists in machine stat, assign it to |value|.
117 void InitFromMachineStatistic(const char* attr, std::string* value);
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_;
125 DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
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.
133 class ServicesCustomizationDocument : public CustomizationDocument,
134 private net::URLFetcherDelegate {
136 static ServicesCustomizationDocument* GetInstance();
138 // Registers preferences.
139 static void RegisterPrefs(PrefRegistrySimple* registry);
141 // Return true if the customization was applied. Customization is applied only
143 static bool WasApplied();
145 // Start fetching customization document.
146 void StartFetching();
148 // Apply customization and save in machine options that customization was
149 // applied successfully. Return true if customization was applied.
150 bool ApplyCustomization();
152 std::string GetInitialStartPage(const std::string& locale) const;
153 std::string GetSupportPage(const std::string& locale) const;
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>;
161 // C-tor for singleton construction.
162 ServicesCustomizationDocument();
164 // C-tor for test construction.
165 explicit ServicesCustomizationDocument(const std::string& manifest);
167 virtual ~ServicesCustomizationDocument();
169 // Save applied state in machine settings.
170 static void SetApplied(bool val);
172 // Overriden from net::URLFetcherDelegate:
173 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
175 // Initiate file fetching.
176 void StartFileFetch();
178 // Executes on FILE thread and reads file to string.
179 void ReadFileInBackground(const base::FilePath& file);
181 // Services customization manifest URL.
184 // URLFetcher instance.
185 scoped_ptr<net::URLFetcher> url_fetcher_;
187 // Timer to retry fetching file if network is not available.
188 base::OneShotTimer<ServicesCustomizationDocument> retry_timer_;
190 // How many times we already tried to fetch customization manifest file.
193 DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
196 } // namespace chromeos
198 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_