Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / customization_document.h
blob09b0b56776997529c118ff5bdded7e9047b360f1
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_
8 #include <string>
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"
19 class PrefRegistrySimple;
21 namespace base {
22 class DictionaryValue;
23 class FilePath;
26 namespace net {
27 class URLFetcher;
30 namespace chromeos {
32 namespace system {
33 class StatisticsProvider;
34 } // system
36 // Base class for OEM customization document classes.
37 class CustomizationDocument {
38 public:
39 virtual ~CustomizationDocument();
41 // Return true if the document was successfully fetched and parsed.
42 bool IsReady() const { return root_.get(); }
44 protected:
45 explicit CustomizationDocument(const std::string& accepted_version);
47 virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
48 virtual bool LoadManifestFromString(const std::string& manifest);
50 std::string GetLocaleSpecificString(const std::string& locale,
51 const std::string& dictionary_name,
52 const std::string& entry_name) const;
54 scoped_ptr<base::DictionaryValue> root_;
56 // Value of the "version" attribute that is supported.
57 // Otherwise config is not loaded.
58 std::string accepted_version_;
60 private:
61 DISALLOW_COPY_AND_ASSIGN(CustomizationDocument);
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();
72 std::string GetHelpPage(const std::string& locale) const;
73 std::string GetEULAPage(const std::string& locale) const;
75 const std::string& registration_url() const { return registration_url_; }
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_; }
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>;
89 // C-tor for singleton construction.
90 StartupCustomizationDocument();
92 // C-tor for test construction.
93 StartupCustomizationDocument(system::StatisticsProvider* provider,
94 const std::string& manifest);
96 virtual ~StartupCustomizationDocument();
98 void Init(system::StatisticsProvider* provider);
100 // If |attr| exists in machine stat, assign it to |value|.
101 void InitFromMachineStatistic(const char* attr, std::string* value);
103 std::string initial_locale_;
104 std::string initial_timezone_;
105 std::string keyboard_layout_;
106 std::string registration_url_;
108 DISALLOW_COPY_AND_ASSIGN(StartupCustomizationDocument);
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();
121 // Registers preferences.
122 static void RegisterPrefs(PrefRegistrySimple* registry);
124 // Return true if the customization was applied. Customization is applied only
125 // once per machine.
126 static bool WasApplied();
128 // Start fetching customization document.
129 void StartFetching();
131 // Apply customization and save in machine options that customization was
132 // applied successfully. Return true if customization was applied.
133 bool ApplyCustomization();
135 std::string GetInitialStartPage(const std::string& locale) const;
136 std::string GetSupportPage(const std::string& locale) const;
138 private:
139 FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, Basic);
140 FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, BadManifest);
141 friend struct DefaultSingletonTraits<ServicesCustomizationDocument>;
143 // C-tor for singleton construction.
144 ServicesCustomizationDocument();
146 // C-tor for test construction.
147 explicit ServicesCustomizationDocument(const std::string& manifest);
149 virtual ~ServicesCustomizationDocument();
151 // Save applied state in machine settings.
152 static void SetApplied(bool val);
154 // Overriden from net::URLFetcherDelegate:
155 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
157 // Initiate file fetching.
158 void StartFileFetch();
160 // Executes on FILE thread and reads file to string.
161 void ReadFileInBackground(const base::FilePath& file);
163 // Services customization manifest URL.
164 GURL url_;
166 // URLFetcher instance.
167 scoped_ptr<net::URLFetcher> url_fetcher_;
169 // Timer to retry fetching file if network is not available.
170 base::OneShotTimer<ServicesCustomizationDocument> retry_timer_;
172 // How many times we already tried to fetch customization manifest file.
173 int num_retries_;
175 DISALLOW_COPY_AND_ASSIGN(ServicesCustomizationDocument);
178 } // namespace chromeos
180 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_DOCUMENT_H_