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/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
;
40 // This test is in global namespace so it must be declared here.
41 void Test__InitStartupCustomizationDocument(const std::string
& manifest
);
45 class CustomizationWallpaperDownloader
;
46 class ServicesCustomizationExternalLoader
;
49 class StatisticsProvider
;
52 // Base class for OEM customization document classes.
53 class CustomizationDocument
{
55 virtual ~CustomizationDocument();
57 // Return true if the document was successfully fetched and parsed.
58 bool IsReady() const { return root_
.get(); }
61 explicit CustomizationDocument(const std::string
& accepted_version
);
63 virtual bool LoadManifestFromFile(const base::FilePath
& manifest_path
);
64 virtual bool LoadManifestFromString(const std::string
& manifest
);
66 std::string
GetLocaleSpecificString(const std::string
& locale
,
67 const std::string
& dictionary_name
,
68 const std::string
& entry_name
) const;
70 scoped_ptr
<base::DictionaryValue
> root_
;
72 // Value of the "version" attribute that is supported.
73 // Otherwise config is not loaded.
74 std::string accepted_version_
;
77 DISALLOW_COPY_AND_ASSIGN(CustomizationDocument
);
80 // OEM startup customization document class.
81 // Now StartupCustomizationDocument is loaded in c-tor so just after create it
82 // may be ready or not (if manifest is missing or corrupted) and this state
83 // won't be changed later (i.e. IsReady() always return the same value).
84 class StartupCustomizationDocument
: public CustomizationDocument
{
86 static StartupCustomizationDocument
* GetInstance();
88 std::string
GetEULAPage(const std::string
& locale
) const;
90 // These methods can be called even if !IsReady(), in this case VPD values
93 // Raw value of "initial_locale" like initial_locale="en-US,sv,da,fi,no" .
94 const std::string
& initial_locale() const { return initial_locale_
; }
96 // Vector of individual locale values.
97 const std::vector
<std::string
>& configured_locales() const;
99 // Default locale value (first value in initial_locale list).
100 const std::string
& initial_locale_default() const;
101 const std::string
& initial_timezone() const { return initial_timezone_
; }
102 const std::string
& keyboard_layout() const { return keyboard_layout_
; }
105 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest
, Basic
);
106 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest
, VPD
);
107 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest
, BadManifest
);
108 FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest
, MultiLanguage
);
109 friend class OobeLocalizationTest
;
110 friend void ::Test__InitStartupCustomizationDocument(
111 const std::string
& manifest
);
112 friend struct DefaultSingletonTraits
<StartupCustomizationDocument
>;
114 // C-tor for singleton construction.
115 StartupCustomizationDocument();
117 // C-tor for test construction.
118 StartupCustomizationDocument(system::StatisticsProvider
* provider
,
119 const std::string
& manifest
);
121 virtual ~StartupCustomizationDocument();
123 void Init(system::StatisticsProvider
* provider
);
125 // If |attr| exists in machine stat, assign it to |value|.
126 void InitFromMachineStatistic(const char* attr
, std::string
* value
);
128 std::string initial_locale_
;
129 std::vector
<std::string
> configured_locales_
;
130 std::string initial_timezone_
;
131 std::string keyboard_layout_
;
133 DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument
);
136 // OEM services customization document class.
137 // ServicesCustomizationDocument is fetched from network therefore it is not
138 // ready just after creation. Fetching of the manifest should be initiated
139 // outside this class by calling StartFetching() or EnsureCustomizationApplied()
141 // User of the file should check IsReady before use it.
142 class ServicesCustomizationDocument
: public CustomizationDocument
,
143 private net::URLFetcherDelegate
{
145 static ServicesCustomizationDocument
* GetInstance();
147 // Registers preferences.
148 static void RegisterPrefs(PrefRegistrySimple
* registry
);
149 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
151 static const char kManifestUrl
[];
153 // Return true if the customization was applied. Customization is applied only
155 static bool WasOOBECustomizationApplied();
157 // If customization has not been applied, start fetching and applying.
158 void EnsureCustomizationApplied();
160 // Returns Closure with the EnsureCustomizationApplied() method.
161 base::Closure
EnsureCustomizationAppliedClosure();
163 // Start fetching customization document.
164 void StartFetching();
166 // Apply customization and save in machine options that customization was
167 // applied successfully. Return true if customization was applied.
168 bool ApplyOOBECustomization();
170 // Returns true if default wallpaper URL attribute found in manifest.
171 // |out_url| is set to attribute value.
172 bool GetDefaultWallpaperUrl(GURL
* out_url
) const;
174 // Returns list of default apps.
175 bool GetDefaultApps(std::vector
<std::string
>* ids
) const;
177 // Creates an extensions::ExternalLoader that will provide OEM default apps.
178 // Cache of OEM default apps stored in profile preferences.
179 extensions::ExternalLoader
* CreateExternalLoader(Profile
* profile
);
181 // Returns the name of the folder for OEM apps for given |locale|.
182 std::string
GetOemAppsFolderName(const std::string
& locale
) const;
184 // Initialize instance of ServicesCustomizationDocument for tests that will
185 // override singleton until ShutdownForTesting is called.
186 static void InitializeForTesting();
188 // Remove instance of ServicesCustomizationDocument for tests.
189 static void ShutdownForTesting();
191 // These methods are also called by WallpaperManager to get "global default"
192 // customized wallpaper path (and to init default wallpaper path from it)
193 // before first wallpaper is shown.
194 static base::FilePath
GetCustomizedWallpaperCacheDir();
195 static base::FilePath
GetCustomizedWallpaperDownloadedFileName();
198 friend struct DefaultSingletonTraits
<ServicesCustomizationDocument
>;
200 typedef std::vector
<base::WeakPtr
<ServicesCustomizationExternalLoader
> >
203 // Guard for a single application task (wallpaper downloading, for example).
206 // C-tor for singleton construction.
207 ServicesCustomizationDocument();
209 // C-tor for test construction.
210 explicit ServicesCustomizationDocument(const std::string
& manifest
);
212 virtual ~ServicesCustomizationDocument();
214 // Save applied state in machine settings.
215 static void SetApplied(bool val
);
217 // Overriden from CustomizationDocument:
218 virtual bool LoadManifestFromString(const std::string
& manifest
) OVERRIDE
;
220 // Overriden from net::URLFetcherDelegate:
221 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
223 // Initiate file fetching. Wait for online status.
224 void StartFileFetch();
226 // Initiate file fetching. Don't wait for online status.
227 void DoStartFileFetch();
229 // Executes on FILE thread and reads file to string.
230 static void ReadFileInBackground(
231 base::WeakPtr
<ServicesCustomizationDocument
> self
,
232 const base::FilePath
& file
);
234 // Called on UI thread with results of ReadFileInBackground.
235 void OnManifesteRead(const std::string
& manifest
);
237 // Method called when manifest was successfully loaded.
238 void OnManifestLoaded();
240 // Returns list of default apps in ExternalProvider format.
241 static scoped_ptr
<base::DictionaryValue
> GetDefaultAppsInProviderFormat(
242 const base::DictionaryValue
& root
);
244 // Update cached manifest for |profile|.
245 void UpdateCachedManifest(Profile
* profile
);
247 // Customization document not found for give ID.
248 void OnCustomizationNotFound();
250 // Set OEM apps folder name for AppListSyncableService for |profile|.
251 void SetOemFolderName(Profile
* profile
, const base::DictionaryValue
& root
);
253 // Returns the name of the folder for OEM apps for given |locale|.
254 std::string
GetOemAppsFolderNameImpl(
255 const std::string
& locale
,
256 const base::DictionaryValue
& root
) const;
258 // Start download of wallpaper image if needed.
259 void StartOEMWallpaperDownload(const GURL
& wallpaper_url
,
260 scoped_ptr
<ApplyingTask
> applying
);
262 // Check that current customized wallpaper cache exists. Once wallpaper is
263 // downloaded, it's never updated (even if manifest is re-fetched).
264 // Start wallpaper download if needed.
265 void CheckAndApplyWallpaper();
267 // Intermediate function to pass the result of PathExists to ApplyWallpaper.
268 void OnCheckedWallpaperCacheExists(scoped_ptr
<bool> exists
,
269 scoped_ptr
<ApplyingTask
> applying
);
271 // Called after downloaded wallpaper has been checked.
272 void ApplyWallpaper(bool default_wallpaper_file_exists
,
273 scoped_ptr
<ApplyingTask
> applying
);
275 // Set Shell default wallpaper to customized.
276 // It's wrapped as a callback and passed as a parameter to
277 // CustomizationWallpaperDownloader.
278 void OnOEMWallpaperDownloaded(scoped_ptr
<ApplyingTask
> applying
,
280 const GURL
& wallpaper_url
);
282 // Register one of Customization applying tasks.
283 void ApplyingTaskStarted();
285 // Mark task finished and check for "all customization applied".
286 void ApplyingTaskFinished(bool success
);
288 // Services customization manifest URL.
291 // URLFetcher instance.
292 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
294 // How many times we already tried to fetch customization manifest file.
297 // Manifest fetch is already in progress.
300 // Delay between checks for network online state.
301 base::TimeDelta network_delay_
;
303 // Known external loaders.
304 ExternalLoaders external_loaders_
;
306 scoped_ptr
<CustomizationWallpaperDownloader
> wallpaper_downloader_
;
308 // This is barrier until customization is applied.
309 // When number of finished tasks match number of started - customization is
311 size_t apply_tasks_started_
;
312 size_t apply_tasks_finished_
;
314 // This is the number of successfully finished customization tasks.
315 // If it matches number of tasks finished - customization is applied
317 size_t apply_tasks_success_
;
319 // Weak factory for callbacks.
320 base::WeakPtrFactory
<ServicesCustomizationDocument
> weak_ptr_factory_
;
322 DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument
);
325 } // namespace chromeos
327 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_