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>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefRegistrySimple;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class URLFetcher;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace system {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StatisticsProvider;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // system
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Base class for OEM customization document classes.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CustomizationDocument {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~CustomizationDocument();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true if the document was successfully fetched and parsed.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsReady() const { return root_.get(); }
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit CustomizationDocument(const std::string& accepted_version);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool LoadManifestFromString(const std::string& manifest);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetLocaleSpecificString(const std::string& locale,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const std::string& dictionary_name,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const std::string& entry_name) const;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> root_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value of the "version" attribute that is supported.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise config is not loaded.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string accepted_version_;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OEM startup customization document class.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Now StartupCustomizationDocument is loaded in c-tor so just after create it
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may be ready or not (if manifest is missing or corrupted) and this state
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// won't be changed later (i.e. IsReady() always return the same value).
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StartupCustomizationDocument : public CustomizationDocument {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StartupCustomizationDocument* GetInstance();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetHelpPage(const std::string& locale) const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetEULAPage(const std::string& locale) const;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& registration_url() const { return registration_url_; }
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These methods can be called even if !IsReady(), in this case VPD values
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will be returned.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& initial_locale() const { return initial_locale_; }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& initial_timezone() const { return initial_timezone_; }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& keyboard_layout() const { return keyboard_layout_; }
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct DefaultSingletonTraits<StartupCustomizationDocument>;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for singleton construction.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartupCustomizationDocument();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for test construction.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartupCustomizationDocument(system::StatisticsProvider* provider,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const std::string& manifest);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~StartupCustomizationDocument();
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Init(system::StatisticsProvider* provider);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |attr| exists in machine stat, assign it to |value|.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitFromMachineStatistic(const char* attr, std::string* value);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string initial_locale_;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string initial_timezone_;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string keyboard_layout_;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string registration_url_;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OEM services customization document class.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ServicesCustomizationDocument is fetched from network or local file but on
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FILE thread therefore it may not be ready just after creation. Fetching of
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the manifest should be initiated outside this class by calling
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// StartFetching() method. User of the file should check IsReady before use it.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ServicesCustomizationDocument : public CustomizationDocument,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      private net::URLFetcherDelegate {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static ServicesCustomizationDocument* GetInstance();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registers preferences.
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void RegisterPrefs(PrefRegistrySimple* registry);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true if the customization was applied. Customization is applied only
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // once per machine.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool WasApplied();
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Start fetching customization document.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartFetching();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Apply customization and save in machine options that customization was
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // applied successfully. Return true if customization was applied.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ApplyCustomization();
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetInitialStartPage(const std::string& locale) const;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetSupportPage(const std::string& locale) const;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, Basic);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, BadManifest);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for singleton construction.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ServicesCustomizationDocument();
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // C-tor for test construction.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit ServicesCustomizationDocument(const std::string& manifest);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ServicesCustomizationDocument();
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Save applied state in machine settings.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void SetApplied(bool val);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden from net::URLFetcherDelegate:
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initiate file fetching.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartFileFetch();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Executes on FILE thread and reads file to string.
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ReadFileInBackground(const base::FilePath& file);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Services customization manifest URL.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url_;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLFetcher instance.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::URLFetcher> url_fetcher_;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Timer to retry fetching file if network is not available.
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::OneShotTimer<ServicesCustomizationDocument> retry_timer_;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // How many times we already tried to fetch customization manifest file.
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_retries_;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_
181