Home | History | Annotate | Download | only in settings
      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 #include "chrome/browser/chromeos/settings/system_settings_provider.h"
      6 
      7 #include "base/strings/string16.h"
      8 #include "base/time/time.h"
      9 #include "base/values.h"
     10 #include "chromeos/login/login_state.h"
     11 #include "chromeos/settings/cros_settings_names.h"
     12 #include "content/public/browser/render_process_host.h"
     13 #include "content/public/browser/render_view_host.h"
     14 #include "content/public/browser/render_widget_host.h"
     15 #include "content/public/browser/render_widget_host_iterator.h"
     16 
     17 namespace chromeos {
     18 
     19 SystemSettingsProvider::SystemSettingsProvider(
     20     const NotifyObserversCallback& notify_cb)
     21     : CrosSettingsProvider(notify_cb) {
     22   system::TimezoneSettings *timezone_settings =
     23       system::TimezoneSettings::GetInstance();
     24   timezone_settings->AddObserver(this);
     25   timezone_value_.reset(new base::StringValue(
     26       timezone_settings->GetCurrentTimezoneID()));
     27 }
     28 
     29 SystemSettingsProvider::~SystemSettingsProvider() {
     30   system::TimezoneSettings::GetInstance()->RemoveObserver(this);
     31 }
     32 
     33 void SystemSettingsProvider::DoSet(const std::string& path,
     34                                    const base::Value& in_value) {
     35   // Non-guest users can change the time zone.
     36   if (LoginState::Get()->IsGuestUser())
     37     return;
     38 
     39   if (path == kSystemTimezone) {
     40     base::string16 timezone_id;
     41     if (!in_value.GetAsString(&timezone_id))
     42       return;
     43     // This will call TimezoneChanged.
     44     system::TimezoneSettings::GetInstance()->SetTimezoneFromID(timezone_id);
     45   }
     46 }
     47 
     48 const base::Value* SystemSettingsProvider::Get(const std::string& path) const {
     49   if (path == kSystemTimezone)
     50     return timezone_value_.get();
     51   return NULL;
     52 }
     53 
     54 // The timezone is always trusted.
     55 CrosSettingsProvider::TrustedStatus
     56     SystemSettingsProvider::PrepareTrustedValues(const base::Closure& cb) {
     57   return TRUSTED;
     58 }
     59 
     60 bool SystemSettingsProvider::HandlesSetting(const std::string& path) const {
     61   return path == kSystemTimezone;
     62 }
     63 
     64 void SystemSettingsProvider::TimezoneChanged(const icu::TimeZone& timezone) {
     65   // Fires system setting change notification.
     66   timezone_value_.reset(new base::StringValue(
     67       system::TimezoneSettings::GetTimezoneID(timezone)));
     68   NotifyObservers(kSystemTimezone);
     69 
     70   // Notify renderers
     71   scoped_ptr<content::RenderWidgetHostIterator> widgets(
     72       content::RenderWidgetHost::GetRenderWidgetHosts());
     73   while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
     74     if (widget->IsRenderView()) {
     75       content::RenderViewHost* view = content::RenderViewHost::From(widget);
     76       view->NotifyTimezoneChange();
     77     }
     78   }
     79 }
     80 
     81 }  // namespace chromeos
     82