1 // Copyright 2015 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/extensions/api/settings_private/settings_private_delegate.h"
6 
7 #include <utility>
8 
9 #include "base/values.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/settings/cros_settings.h"
12 #include "chrome/browser/extensions/api/settings_private/prefs_util.h"
13 #include "chrome/browser/extensions/api/settings_private/prefs_util_enums.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/prefs/pref_service.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "third_party/blink/public/common/page/page_zoom.h"
21 #include "url/gurl.h"
22 
23 namespace extensions {
24 
SettingsPrivateDelegate(Profile * profile)25 SettingsPrivateDelegate::SettingsPrivateDelegate(Profile* profile)
26     : profile_(profile) {
27   prefs_util_.reset(new PrefsUtil(profile));
28 }
29 
~SettingsPrivateDelegate()30 SettingsPrivateDelegate::~SettingsPrivateDelegate() {
31 }
32 
GetPref(const std::string & name)33 std::unique_ptr<base::Value> SettingsPrivateDelegate::GetPref(
34     const std::string& name) {
35   std::unique_ptr<api::settings_private::PrefObject> pref =
36       prefs_util_->GetPref(name);
37   if (!pref)
38     return std::make_unique<base::Value>();
39   return pref->ToValue();
40 }
41 
GetAllPrefs()42 std::unique_ptr<base::Value> SettingsPrivateDelegate::GetAllPrefs() {
43   std::unique_ptr<base::ListValue> prefs(new base::ListValue());
44 
45   const TypedPrefMap& keys = prefs_util_->GetAllowlistedKeys();
46   for (const auto& it : keys) {
47     std::unique_ptr<base::Value> pref = GetPref(it.first);
48     if (!pref->is_none())
49       prefs->Append(std::move(pref));
50   }
51 
52   return std::move(prefs);
53 }
54 
SetPref(const std::string & pref_name,const base::Value * value)55 settings_private::SetPrefResult SettingsPrivateDelegate::SetPref(
56     const std::string& pref_name,
57     const base::Value* value) {
58   return prefs_util_->SetPref(pref_name, value);
59 }
60 
GetDefaultZoom()61 std::unique_ptr<base::Value> SettingsPrivateDelegate::GetDefaultZoom() {
62   // Zoom level prefs aren't available for off-the-record profiles (like guest
63   // mode on Chrome OS). The setting isn't visible to users anyway, so return a
64   // default value.
65   if (profile_->IsOffTheRecord())
66     return std::make_unique<base::Value>(0.0);
67   double zoom = blink::PageZoomLevelToZoomFactor(
68       profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref());
69   return std::make_unique<base::Value>(zoom);
70 }
71 
SetDefaultZoom(double zoom)72 settings_private::SetPrefResult SettingsPrivateDelegate::SetDefaultZoom(
73     double zoom) {
74   // See comment in GetDefaultZoom().
75   if (profile_->IsOffTheRecord())
76     return settings_private::SetPrefResult::PREF_NOT_MODIFIABLE;
77   double zoom_factor = blink::PageZoomFactorToZoomLevel(zoom);
78   profile_->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(zoom_factor);
79   return settings_private::SetPrefResult::SUCCESS;
80 }
81 
82 }  // namespace extensions
83