Chromium Code Reviews| Index: chrome/browser/chromeos/customization_document.cc |
| diff --git a/chrome/browser/chromeos/customization_document.cc b/chrome/browser/chromeos/customization_document.cc |
| index 33950b328a0b8c5807cf6490b702595e501730bb..c513e6e3fb725ef3c360bcb9e5502bc78b2cba3b 100644 |
| --- a/chrome/browser/chromeos/customization_document.cc |
| +++ b/chrome/browser/chromeos/customization_document.cc |
| @@ -4,12 +4,19 @@ |
| #include "chrome/browser/chromeos/customization_document.h" |
| +#include "base/file_path.h" |
| #include "base/file_util.h" |
| #include "base/json/json_reader.h" |
| #include "base/logging.h" |
| #include "base/string_tokenizer.h" |
| #include "base/string_util.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "chrome/browser/chromeos/cros/network_library.h" |
| #include "chrome/browser/chromeos/system_access.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "content/browser/browser_thread.h" |
| // Manifest attributes names. |
| @@ -34,8 +41,28 @@ const char kAcceptedManifestVersion[] = "1.0"; |
| const char kHwid[] = "hwid"; |
| +// Path to OEM partner startup customization manifest. |
| +const char kStartupCustomizationManifestPath[] = |
| + "/opt/oem/etc/startup_manifest.json"; |
| + |
| +// URL where to fetch OEM services customization manifest from. |
| +const char kServicesCustomizationManifestUrl[] = |
| + "file:///opt/oem/etc/services_manifest.json"; |
| + |
| +// Name of local state option that tracks if services customization has been |
| +// applied. |
| +const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; |
| + |
| +// Maximum number of retries to fetch file if network is not available. |
| +const int kMaxFetchRetries = 3; |
| + |
| +// Delay between file fetch retries if network is not available. |
| +const int kRetriesDelayInSec = 2; |
| + |
| } // anonymous namespace |
| +DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ServicesCustomizationDocument); |
| + |
| namespace chromeos { |
| // CustomizationDocument implementation. |
| @@ -93,18 +120,30 @@ std::string CustomizationDocument::GetLocaleSpecificString( |
| } |
| // StartupCustomizationDocument implementation. |
|
Nikita (slow)
2011/04/27 13:57:07
Please use this style here and trough the file:
/
Dmitry Polukhin
2011/04/27 14:56:15
Done.
|
| +StartupCustomizationDocument::StartupCustomizationDocument() { |
| + { |
| + // Loading manifest causes us to do blocking IO on UI thread. |
| + // Temporarily allow it until we fix http://crosbug.com/11103 |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + LoadManifestFromFile(FilePath(kStartupCustomizationManifestPath)); |
| + } |
| + Init(SystemAccess::GetInstance()); |
| +} |
| + |
| StartupCustomizationDocument::StartupCustomizationDocument( |
| - SystemAccess* system_access) |
| - : system_access_(system_access) { |
| + SystemAccess* system_access, const std::string& manifest) { |
| + LoadManifestFromString(manifest); |
|
Nikita (slow)
2011/04/27 13:57:07
Are thread restrictions applied during testing?
Dmitry Polukhin
2011/04/27 14:56:15
I think so. But in test we don't ready manifest fr
|
| + Init(system_access); |
| } |
| -bool StartupCustomizationDocument::LoadManifestFromString( |
| - const std::string& manifest) { |
| - DCHECK(system_access_); |
| +StartupCustomizationDocument* StartupCustomizationDocument::GetInstance() { |
| + return Singleton<StartupCustomizationDocument, |
| + DefaultSingletonTraits<StartupCustomizationDocument> >::get(); |
| +} |
| - if (!CustomizationDocument::LoadManifestFromString(manifest)) { |
| - return false; |
| - } |
| +void StartupCustomizationDocument::Init(SystemAccess* system_access) { |
| + if (!IsReady()) |
| + return; |
| root_->GetString(kInitialLocaleAttr, &initial_locale_); |
| root_->GetString(kInitialTimezoneAttr, &initial_timezone_); |
| @@ -112,7 +151,7 @@ bool StartupCustomizationDocument::LoadManifestFromString( |
| root_->GetString(kRegistrationUrlAttr, ®istration_url_); |
| std::string hwid; |
| - if (system_access_->GetMachineStatistic(kHwid, &hwid)) { |
| + if (system_access->GetMachineStatistic(kHwid, &hwid)) { |
| ListValue* hwid_list = NULL; |
| if (root_->GetList(kHwidMapAttr, &hwid_list)) { |
| for (size_t i = 0; i < hwid_list->GetSize(); ++i) { |
| @@ -143,14 +182,9 @@ bool StartupCustomizationDocument::LoadManifestFromString( |
| LOG(ERROR) << "HWID is missing in machine statistics"; |
| } |
| - system_access_->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_); |
| - system_access_->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_); |
| - system_access_->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_); |
| - |
| - // system_access_ is no longer used. |
| - system_access_ = NULL; |
| - |
| - return true; |
| + system_access->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_); |
| + system_access->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_); |
| + system_access->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_); |
| } |
| std::string StartupCustomizationDocument::GetHelpPage( |
| @@ -164,6 +198,101 @@ std::string StartupCustomizationDocument::GetEULAPage( |
| } |
| // ServicesCustomizationDocument implementation. |
| +ServicesCustomizationDocument::ServicesCustomizationDocument() |
| + : url_(kServicesCustomizationManifestUrl) { |
| +} |
| + |
| +ServicesCustomizationDocument::ServicesCustomizationDocument( |
| + const std::string& manifest) { |
| + LoadManifestFromString(manifest); |
|
Nikita (slow)
2011/04/27 13:57:07
Instance enters into state when call on StartFetch
Dmitry Polukhin
2011/04/27 14:56:15
Added DCHECK(url_.is_valid());
|
| +} |
| + |
| +// static |
| +ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { |
| + return Singleton<ServicesCustomizationDocument, |
| + DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); |
| +} |
| + |
| +// static |
| +void ServicesCustomizationDocument::RegisterPrefs(PrefService* local_state) { |
| + local_state->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); |
|
Nikita (slow)
2011/04/27 14:50:02
FYI http://codereview.chromium.org/6905044/
|
| +} |
| + |
| +// static |
| +bool ServicesCustomizationDocument::WasApplied() { |
| + PrefService* prefs = g_browser_process->local_state(); |
| + return prefs->GetBoolean(kServicesCustomizationAppliedPref); |
| +} |
| + |
| +// static |
| +void ServicesCustomizationDocument::SetApplied(bool val) { |
| + PrefService* prefs = g_browser_process->local_state(); |
| + prefs->SetBoolean(kServicesCustomizationAppliedPref, val); |
| +} |
| + |
| +void ServicesCustomizationDocument::StartFetching() { |
| + if (url_.SchemeIsFile()) { |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + NewRunnableMethod(this, |
| + &ServicesCustomizationDocument::ReadFileInBackground, |
| + FilePath(url_.path()))); |
| + } else { |
| + StartFileFetch(); |
| + } |
| +} |
| + |
| +void ServicesCustomizationDocument::ReadFileInBackground(const FilePath& file) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + std::string manifest; |
| + if (file_util::ReadFileToString(file, &manifest)) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod( |
| + this, |
| + &ServicesCustomizationDocument::LoadManifestFromString, |
| + manifest)); |
| + } else { |
| + VLOG(1) << "Failed to load services customization manifest from: " |
| + << file.value(); |
| + } |
| +} |
| + |
| +void ServicesCustomizationDocument::StartFileFetch() { |
| + url_fetcher_.reset(new URLFetcher(url_, URLFetcher::GET, this)); |
| + url_fetcher_->set_request_context( |
| + ProfileManager::GetDefaultProfile()->GetRequestContext()); |
| + url_fetcher_->Start(); |
| +} |
| + |
| +void ServicesCustomizationDocument::OnURLFetchComplete( |
| + const URLFetcher* source, |
| + const GURL& url, |
| + const net::URLRequestStatus& status, |
| + int response_code, |
| + const ResponseCookies& cookies, |
| + const std::string& data) { |
| + if (response_code == 200) { |
| + LoadManifestFromString(data); |
| + } else { |
| + NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); |
| + if (!network->Connected() && num_retries_ < kMaxFetchRetries) { |
| + num_retries_++; |
| + retry_timer_.Start(base::TimeDelta::FromSeconds(kRetriesDelayInSec), |
| + this, &ServicesCustomizationDocument::StartFileFetch); |
| + return; |
| + } |
| + LOG(ERROR) << "URL fetch for services customization failed:" |
| + << " response code = " << response_code |
| + << " URL = " << url.spec(); |
| + } |
| +} |
| + |
| +bool ServicesCustomizationDocument::ApplyCustmization() { |
| + // TODO(dpolukhin): apply customized apps, exts and support page. |
| + SetApplied(true); |
| + return true; |
| +} |
| + |
| std::string ServicesCustomizationDocument::GetInitialStartPage( |
| const std::string& locale) const { |
| return GetLocaleSpecificString( |