Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / customization_document.h
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
20 class PrefRegistrySimple;
21 class Profile;
22
23 namespace base {
24 class DictionaryValue;
25 class FilePath;
26 }
27
28 namespace extensions {
29 class ExternalLoader;
30 }
31
32 namespace net {
33 class URLFetcher;
34 }
35
36 namespace user_prefs {
37 class PrefRegistrySyncable;
38 }
39
40 // This test is in global namespace so it must be declared here.
41 void Test__InitStartupCustomizationDocument(const std::string& manifest);
42
43 namespace chromeos {
44
45 class CustomizationWallpaperDownloader;
46 class ServicesCustomizationExternalLoader;
47
48 namespace system {
49 class StatisticsProvider;
50 }  // system
51
52 // Base class for OEM customization document classes.
53 class CustomizationDocument {
54  public:
55   virtual ~CustomizationDocument();
56
57   // Return true if the document was successfully fetched and parsed.
58   bool IsReady() const { return root_.get(); }
59
60  protected:
61   explicit CustomizationDocument(const std::string& accepted_version);
62
63   virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
64   virtual bool LoadManifestFromString(const std::string& manifest);
65
66   std::string GetLocaleSpecificString(const std::string& locale,
67                                       const std::string& dictionary_name,
68                                       const std::string& entry_name) const;
69
70   scoped_ptr<base::DictionaryValue> root_;
71
72   // Value of the "version" attribute that is supported.
73   // Otherwise config is not loaded.
74   std::string accepted_version_;
75
76  private:
77   DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
78 };
79
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 {
85  public:
86   static StartupCustomizationDocument* GetInstance();
87
88   std::string GetEULAPage(const std::string& locale) const;
89
90   // These methods can be called even if !IsReady(), in this case VPD values
91   // will be returned.
92   //
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_; }
95
96   // Vector of individual locale values.
97   const std::vector<std::string>& configured_locales() const;
98
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_; }
103
104  private:
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>;
113
114   // C-tor for singleton construction.
115   StartupCustomizationDocument();
116
117   // C-tor for test construction.
118   StartupCustomizationDocument(system::StatisticsProvider* provider,
119                                const std::string& manifest);
120
121   virtual ~StartupCustomizationDocument();
122
123   void Init(system::StatisticsProvider* provider);
124
125   // If |attr| exists in machine stat, assign it to |value|.
126   void InitFromMachineStatistic(const char* attr, std::string* value);
127
128   std::string initial_locale_;
129   std::vector<std::string> configured_locales_;
130   std::string initial_timezone_;
131   std::string keyboard_layout_;
132
133   DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
134 };
135
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()
140 // methods.
141 // User of the file should check IsReady before use it.
142 class ServicesCustomizationDocument : public CustomizationDocument,
143                                       private net::URLFetcherDelegate {
144  public:
145   static ServicesCustomizationDocument* GetInstance();
146
147   // Registers preferences.
148   static void RegisterPrefs(PrefRegistrySimple* registry);
149   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
150
151   static const char kManifestUrl[];
152
153   // Return true if the customization was applied. Customization is applied only
154   // once per machine.
155   static bool WasOOBECustomizationApplied();
156
157   // If customization has not been applied, start fetching and applying.
158   void EnsureCustomizationApplied();
159
160   // Returns Closure with the EnsureCustomizationApplied() method.
161   base::Closure EnsureCustomizationAppliedClosure();
162
163   // Start fetching customization document.
164   void StartFetching();
165
166   // Apply customization and save in machine options that customization was
167   // applied successfully. Return true if customization was applied.
168   bool ApplyOOBECustomization();
169
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;
173
174   // Returns list of default apps.
175   bool GetDefaultApps(std::vector<std::string>* ids) const;
176
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);
180
181   // Returns the name of the folder for OEM apps for given |locale|.
182   std::string GetOemAppsFolderName(const std::string& locale) const;
183
184   // Initialize instance of ServicesCustomizationDocument for tests that will
185   // override singleton until ShutdownForTesting is called.
186   static void InitializeForTesting();
187
188   // Remove instance of ServicesCustomizationDocument for tests.
189   static void ShutdownForTesting();
190
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();
196
197  private:
198   friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
199
200   typedef std::vector<base::WeakPtr<ServicesCustomizationExternalLoader> >
201       ExternalLoaders;
202
203   // Guard for a single application task (wallpaper downloading, for example).
204   class ApplyingTask;
205
206   // C-tor for singleton construction.
207   ServicesCustomizationDocument();
208
209   // C-tor for test construction.
210   explicit ServicesCustomizationDocument(const std::string& manifest);
211
212   virtual ~ServicesCustomizationDocument();
213
214   // Save applied state in machine settings.
215   static void SetApplied(bool val);
216
217   // Overriden from CustomizationDocument:
218   virtual bool LoadManifestFromString(const std::string& manifest) OVERRIDE;
219
220   // Overriden from net::URLFetcherDelegate:
221   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
222
223   // Initiate file fetching. Wait for online status.
224   void StartFileFetch();
225
226   // Initiate file fetching. Don't wait for online status.
227   void DoStartFileFetch();
228
229   // Executes on FILE thread and reads file to string.
230   static void ReadFileInBackground(
231       base::WeakPtr<ServicesCustomizationDocument> self,
232       const base::FilePath& file);
233
234   // Called on UI thread with results of ReadFileInBackground.
235   void OnManifesteRead(const std::string& manifest);
236
237   // Method called when manifest was successfully loaded.
238   void OnManifestLoaded();
239
240   // Returns list of default apps in ExternalProvider format.
241   static scoped_ptr<base::DictionaryValue> GetDefaultAppsInProviderFormat(
242       const base::DictionaryValue& root);
243
244   // Update cached manifest for |profile|.
245   void UpdateCachedManifest(Profile* profile);
246
247   // Customization document not found for give ID.
248   void OnCustomizationNotFound();
249
250   // Set OEM apps folder name for AppListSyncableService for |profile|.
251   void SetOemFolderName(Profile* profile, const base::DictionaryValue& root);
252
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;
257
258   // Start download of wallpaper image if needed.
259   void StartOEMWallpaperDownload(const GURL& wallpaper_url,
260                                  scoped_ptr<ApplyingTask> applying);
261
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();
266
267   // Intermediate function to pass the result of PathExists to ApplyWallpaper.
268   void OnCheckedWallpaperCacheExists(scoped_ptr<bool> exists,
269                                      scoped_ptr<ApplyingTask> applying);
270
271   // Called after downloaded wallpaper has been checked.
272   void ApplyWallpaper(bool default_wallpaper_file_exists,
273                       scoped_ptr<ApplyingTask> applying);
274
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,
279                                 bool success,
280                                 const GURL& wallpaper_url);
281
282   // Register one of Customization applying tasks.
283   void ApplyingTaskStarted();
284
285   // Mark task finished and check for "all customization applied".
286   void ApplyingTaskFinished(bool success);
287
288   // Services customization manifest URL.
289   GURL url_;
290
291   // URLFetcher instance.
292   scoped_ptr<net::URLFetcher> url_fetcher_;
293
294   // How many times we already tried to fetch customization manifest file.
295   int num_retries_;
296
297   // Manifest fetch is already in progress.
298   bool fetch_started_;
299
300   // Delay between checks for network online state.
301   base::TimeDelta network_delay_;
302
303   // Known external loaders.
304   ExternalLoaders external_loaders_;
305
306   scoped_ptr<CustomizationWallpaperDownloader> wallpaper_downloader_;
307
308   // This is barrier until customization is applied.
309   // When number of finished tasks match number of started - customization is
310   // applied.
311   size_t apply_tasks_started_;
312   size_t apply_tasks_finished_;
313
314   // This is the number of successfully finished customization tasks.
315   // If it matches number of tasks finished - customization is applied
316   // successfully.
317   size_t apply_tasks_success_;
318
319   // Weak factory for callbacks.
320   base::WeakPtrFactory<ServicesCustomizationDocument> weak_ptr_factory_;
321
322   DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
323 };
324
325 }  // namespace chromeos
326
327 #endif  // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_