Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / settings_private / settings_private_api.cc
blobcf3e7806428e088b994aa0c130f7b0a76386d62e
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.
5 #include "chrome/browser/extensions/api/settings_private/settings_private_api.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/api/settings_private/settings_private_delegate.h"
9 #include "chrome/browser/extensions/api/settings_private/settings_private_delegate_factory.h"
10 #include "chrome/browser/extensions/chrome_extension_function.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/extensions/api/settings_private.h"
13 #include "extensions/browser/extension_function_registry.h"
15 namespace extensions {
17 ////////////////////////////////////////////////////////////////////////////////
18 // SettingsPrivateSetPrefFunction
19 ////////////////////////////////////////////////////////////////////////////////
21 SettingsPrivateSetPrefFunction::~SettingsPrivateSetPrefFunction() {
24 ExtensionFunction::ResponseAction SettingsPrivateSetPrefFunction::Run() {
25 scoped_ptr<api::settings_private::SetPref::Params> parameters =
26 api::settings_private::SetPref::Params::Create(*args_);
27 EXTENSION_FUNCTION_VALIDATE(parameters.get());
29 SettingsPrivateDelegate* delegate =
30 SettingsPrivateDelegateFactory::GetForBrowserContext(browser_context());
32 PrefsUtil::SetPrefResult result =
33 delegate->SetPref(parameters->name, parameters->value.get());
34 switch (result) {
35 case PrefsUtil::SUCCESS:
36 return RespondNow(OneArgument(new base::FundamentalValue(true)));
37 case PrefsUtil::PREF_NOT_MODIFIABLE:
38 // Not an error, but return false to indicate setting the pref failed.
39 return RespondNow(OneArgument(new base::FundamentalValue(false)));
40 case PrefsUtil::PREF_NOT_FOUND:
41 return RespondNow(Error("Pref not found: *", parameters->name));
42 case PrefsUtil::PREF_TYPE_MISMATCH:
43 return RespondNow(Error("Incorrect type used for value of pref *",
44 parameters->name));
45 case PrefsUtil::PREF_TYPE_UNSUPPORTED:
46 return RespondNow(Error("Unsupported type used for value of pref *",
47 parameters->name));
49 NOTREACHED();
50 return RespondNow(OneArgument(new base::FundamentalValue(false)));
53 ////////////////////////////////////////////////////////////////////////////////
54 // SettingsPrivateGetAllPrefsFunction
55 ////////////////////////////////////////////////////////////////////////////////
57 SettingsPrivateGetAllPrefsFunction::~SettingsPrivateGetAllPrefsFunction() {
60 ExtensionFunction::ResponseAction SettingsPrivateGetAllPrefsFunction::Run() {
61 SettingsPrivateDelegate* delegate =
62 SettingsPrivateDelegateFactory::GetForBrowserContext(browser_context());
64 return RespondNow(OneArgument(delegate->GetAllPrefs().release()));
67 ////////////////////////////////////////////////////////////////////////////////
68 // SettingsPrivateGetPrefFunction
69 ////////////////////////////////////////////////////////////////////////////////
71 SettingsPrivateGetPrefFunction::~SettingsPrivateGetPrefFunction() {
74 ExtensionFunction::ResponseAction SettingsPrivateGetPrefFunction::Run() {
75 scoped_ptr<api::settings_private::GetPref::Params> parameters =
76 api::settings_private::GetPref::Params::Create(*args_);
77 EXTENSION_FUNCTION_VALIDATE(parameters.get());
79 SettingsPrivateDelegate* delegate =
80 SettingsPrivateDelegateFactory::GetForBrowserContext(browser_context());
82 scoped_ptr<base::Value> value = delegate->GetPref(parameters->name);
83 if (value->IsType(base::Value::TYPE_NULL))
84 return RespondNow(Error("Pref * does not exist", parameters->name));
85 else
86 return RespondNow(OneArgument(value.release()));
89 } // namespace extensions