• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/ui/webui/options/chromeos/core_chromeos_options_handler.h"
6 
7 #include <string>
8 
9 #include "base/string_number_conversions.h"
10 #include "base/string_util.h"
11 #include "chrome/browser/chromeos/cros_settings.h"
12 #include "chrome/browser/metrics/user_metrics.h"
13 #include "content/common/notification_details.h"
14 #include "content/common/notification_source.h"
15 
16 namespace chromeos {
17 
CoreChromeOSOptionsHandler()18 CoreChromeOSOptionsHandler::CoreChromeOSOptionsHandler()
19     : handling_change_(false) {
20 }
21 
FetchPref(const std::string & pref_name)22 Value* CoreChromeOSOptionsHandler::FetchPref(const std::string& pref_name) {
23   if (!CrosSettings::IsCrosSettings(pref_name))
24     return ::CoreOptionsHandler::FetchPref(pref_name);
25 
26   Value* pref_value = NULL;
27   CrosSettings::Get()->Get(pref_name, &pref_value);
28   return pref_value ? pref_value : Value::CreateNullValue();
29 }
30 
ObservePref(const std::string & pref_name)31 void CoreChromeOSOptionsHandler::ObservePref(const std::string& pref_name) {
32   if (!CrosSettings::IsCrosSettings(pref_name))
33     return ::CoreOptionsHandler::ObservePref(pref_name);
34 
35   // TODO(xiyuan): Change this when CrosSettings supports observers.
36   CrosSettings::Get()->AddSettingsObserver(pref_name.c_str(), this);
37 }
38 
SetPref(const std::string & pref_name,const Value * value,const std::string & metric)39 void CoreChromeOSOptionsHandler::SetPref(const std::string& pref_name,
40                                          const Value* value,
41                                          const std::string& metric) {
42   if (!CrosSettings::IsCrosSettings(pref_name))
43     return ::CoreOptionsHandler::SetPref(pref_name, value, metric);
44   handling_change_ = true;
45   // CrosSettings takes ownership of its value so we need to copy it.
46   Value* pref_value = value->DeepCopy();
47   CrosSettings::Get()->Set(pref_name, pref_value);
48   handling_change_ = false;
49 
50   ProcessUserMetric(value, metric);
51 }
52 
StopObservingPref(const std::string & path)53 void CoreChromeOSOptionsHandler::StopObservingPref(const std::string& path) {
54   // Unregister this instance from observing prefs of chrome os settings.
55   if (CrosSettings::IsCrosSettings(path))
56     CrosSettings::Get()->RemoveSettingsObserver(path.c_str(), this);
57   else  // Call base class to handle regular preferences.
58     ::CoreOptionsHandler::StopObservingPref(path);
59 }
60 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)61 void CoreChromeOSOptionsHandler::Observe(NotificationType type,
62                                          const NotificationSource& source,
63                                          const NotificationDetails& details) {
64   // Ignore the notification if this instance had caused it.
65   if (handling_change_)
66     return;
67   if (type == NotificationType::SYSTEM_SETTING_CHANGED) {
68     NotifySettingsChanged(Details<std::string>(details).ptr());
69     return;
70   }
71   ::CoreOptionsHandler::Observe(type, source, details);
72 }
73 
NotifySettingsChanged(const std::string * setting_name)74 void CoreChromeOSOptionsHandler::NotifySettingsChanged(
75     const std::string* setting_name) {
76   DCHECK(web_ui_);
77   DCHECK(CrosSettings::Get()->IsCrosSettings(*setting_name));
78   Value* value = NULL;
79   if (!CrosSettings::Get()->Get(*setting_name, &value)) {
80     NOTREACHED();
81     if (value)
82       delete value;
83     return;
84   }
85   for (PreferenceCallbackMap::const_iterator iter =
86       pref_callback_map_.find(*setting_name);
87       iter != pref_callback_map_.end(); ++iter) {
88     const std::wstring& callback_function = iter->second;
89     ListValue result_value;
90     result_value.Append(Value::CreateStringValue(setting_name->c_str()));
91     result_value.Append(value->DeepCopy());
92     web_ui_->CallJavascriptFunction(WideToASCII(callback_function),
93                                     result_value);
94   }
95   if (value)
96     delete value;
97 }
98 
99 }  // namespace chromeos
100