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 10 #include "base/compiler_specific.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/singleton.h" 14 #include "base/timer/timer.h" 15 #include "base/values.h" 16 #include "net/url_request/url_fetcher_delegate.h" 17 #include "url/gurl.h" 18 19 class PrefRegistrySimple; 20 21 namespace base { 22 class DictionaryValue; 23 class FilePath; 24 } 25 26 namespace net { 27 class URLFetcher; 28 } 29 30 namespace chromeos { 31 32 namespace system { 33 class StatisticsProvider; 34 } // system 35 36 // Base class for OEM customization document classes. 37 class CustomizationDocument { 38 public: 39 virtual ~CustomizationDocument(); 40 41 // Return true if the document was successfully fetched and parsed. 42 bool IsReady() const { return root_.get(); } 43 44 protected: 45 explicit CustomizationDocument(const std::string& accepted_version); 46 47 virtual bool LoadManifestFromFile(const base::FilePath& manifest_path); 48 virtual bool LoadManifestFromString(const std::string& manifest); 49 50 std::string GetLocaleSpecificString(const std::string& locale, 51 const std::string& dictionary_name, 52 const std::string& entry_name) const; 53 54 scoped_ptr<base::DictionaryValue> root_; 55 56 // Value of the "version" attribute that is supported. 57 // Otherwise config is not loaded. 58 std::string accepted_version_; 59 60 private: 61 DISALLOW_COPY_AND_ASSIGN(CustomizationDocument); 62 }; 63 64 // OEM startup customization document class. 65 // Now StartupCustomizationDocument is loaded in c-tor so just after create it 66 // may be ready or not (if manifest is missing or corrupted) and this state 67 // won't be changed later (i.e. IsReady() always return the same value). 68 class StartupCustomizationDocument : public CustomizationDocument { 69 public: 70 static StartupCustomizationDocument* GetInstance(); 71 72 std::string GetHelpPage(const std::string& locale) const; 73 std::string GetEULAPage(const std::string& locale) const; 74 75 const std::string& registration_url() const { return registration_url_; } 76 77 // These methods can be called even if !IsReady(), in this case VPD values 78 // will be returned. 79 const std::string& initial_locale() const { return initial_locale_; } 80 const std::string& initial_timezone() const { return initial_timezone_; } 81 const std::string& keyboard_layout() const { return keyboard_layout_; } 82 83 private: 84 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic); 85 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD); 86 FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest); 87 friend struct DefaultSingletonTraits<StartupCustomizationDocument>; 88 89 // C-tor for singleton construction. 90 StartupCustomizationDocument(); 91 92 // C-tor for test construction. 93 StartupCustomizationDocument(system::StatisticsProvider* provider, 94 const std::string& manifest); 95 96 virtual ~StartupCustomizationDocument(); 97 98 void Init(system::StatisticsProvider* provider); 99 100 // If |attr| exists in machine stat, assign it to |value|. 101 void InitFromMachineStatistic(const char* attr, std::string* value); 102 103 std::string initial_locale_; 104 std::string initial_timezone_; 105 std::string keyboard_layout_; 106 std::string registration_url_; 107 108 DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument); 109 }; 110 111 // OEM services customization document class. 112 // ServicesCustomizationDocument is fetched from network or local file but on 113 // FILE thread therefore it may not be ready just after creation. Fetching of 114 // the manifest should be initiated outside this class by calling 115 // StartFetching() method. User of the file should check IsReady before use it. 116 class ServicesCustomizationDocument : public CustomizationDocument, 117 private net::URLFetcherDelegate { 118 public: 119 static ServicesCustomizationDocument* GetInstance(); 120 121 // Registers preferences. 122 static void RegisterPrefs(PrefRegistrySimple* registry); 123 124 // Return true if the customization was applied. Customization is applied only 125 // once per machine. 126 static bool WasApplied(); 127 128 // Start fetching customization document. 129 void StartFetching(); 130 131 // Apply customization and save in machine options that customization was 132 // applied successfully. Return true if customization was applied. 133 bool ApplyCustomization(); 134 135 std::string GetInitialStartPage(const std::string& locale) const; 136 std::string GetSupportPage(const std::string& locale) const; 137 138 private: 139 FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, Basic); 140 FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, BadManifest); 141 friend struct DefaultSingletonTraits<ServicesCustomizationDocument>; 142 143 // C-tor for singleton construction. 144 ServicesCustomizationDocument(); 145 146 // C-tor for test construction. 147 explicit ServicesCustomizationDocument(const std::string& manifest); 148 149 virtual ~ServicesCustomizationDocument(); 150 151 // Save applied state in machine settings. 152 static void SetApplied(bool val); 153 154 // Overriden from net::URLFetcherDelegate: 155 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 156 157 // Initiate file fetching. 158 void StartFileFetch(); 159 160 // Executes on FILE thread and reads file to string. 161 void ReadFileInBackground(const base::FilePath& file); 162 163 // Services customization manifest URL. 164 GURL url_; 165 166 // URLFetcher instance. 167 scoped_ptr<net::URLFetcher> url_fetcher_; 168 169 // Timer to retry fetching file if network is not available. 170 base::OneShotTimer<ServicesCustomizationDocument> retry_timer_; 171 172 // How many times we already tried to fetch customization manifest file. 173 int num_retries_; 174 175 DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument); 176 }; 177 178 } // namespace chromeos 179 180 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_ 181