customization_document.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefRegistrySimple;
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Profile;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace extensions {
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ExternalLoader;
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class URLFetcher;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace user_prefs {
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class PrefRegistrySyncable;
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This test is in global namespace so it must be declared here.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void Test__InitStartupCustomizationDocument(const std::string& manifest);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass CustomizationWallpaperDownloader;
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ServicesCustomizationExternalLoader;
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace system {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StatisticsProvider;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // system
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Base class for OEM customization document classes.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CustomizationDocument {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~CustomizationDocument();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true if the document was successfully fetched and parsed.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsReady() const { return root_.get(); }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit CustomizationDocument(const std::string& accepted_version);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool LoadManifestFromString(const std::string& manifest);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetLocaleSpecificString(const std::string& locale,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const std::string& dictionary_name,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const std::string& entry_name) const;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> root_;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value of the "version" attribute that is supported.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise config is not loaded.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string accepted_version_;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OEM startup customization document class.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Now StartupCustomizationDocument is loaded in c-tor so just after create it
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may be ready or not (if manifest is missing or corrupted) and this state
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// won't be changed later (i.e. IsReady() always return the same value).
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StartupCustomizationDocument : public CustomizationDocument {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StartupCustomizationDocument* GetInstance();
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetEULAPage(const std::string& locale) const;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These methods can be called even if !IsReady(), in this case VPD values
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will be returned.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Raw value of "initial_locale" like initial_locale="en-US,sv,da,fi,no" .
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& initial_locale() const { return initial_locale_; }
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Vector of individual locale values.
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::vector<std::string>& configured_locales() const;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Default locale value (first value in initial_locale list).
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& initial_locale_default() const;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& initial_timezone() const { return initial_timezone_; }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& keyboard_layout() const { return keyboard_layout_; }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest);
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, MultiLanguage);
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class OobeLocalizationTest;
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend void ::Test__InitStartupCustomizationDocument(
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& manifest);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct DefaultSingletonTraits<StartupCustomizationDocument>;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for singleton construction.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartupCustomizationDocument();
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for test construction.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartupCustomizationDocument(system::StatisticsProvider* provider,
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const std::string& manifest);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~StartupCustomizationDocument();
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Init(system::StatisticsProvider* provider);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |attr| exists in machine stat, assign it to |value|.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitFromMachineStatistic(const char* attr, std::string* value);
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string initial_locale_;
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<std::string> configured_locales_;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string initial_timezone_;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string keyboard_layout_;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OEM services customization document class.
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// ServicesCustomizationDocument is fetched from network therefore it is not
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// ready just after creation. Fetching of the manifest should be initiated
1390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// outside this class by calling StartFetching() or EnsureCustomizationApplied()
1400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// methods.
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// User of the file should check IsReady before use it.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ServicesCustomizationDocument : public CustomizationDocument,
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      private net::URLFetcherDelegate {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static ServicesCustomizationDocument* GetInstance();
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registers preferences.
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void RegisterPrefs(PrefRegistrySimple* registry);
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static const char kManifestUrl[];
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true if the customization was applied. Customization is applied only
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // once per machine.
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool WasOOBECustomizationApplied();
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // If customization has not been applied, start fetching and applying.
1580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void EnsureCustomizationApplied();
1590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Returns Closure with the EnsureCustomizationApplied() method.
1610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::Closure EnsureCustomizationAppliedClosure();
1620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Start fetching customization document.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartFetching();
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Apply customization and save in machine options that customization was
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // applied successfully. Return true if customization was applied.
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool ApplyOOBECustomization();
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Returns true if default wallpaper URL attribute found in manifest.
1710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // |out_url| is set to attribute value.
1720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  bool GetDefaultWallpaperUrl(GURL* out_url) const;
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns list of default apps.
175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool GetDefaultApps(std::vector<std::string>* ids) const;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Creates an extensions::ExternalLoader that will provide OEM default apps.
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Cache of OEM default apps stored in profile preferences.
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  extensions::ExternalLoader* CreateExternalLoader(Profile* profile);
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
181effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Returns the name of the folder for OEM apps for given |locale|.
182effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  std::string GetOemAppsFolderName(const std::string& locale) const;
183effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
184effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Initialize instance of ServicesCustomizationDocument for tests that will
185effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // override singleton until ShutdownForTesting is called.
186effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  static void InitializeForTesting();
187effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
188effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Remove instance of ServicesCustomizationDocument for tests.
189effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  static void ShutdownForTesting();
190effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // These methods are also called by WallpaperManager to get "global default"
1920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // customized wallpaper path (and to init default wallpaper path from it)
1930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // before first wallpaper is shown.
1940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static base::FilePath GetCustomizedWallpaperCacheDir();
1950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static base::FilePath GetCustomizedWallpaperDownloadedFileName();
1960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CustomizationWallpaperDownloader* wallpaper_downloader_for_testing() {
198cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return wallpaper_downloader_.get();
199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest,
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           OEMWallpaperIsPresent);
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest,
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           OEMWallpaperRetryFetch);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  typedef std::vector<base::WeakPtr<ServicesCustomizationExternalLoader> >
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ExternalLoaders;
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Guard for a single application task (wallpaper downloading, for example).
2120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  class ApplyingTask;
2130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for singleton construction.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ServicesCustomizationDocument();
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for test construction.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit ServicesCustomizationDocument(const std::string& manifest);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ServicesCustomizationDocument();
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Save applied state in machine settings.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void SetApplied(bool val);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Overriden from CustomizationDocument:
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool LoadManifestFromString(const std::string& manifest) OVERRIDE;
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden from net::URLFetcherDelegate:
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
231effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Initiate file fetching. Wait for online status.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartFileFetch();
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
234effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Initiate file fetching. Don't wait for online status.
235effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void DoStartFileFetch();
236effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Executes on FILE thread and reads file to string.
238effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  static void ReadFileInBackground(
239effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::WeakPtr<ServicesCustomizationDocument> self,
240effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      const base::FilePath& file);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Called on UI thread with results of ReadFileInBackground.
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void OnManifesteRead(const std::string& manifest);
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Method called when manifest was successfully loaded.
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void OnManifestLoaded();
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns list of default apps in ExternalProvider format.
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static scoped_ptr<base::DictionaryValue> GetDefaultAppsInProviderFormat(
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const base::DictionaryValue& root);
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Update cached manifest for |profile|.
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void UpdateCachedManifest(Profile* profile);
254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
255effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Customization document not found for give ID.
256effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void OnCustomizationNotFound();
257effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
258effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Set OEM apps folder name for AppListSyncableService for |profile|.
259effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void SetOemFolderName(Profile* profile, const base::DictionaryValue& root);
260effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
261effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Returns the name of the folder for OEM apps for given |locale|.
262effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  std::string GetOemAppsFolderNameImpl(
263effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      const std::string& locale,
264effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      const base::DictionaryValue& root) const;
265effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Start download of wallpaper image if needed.
2670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void StartOEMWallpaperDownload(const GURL& wallpaper_url,
2680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                 scoped_ptr<ApplyingTask> applying);
2690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Check that current customized wallpaper cache exists. Once wallpaper is
2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // downloaded, it's never updated (even if manifest is re-fetched).
2720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Start wallpaper download if needed.
2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void CheckAndApplyWallpaper();
2740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Intermediate function to pass the result of PathExists to ApplyWallpaper.
2760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void OnCheckedWallpaperCacheExists(scoped_ptr<bool> exists,
2770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                     scoped_ptr<ApplyingTask> applying);
2780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Called after downloaded wallpaper has been checked.
2800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void ApplyWallpaper(bool default_wallpaper_file_exists,
2810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                      scoped_ptr<ApplyingTask> applying);
2820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Set Shell default wallpaper to customized.
2840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // It's wrapped as a callback and passed as a parameter to
2850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // CustomizationWallpaperDownloader.
2860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void OnOEMWallpaperDownloaded(scoped_ptr<ApplyingTask> applying,
2870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                bool success,
2880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                const GURL& wallpaper_url);
2890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Register one of Customization applying tasks.
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void ApplyingTaskStarted();
2920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Mark task finished and check for "all customization applied".
2940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void ApplyingTaskFinished(bool success);
2950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Services customization manifest URL.
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url_;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLFetcher instance.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::URLFetcher> url_fetcher_;
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // How many times we already tried to fetch customization manifest file.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_retries_;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Manifest fetch is already in progress.
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool fetch_started_;
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
308effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Delay between checks for network online state.
309effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::TimeDelta network_delay_;
310effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Known external loaders.
312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ExternalLoaders external_loaders_;
313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_ptr<CustomizationWallpaperDownloader> wallpaper_downloader_;
3150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // This is barrier until customization is applied.
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // When number of finished tasks match number of started - customization is
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // applied.
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  size_t apply_tasks_started_;
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  size_t apply_tasks_finished_;
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // This is the number of successfully finished customization tasks.
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // If it matches number of tasks finished - customization is applied
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // successfully.
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  size_t apply_tasks_success_;
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
327effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Weak factory for callbacks.
328effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::WeakPtrFactory<ServicesCustomizationDocument> weak_ptr_factory_;
329effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
336