Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: chrome/browser/extensions/extension_managed_mode_api.cc

Issue 10704201: Moving managed_mode to api/ . (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 // Implementation of the Chrome Extensions Managed Mode API.
6
7 #include "chrome/browser/extensions/extension_managed_mode_api.h"
8
9 #include <string>
10
11 #include "base/bind.h"
12 #include "base/json/json_writer.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/extension_event_router.h"
15 #include "chrome/browser/extensions/extension_preference_api_constants.h"
16 #include "chrome/browser/managed_mode.h"
17 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_notification_types.h"
20 #include "chrome/common/pref_names.h"
21 #include "content/public/browser/notification_details.h"
22
23 #if defined(ENABLE_CONFIGURATION_POLICY)
24 #include "chrome/browser/policy/managed_mode_policy_provider.h"
25 #include "chrome/browser/policy/managed_mode_policy_provider_factory.h"
26 #endif
27
28 namespace {
29
30 // Event that is fired when we enter or leave managed mode.
31 const char kChangeEventName[] = "managedModePrivate.onChange";
32
33 // Key to report whether the attempt to enter managed mode succeeded.
34 const char kEnterSuccessKey[] = "success";
35
36 } // namespace
37
38 namespace keys = extension_preference_api_constants;
39
40 ExtensionManagedModeEventRouter::ExtensionManagedModeEventRouter(
41 Profile* profile) : profile_(profile) {
42 }
43
44 void ExtensionManagedModeEventRouter::Init() {
45 registrar_.Init(g_browser_process->local_state());
46 registrar_.Add(prefs::kInManagedMode, this);
47 }
48
49 ExtensionManagedModeEventRouter::~ExtensionManagedModeEventRouter() {
50 }
51
52 void ExtensionManagedModeEventRouter::Observe(
53 int type,
54 const content::NotificationSource& source,
55 const content::NotificationDetails& details) {
56 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
57 const std::string& pref_name =
58 *content::Details<std::string>(details).ptr();
59 DCHECK_EQ(std::string(prefs::kInManagedMode), pref_name);
60
61 ListValue args;
62 DictionaryValue* dict = new DictionaryValue();
63 args.Append(dict);
64 dict->SetBoolean(extension_preference_api_constants::kValue,
65 g_browser_process->local_state()->GetBoolean(prefs::kInManagedMode));
66 std::string json_args;
67 base::JSONWriter::Write(&args, &json_args);
68 ExtensionEventRouter* event_router = profile_->GetExtensionEventRouter();
69 event_router->DispatchEventToRenderers(kChangeEventName, json_args, NULL,
70 GURL(),
71 extensions::EventFilteringInfo());
72 }
73
74 GetManagedModeFunction::~GetManagedModeFunction() { }
75
76 bool GetManagedModeFunction::RunImpl() {
77 bool in_managed_mode = ManagedMode::IsInManagedMode();
78
79 scoped_ptr<DictionaryValue> result(new DictionaryValue);
80 result->SetBoolean(keys::kValue, in_managed_mode);
81 SetResult(result.release());
82 return true;
83 }
84
85 EnterManagedModeFunction::~EnterManagedModeFunction() { }
86
87 bool EnterManagedModeFunction::RunImpl() {
88 ManagedMode::EnterManagedMode(
89 profile(),
90 base::Bind(&EnterManagedModeFunction::SendResult, this));
91 return true;
92 }
93
94 void EnterManagedModeFunction::SendResult(bool success) {
95 scoped_ptr<DictionaryValue> result(new DictionaryValue);
96 result->SetBoolean(kEnterSuccessKey, success);
97 SetResult(result.release());
98 SendResponse(true);
99 }
100
101 GetPolicyFunction::~GetPolicyFunction() { }
102
103 bool GetPolicyFunction::RunImpl() {
104 std::string key;
105 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &key));
106 #if defined(ENABLE_CONFIGURATION_POLICY)
107 policy::ManagedModePolicyProvider* policy_provider =
108 ManagedModePolicyProviderFactory::GetForProfile(profile_);
109 const base::Value* policy = policy_provider->GetPolicy(key);
110 if (policy)
111 SetResult(policy->DeepCopy());
112 #endif
113 return true;
114 }
115
116 SetPolicyFunction::~SetPolicyFunction() { }
117
118 bool SetPolicyFunction::RunImpl() {
119 std::string key;
120 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &key));
121 base::Value* value = NULL;
122 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &value));
123 #if defined(ENABLE_CONFIGURATION_POLICY)
124 policy::ManagedModePolicyProvider* policy_provider =
125 ManagedModePolicyProviderFactory::GetForProfile(profile_);
126 policy_provider->SetPolicy(key, value);
127 #endif
128 return true;
129 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_managed_mode_api.h ('k') | chrome/browser/extensions/extension_managed_mode_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698