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_CUSTOMIZATION_DOCUMENT_H_
6 #define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_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/memory/weak_ptr.h"
16 #include "base/values.h"
17 #include "net/url_request/url_fetcher_delegate.h"
20 class PrefRegistrySimple
;
24 class DictionaryValue
;
28 namespace extensions
{
36 namespace user_prefs
{
37 class PrefRegistrySyncable
;
42 class CustomizationWallpaperDownloader
;
43 class ServicesCustomizationExternalLoader
;
45 void InitStartupCustomizationDocumentForTesting(const std::string
& manifest
);
48 class StatisticsProvider
;
51 // Base class for OEM customization document classes.
52 class CustomizationDocument
{
54 virtual ~CustomizationDocument();
56 // Return true if the document was successfully fetched and parsed.
57 bool IsReady() const { return root_
.get(); }
60 explicit CustomizationDocument(const std::string
& accepted_version
);
62 virtual bool LoadManifestFromFile(const base::FilePath
& manifest_path
);
63 virtual bool LoadManifestFromString(const std::string
& manifest
);
65 std::string
GetLocaleSpecificString(const std::string
& locale
,
66 const std::string
& dictionary_name
,
67 const std::string
& entry_name
) const;
69 scoped_ptr
<base::DictionaryValue
> root_
;
71 // Value of the "version" attribute that is supported.
72 // Otherwise config is not loaded.
73 std::string accepted_version_
;
76 DISALLOW_COPY_AND_ASSIGN(CustomizationDocument
);
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).
83 class StartupCustomizationDocument
: public CustomizationDocument
{
85 static StartupCustomizationDocument
* GetInstance();
87 std::string
GetEULAPage(const std::string
& locale
) const;
89 // These methods can be called even if !IsReady(), in this case VPD values
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_
; }
95 // Vector of individual locale values.
96 const std::vector
<std::string
>& configured_locales() const;
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_
; }
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 InitStartupCustomizationDocumentForTesting(
110 const std::string
& manifest
);
111 friend struct base::DefaultSingletonTraits
<StartupCustomizationDocument
>;
113 // C-tor for singleton construction.
114 StartupCustomizationDocument();
116 // C-tor for test construction.
117 StartupCustomizationDocument(system::StatisticsProvider
* provider
,
118 const std::string
& manifest
);
120 ~StartupCustomizationDocument() override
;
122 void Init(system::StatisticsProvider
* provider
);
124 // If |attr| exists in machine stat, assign it to |value|.
125 void InitFromMachineStatistic(const char* attr
, std::string
* value
);
127 std::string initial_locale_
;
128 std::vector
<std::string
> configured_locales_
;
129 std::string initial_timezone_
;
130 std::string keyboard_layout_
;
132 DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument
);
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() or EnsureCustomizationApplied()
140 // User of the file should check IsReady before use it.
141 class ServicesCustomizationDocument
: public CustomizationDocument
,
142 private net::URLFetcherDelegate
{
144 static ServicesCustomizationDocument
* GetInstance();
146 // Registers preferences.
147 static void RegisterPrefs(PrefRegistrySimple
* registry
);
148 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
150 static const char kManifestUrl
[];
152 // Return true if the customization was applied. Customization is applied only
154 static bool WasOOBECustomizationApplied();
156 // If customization has not been applied, start fetching and applying.
157 void EnsureCustomizationApplied();
159 // Returns Closure with the EnsureCustomizationApplied() method.
160 base::Closure
EnsureCustomizationAppliedClosure();
162 // Start fetching customization document.
163 void StartFetching();
165 // Apply customization and save in machine options that customization was
166 // applied successfully. Return true if customization was applied.
167 bool ApplyOOBECustomization();
169 // Returns true if default wallpaper URL attribute found in manifest.
170 // |out_url| is set to attribute value.
171 bool GetDefaultWallpaperUrl(GURL
* out_url
) const;
173 // Returns list of default apps.
174 scoped_ptr
<base::DictionaryValue
> GetDefaultApps() const;
176 // Creates an extensions::ExternalLoader that will provide OEM default apps.
177 // Cache of OEM default apps stored in profile preferences.
178 extensions::ExternalLoader
* CreateExternalLoader(Profile
* profile
);
180 // Returns the name of the folder for OEM apps for given |locale|.
181 std::string
GetOemAppsFolderName(const std::string
& locale
) const;
183 // Initialize instance of ServicesCustomizationDocument for tests that will
184 // override singleton until ShutdownForTesting is called.
185 static void InitializeForTesting();
187 // Remove instance of ServicesCustomizationDocument for tests.
188 static void ShutdownForTesting();
190 // These methods are also called by WallpaperManager to get "global default"
191 // customized wallpaper path (and to init default wallpaper path from it)
192 // before first wallpaper is shown.
193 static base::FilePath
GetCustomizedWallpaperCacheDir();
194 static base::FilePath
GetCustomizedWallpaperDownloadedFileName();
196 CustomizationWallpaperDownloader
* wallpaper_downloader_for_testing() {
197 return wallpaper_downloader_
.get();
201 friend struct base::DefaultSingletonTraits
<ServicesCustomizationDocument
>;
202 FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest
,
203 OEMWallpaperIsPresent
);
204 FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest
,
205 OEMWallpaperRetryFetch
);
207 typedef std::vector
<base::WeakPtr
<ServicesCustomizationExternalLoader
> >
210 // Guard for a single application task (wallpaper downloading, for example).
213 // C-tor for singleton construction.
214 ServicesCustomizationDocument();
216 // C-tor for test construction.
217 explicit ServicesCustomizationDocument(const std::string
& manifest
);
219 ~ServicesCustomizationDocument() override
;
221 // Save applied state in machine settings.
222 static void SetApplied(bool val
);
224 // Overriden from CustomizationDocument:
225 bool LoadManifestFromString(const std::string
& manifest
) override
;
227 // Overriden from net::URLFetcherDelegate:
228 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
230 // Initiate file fetching. Wait for online status.
231 void StartFileFetch();
233 // Initiate file fetching. Don't wait for online status.
234 void DoStartFileFetch();
236 // Executes on FILE thread and reads file to string.
237 static void ReadFileInBackground(
238 base::WeakPtr
<ServicesCustomizationDocument
> self
,
239 const base::FilePath
& file
);
241 // Called on UI thread with results of ReadFileInBackground.
242 void OnManifesteRead(const std::string
& manifest
);
244 // Method called when manifest was successfully loaded.
245 void OnManifestLoaded();
247 // Returns list of default apps in ExternalProvider format.
248 static scoped_ptr
<base::DictionaryValue
> GetDefaultAppsInProviderFormat(
249 const base::DictionaryValue
& root
);
251 // Update cached manifest for |profile|.
252 void UpdateCachedManifest(Profile
* profile
);
254 // Customization document not found for give ID.
255 void OnCustomizationNotFound();
257 // Set OEM apps folder name for AppListSyncableService for |profile|.
258 void SetOemFolderName(Profile
* profile
, const base::DictionaryValue
& root
);
260 // Returns the name of the folder for OEM apps for given |locale|.
261 std::string
GetOemAppsFolderNameImpl(
262 const std::string
& locale
,
263 const base::DictionaryValue
& root
) const;
265 // Start download of wallpaper image if needed.
266 void StartOEMWallpaperDownload(const GURL
& wallpaper_url
,
267 scoped_ptr
<ApplyingTask
> applying
);
269 // Check that current customized wallpaper cache exists. Once wallpaper is
270 // downloaded, it's never updated (even if manifest is re-fetched).
271 // Start wallpaper download if needed.
272 void CheckAndApplyWallpaper();
274 // Intermediate function to pass the result of PathExists to ApplyWallpaper.
275 void OnCheckedWallpaperCacheExists(scoped_ptr
<bool> exists
,
276 scoped_ptr
<ApplyingTask
> applying
);
278 // Called after downloaded wallpaper has been checked.
279 void ApplyWallpaper(bool default_wallpaper_file_exists
,
280 scoped_ptr
<ApplyingTask
> applying
);
282 // Set Shell default wallpaper to customized.
283 // It's wrapped as a callback and passed as a parameter to
284 // CustomizationWallpaperDownloader.
285 void OnOEMWallpaperDownloaded(scoped_ptr
<ApplyingTask
> applying
,
287 const GURL
& wallpaper_url
);
289 // Register one of Customization applying tasks.
290 void ApplyingTaskStarted();
292 // Mark task finished and check for "all customization applied".
293 void ApplyingTaskFinished(bool success
);
295 // Services customization manifest URL.
298 // URLFetcher instance.
299 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
301 // How many times we already tried to fetch customization manifest file.
304 // Manifest fetch is already in progress.
307 // Delay between checks for network online state.
308 base::TimeDelta network_delay_
;
310 // Known external loaders.
311 ExternalLoaders external_loaders_
;
313 scoped_ptr
<CustomizationWallpaperDownloader
> wallpaper_downloader_
;
315 // This is barrier until customization is applied.
316 // When number of finished tasks match number of started - customization is
318 size_t apply_tasks_started_
;
319 size_t apply_tasks_finished_
;
321 // This is the number of successfully finished customization tasks.
322 // If it matches number of tasks finished - customization is applied
324 size_t apply_tasks_success_
;
326 // Weak factory for callbacks.
327 base::WeakPtrFactory
<ServicesCustomizationDocument
> weak_ptr_factory_
;
329 DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument
);
332 } // namespace chromeos
334 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_CUSTOMIZATION_DOCUMENT_H_