OLD | NEW |
(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/api/managed_mode_private/managed_mode_privat
e_api.h" |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/json/json_writer.h" |
| 13 #include "base/lazy_instance.h" |
| 14 #include "base/prefs/pref_service.h" |
| 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" |
| 17 #include "chrome/browser/extensions/event_router.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/browser/managed_mode/managed_mode.h" |
| 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "chrome/common/pref_names.h" |
| 23 #include "content/public/browser/notification_details.h" |
| 24 |
| 25 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 26 #include "chrome/browser/policy/managed_mode_policy_provider.h" |
| 27 #endif |
| 28 |
| 29 namespace { |
| 30 |
| 31 // Event that is fired when we enter or leave managed mode. |
| 32 const char kChangeEventName[] = "managedModePrivate.onChange"; |
| 33 |
| 34 // Key to report whether the attempt to enter managed mode succeeded. |
| 35 const char kEnterSuccessKey[] = "success"; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 namespace keys = extensions::preference_api_constants; |
| 40 |
| 41 namespace extensions { |
| 42 |
| 43 ManagedModeEventRouter::ManagedModeEventRouter( |
| 44 Profile* profile) : profile_(profile) { |
| 45 registrar_.Init(g_browser_process->local_state()); |
| 46 registrar_.Add( |
| 47 prefs::kInManagedMode, |
| 48 base::Bind(&ManagedModeEventRouter::OnInManagedModeChanged, |
| 49 base::Unretained(this))); |
| 50 } |
| 51 |
| 52 ManagedModeEventRouter::~ManagedModeEventRouter() { |
| 53 } |
| 54 |
| 55 void ManagedModeEventRouter::OnInManagedModeChanged() { |
| 56 DictionaryValue* dict = new DictionaryValue(); |
| 57 dict->SetBoolean( |
| 58 keys::kValue, |
| 59 g_browser_process->local_state()->GetBoolean(prefs::kInManagedMode)); |
| 60 scoped_ptr<ListValue> args(new ListValue()); |
| 61 args->Set(0, dict); |
| 62 |
| 63 extensions::EventRouter* event_router = |
| 64 extensions::ExtensionSystem::Get(profile_)->event_router(); |
| 65 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 66 kChangeEventName, args.Pass())); |
| 67 event_router->BroadcastEvent(event.Pass()); |
| 68 } |
| 69 |
| 70 ManagedModePrivateGetFunction::~ManagedModePrivateGetFunction() { } |
| 71 |
| 72 bool ManagedModePrivateGetFunction::RunImpl() { |
| 73 bool in_managed_mode = ManagedMode::IsInManagedMode(); |
| 74 |
| 75 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 76 result->SetBoolean(keys::kValue, in_managed_mode); |
| 77 SetResult(result.release()); |
| 78 return true; |
| 79 } |
| 80 |
| 81 ManagedModePrivateEnterFunction::~ManagedModePrivateEnterFunction() { } |
| 82 |
| 83 bool ManagedModePrivateEnterFunction::RunImpl() { |
| 84 ManagedMode::EnterManagedMode( |
| 85 profile(), |
| 86 base::Bind(&ManagedModePrivateEnterFunction::SendResult, this)); |
| 87 return true; |
| 88 } |
| 89 |
| 90 void ManagedModePrivateEnterFunction::SendResult(bool success) { |
| 91 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 92 result->SetBoolean(kEnterSuccessKey, success); |
| 93 SetResult(result.release()); |
| 94 SendResponse(true); |
| 95 } |
| 96 |
| 97 ManagedModePrivateGetPolicyFunction::~ManagedModePrivateGetPolicyFunction() { } |
| 98 |
| 99 bool ManagedModePrivateGetPolicyFunction::RunImpl() { |
| 100 std::string key; |
| 101 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &key)); |
| 102 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 103 policy::ManagedModePolicyProvider* policy_provider = |
| 104 profile_->GetManagedModePolicyProvider(); |
| 105 const base::Value* policy = policy_provider->GetPolicy(key); |
| 106 if (policy) |
| 107 SetResult(policy->DeepCopy()); |
| 108 #endif |
| 109 return true; |
| 110 } |
| 111 |
| 112 ManagedModePrivateSetPolicyFunction::~ManagedModePrivateSetPolicyFunction() { } |
| 113 |
| 114 bool ManagedModePrivateSetPolicyFunction::RunImpl() { |
| 115 std::string key; |
| 116 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &key)); |
| 117 base::Value* value = NULL; |
| 118 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &value)); |
| 119 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 120 policy::ManagedModePolicyProvider* policy_provider = |
| 121 profile_->GetManagedModePolicyProvider(); |
| 122 policy_provider->SetPolicy(key, value); |
| 123 #endif |
| 124 return true; |
| 125 } |
| 126 |
| 127 ManagedModeAPI::ManagedModeAPI(Profile* profile) |
| 128 : profile_(profile) { |
| 129 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 130 this, kChangeEventName); |
| 131 } |
| 132 |
| 133 ManagedModeAPI::~ManagedModeAPI() { |
| 134 } |
| 135 |
| 136 void ManagedModeAPI::Shutdown() { |
| 137 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 138 } |
| 139 |
| 140 static base::LazyInstance<ProfileKeyedAPIFactory<ManagedModeAPI> > |
| 141 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 142 |
| 143 // static |
| 144 ProfileKeyedAPIFactory<ManagedModeAPI>* ManagedModeAPI::GetFactoryInstance() { |
| 145 return &g_factory.Get(); |
| 146 } |
| 147 |
| 148 void ManagedModeAPI::OnListenerAdded( |
| 149 const extensions::EventListenerInfo& details) { |
| 150 managed_mode_event_router_.reset(new ManagedModeEventRouter(profile_)); |
| 151 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 152 } |
| 153 |
| 154 } // namespace extensions |
OLD | NEW |