| // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "chromeos/settings/system_settings_provider.h" | |
| #include "base/command_line.h" | |
| #include "base/strings/string16.h" | |
| #include "base/time/time.h" | |
| #include "base/values.h" | |
| #include "chromeos/constants/chromeos_switches.h" | |
| #include "chromeos/login/login_state/login_state.h" | |
| #include "chromeos/settings/cros_settings_names.h" | |
| namespace chromeos { | |
| namespace { | |
| // TODO(olsen): PerUserTimeZoneEnabled and FineGrainedTimeZoneDetectionEnabled | |
| // are duplicated in chrome/browser/chromeos/system/timezone_util.cc, which | |
| // is not visible from this package. Try to re-unify these functions by moving | |
| // timezone_util to src/chromeos too (out of src/chrome/browser). | |
| bool PerUserTimezoneEnabled() { | |
| return !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| switches::kDisablePerUserTimezone); | |
| } | |
| bool FineGrainedTimeZoneDetectionEnabled() { | |
| return !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| switches::kDisableFineGrainedTimeZoneDetection); | |
| } | |
| } // namespace | |
| SystemSettingsProvider::SystemSettingsProvider() | |
| : CrosSettingsProvider(CrosSettingsProvider::NotifyObserversCallback()) { | |
| Init(); | |
| } | |
| SystemSettingsProvider::SystemSettingsProvider( | |
| const NotifyObserversCallback& notify_cb) | |
| : CrosSettingsProvider(notify_cb) { | |
| Init(); | |
| } | |
| SystemSettingsProvider::~SystemSettingsProvider() { | |
| system::TimezoneSettings::GetInstance()->RemoveObserver(this); | |
| } | |
| void SystemSettingsProvider::Init() { | |
| system::TimezoneSettings* timezone_settings = | |
| system::TimezoneSettings::GetInstance(); | |
| timezone_settings->AddObserver(this); | |
| timezone_value_.reset( | |
| new base::Value(timezone_settings->GetCurrentTimezoneID())); | |
| per_user_timezone_enabled_value_.reset( | |
| new base::Value(PerUserTimezoneEnabled())); | |
| fine_grained_time_zone_enabled_value_.reset( | |
| new base::Value(FineGrainedTimeZoneDetectionEnabled())); | |
| } | |
| void SystemSettingsProvider::DoSet(const std::string& path, | |
| const base::Value& in_value) { | |
| // TODO(olsen): crbug.com/433840 - separate read path and write path. | |
| // The write path which goes through CrosSettings and SystemSettingsProvider, | |
| // should more simply go straight through TimezoneUtil. | |
| // Guest, public, or child accounts cannot change the time zone. | |
| // TODO(olsen): This logic is duplicated in TimezoneUtil::CanSetSystemTimezone | |
| // and can be removed once the write path goes through there. | |
| if (!LoginState::Get()->IsUserLoggedIn() || | |
| LoginState::Get()->IsGuestSessionUser() || | |
| LoginState::Get()->IsPublicSessionUser() || | |
| LoginState::Get()->IsChildUser()) { | |
| return; | |
| } | |
| if (path == kSystemTimezone) { | |
| base::string16 timezone_id; | |
| if (!in_value.GetAsString(&timezone_id)) | |
| return; | |
| // This will call TimezoneChanged. | |
| system::TimezoneSettings::GetInstance()->SetTimezoneFromID(timezone_id); | |
| } | |
| // kPerUserTimezoneEnabled is read-only. | |
| // kFineGrainedTimeZoneResolveEnabled is read-only. | |
| } | |
| const base::Value* SystemSettingsProvider::Get(const std::string& path) const { | |
| if (path == kSystemTimezone) | |
| return timezone_value_.get(); | |
| if (path == kPerUserTimezoneEnabled) | |
| return per_user_timezone_enabled_value_.get(); | |
| if (path == kFineGrainedTimeZoneResolveEnabled) | |
| return fine_grained_time_zone_enabled_value_.get(); | |
| return NULL; | |
| } | |
| // The timezone is always trusted. | |
| CrosSettingsProvider::TrustedStatus | |
| SystemSettingsProvider::PrepareTrustedValues(const base::Closure& cb) { | |
| return TRUSTED; | |
| } | |
| bool SystemSettingsProvider::HandlesSetting(const std::string& path) const { | |
| return path == kSystemTimezone || path == kPerUserTimezoneEnabled || | |
| path == kFineGrainedTimeZoneResolveEnabled; | |
| } | |
| void SystemSettingsProvider::TimezoneChanged(const icu::TimeZone& timezone) { | |
| // Fires system setting change notification. | |
| timezone_value_.reset( | |
| new base::Value(system::TimezoneSettings::GetTimezoneID(timezone))); | |
| NotifyObservers(kSystemTimezone); | |
| } | |
| } // namespace chromeos |