diff options
Diffstat (limited to 'chrome/browser')
9 files changed, 216 insertions, 60 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index a26b2b6..881dff5 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -36,6 +36,7 @@ #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/extensions/extension_i18n_api.h" #include "chrome/browser/extensions/extension_idle_api.h" +#include "chrome/browser/extensions/extension_managed_mode_api.h" #include "chrome/browser/extensions/extension_management_api.h" #include "chrome/browser/extensions/extension_metrics_module.h" #include "chrome/browser/extensions/extension_module.h" @@ -348,6 +349,10 @@ void FactoryRegistry::ResetFunctions() { #endif #endif + // Managed mode. + RegisterFunction<GetManagedModeFunction>(); + RegisterFunction<EnterManagedModeFunction>(); + // Management. RegisterFunction<GetAllExtensionsFunction>(); RegisterFunction<GetExtensionByIdFunction>(); diff --git a/chrome/browser/extensions/extension_managed_mode_api.cc b/chrome/browser/extensions/extension_managed_mode_api.cc new file mode 100644 index 0000000..12da93d --- /dev/null +++ b/chrome/browser/extensions/extension_managed_mode_api.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Implementation of the Chrome Extensions Managed Mode API. + +#include "chrome/browser/extensions/extension_managed_mode_api.h" + +#include <string> + +#include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/extension_preference_api_constants.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/common/pref_names.h" + +namespace { + +// Key to report whether the attempt to enter managed mode succeeded. +const char kEnterSuccessKey[] = "success"; + +} // namespace + +namespace keys = extension_preference_api_constants; + +GetManagedModeFunction::~GetManagedModeFunction() { } + +bool GetManagedModeFunction::RunImpl() { + PrefService* local_state = g_browser_process->local_state(); + bool in_managed_mode = local_state->GetBoolean(prefs::kInManagedMode); + + scoped_ptr<DictionaryValue> result(new DictionaryValue); + result->SetBoolean(keys::kValue, in_managed_mode); + result_.reset(result.release()); + return true; +} + +EnterManagedModeFunction::~EnterManagedModeFunction() { } + +bool EnterManagedModeFunction::RunImpl() { + PrefService* local_state = g_browser_process->local_state(); + bool in_managed_mode = local_state->GetBoolean(prefs::kInManagedMode); + + bool confirmed = true; + if (!in_managed_mode) { + // TODO(pamg): WIP. Show modal password dialog and save hashed password. Set + // |confirmed| to false if user cancels dialog. + + if (confirmed) + local_state->SetBoolean(prefs::kInManagedMode, true); + } + + scoped_ptr<DictionaryValue> result(new DictionaryValue); + result->SetBoolean(kEnterSuccessKey, confirmed); + result_.reset(result.release()); + return true; +} diff --git a/chrome/browser/extensions/extension_managed_mode_api.h b/chrome/browser/extensions/extension_managed_mode_api.h new file mode 100644 index 0000000..0c1ef49 --- /dev/null +++ b/chrome/browser/extensions/extension_managed_mode_api.h @@ -0,0 +1,28 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Defines the Chrome Extensions Managed Mode API relevant classes to realize +// the API as specified in the extension API JSON. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGED_MODE_API_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGED_MODE_API_H_ +#pragma once + +#include "chrome/browser/extensions/extension_function.h" + +class GetManagedModeFunction : public SyncExtensionFunction { + public: + virtual ~GetManagedModeFunction(); + virtual bool RunImpl() OVERRIDE; + DECLARE_EXTENSION_FUNCTION_NAME("experimental.managedMode.get") +}; + +class EnterManagedModeFunction : public SyncExtensionFunction { + public: + virtual ~EnterManagedModeFunction(); + virtual bool RunImpl() OVERRIDE; + DECLARE_EXTENSION_FUNCTION_NAME("experimental.managedMode.enter") +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGED_MODE_API_H_ diff --git a/chrome/browser/extensions/extension_managed_mode_apitest.cc b/chrome/browser/extensions/extension_managed_mode_apitest.cc new file mode 100644 index 0000000..54970e5 --- /dev/null +++ b/chrome/browser/extensions/extension_managed_mode_apitest.cc @@ -0,0 +1,24 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/pref_names.h" + +// Tests enabling and querying managed mode. +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ManagedModeGetAndEnable) { + CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kEnableExperimentalExtensionApis); + + PrefService* local_state = g_browser_process->local_state(); + ASSERT_FALSE(local_state->GetBoolean(prefs::kInManagedMode)); + + ASSERT_TRUE(RunExtensionTest("managedMode")) << message_; + const Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension); + + EXPECT_TRUE(local_state->GetBoolean(prefs::kInManagedMode)); +} diff --git a/chrome/browser/extensions/extension_preference_api.cc b/chrome/browser/extensions/extension_preference_api.cc index 7f4f4ee..585850c 100644 --- a/chrome/browser/extensions/extension_preference_api.cc +++ b/chrome/browser/extensions/extension_preference_api.cc @@ -26,6 +26,9 @@ #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" +namespace keys = extension_preference_api_constants; +namespace helpers = extension_preference_helpers; + namespace { struct PrefMappingEntry { @@ -41,15 +44,6 @@ struct PrefMappingEntry { ExtensionAPIPermission::ID permission; }; -const char kNotControllable[] = "not_controllable"; -const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; -const char kControllableByThisExtension[] = "controllable_by_this_extension"; -const char kControlledByThisExtension[] = "controlled_by_this_extension"; - -const char kIncognitoSpecific[] = "incognitoSpecific"; -const char kLevelOfControl[] = "levelOfControl"; -const char kValue[] = "value"; - const char kOnPrefChangeFormat[] = "types.ChromeSetting.%s.onChange"; PrefMappingEntry kPrefMapping[] = { @@ -69,6 +63,10 @@ PrefMappingEntry kPrefMapping[] = { prefs::kInstantEnabled, ExtensionAPIPermission::kPrivacy }, + { "managedModeEnabled", + prefs::kInManagedMode, + ExtensionAPIPermission::kManagedMode + }, { "networkPredictionEnabled", prefs::kNetworkPredictionEnabled, ExtensionAPIPermission::kPrivacy @@ -148,15 +146,15 @@ const char* GetLevelOfControl( ExtensionPrefs* ep = profile->GetExtensionService()->extension_prefs(); if (!pref->IsExtensionModifiable()) - return kNotControllable; + return keys::kNotControllable; if (ep->DoesExtensionControlPref(extension_id, browser_pref, incognito)) - return kControlledByThisExtension; + return keys::kControlledByThisExtension; if (ep->CanExtensionControlPref(extension_id, browser_pref, incognito)) - return kControllableByThisExtension; + return keys::kControllableByThisExtension; - return kControlledByOtherExtensions; + return keys::kControlledByOtherExtensions; } class PrefMapping { @@ -253,9 +251,6 @@ class PrefMapping { } // namespace -namespace keys = extension_preference_api_constants; -namespace helpers = extension_preference_helpers; - ExtensionPreferenceEventRouter::ExtensionPreferenceEventRouter( Profile* profile) : profile_(profile) { registrar_.Init(profile_->GetPrefs()); @@ -301,10 +296,11 @@ void ExtensionPreferenceEventRouter::OnPrefChanged( ExtensionService* extension_service = profile_->GetExtensionService(); PrefTransformerInterface* transformer = PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); - dict->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); + dict->Set(keys::kValue, + transformer->BrowserToExtensionPref(pref->GetValue())); if (incognito) { ExtensionPrefs* ep = extension_service->extension_prefs(); - dict->SetBoolean(kIncognitoSpecific, + dict->SetBoolean(keys::kIncognitoSpecific, ep->HasIncognitoPrefValue(browser_pref)); } @@ -321,7 +317,7 @@ void ExtensionPreferenceEventRouter::OnPrefChanged( (!incognito || extension_service->CanCrossIncognito(*it))) { std::string level_of_control = GetLevelOfControl(profile_, extension_id, browser_pref, incognito); - dict->SetString(kLevelOfControl, level_of_control); + dict->SetString(keys::kLevelOfControl, level_of_control); std::string json_args; base::JSONWriter::Write(&args, false, &json_args); @@ -339,7 +335,22 @@ void ExtensionPreferenceEventRouter::DispatchEvent( extension_id, event_name, json_args, NULL, GURL()); } -// TODO(battre): Factor out common parts once this is stable. +PreferenceFunction::~PreferenceFunction() { } + +bool PreferenceFunction::ValidateBrowserPref( + const std::string& extension_pref_key, + std::string* browser_pref_key) { + ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; + EXTENSION_FUNCTION_VALIDATE( + PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( + extension_pref_key, browser_pref_key, &permission)); + if (!GetExtension()->HasAPIPermission(permission)) { + error_ = ExtensionErrorUtils::FormatErrorMessage( + keys::kPermissionErrorMessage, extension_pref_key); + return false; + } + return true; +} GetPreferenceFunction::~GetPreferenceFunction() { } @@ -354,40 +365,42 @@ bool GetPreferenceFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(keys::kIncognitoKey, &incognito)); + // Check incognito access. if (incognito && !include_incognito()) { error_ = keys::kIncognitoErrorMessage; return false; } - PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() - : profile_->GetPrefs(); + // Obtain pref. std::string browser_pref; - ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; - EXTENSION_FUNCTION_VALIDATE( - PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( - pref_key, &browser_pref, &permission)); - if (!GetExtension()->HasAPIPermission(permission)) { - error_ = ExtensionErrorUtils::FormatErrorMessage( - keys::kPermissionErrorMessage, pref_key); + if (!ValidateBrowserPref(pref_key, &browser_pref)) return false; - } - + PrefService* prefs = incognito ? profile_->GetOffTheRecordPrefs() + : profile_->GetPrefs(); const PrefService::Preference* pref = prefs->FindPreference(browser_pref.c_str()); CHECK(pref); + + scoped_ptr<DictionaryValue> result(new DictionaryValue); + + // Retrieve level of control. std::string level_of_control = GetLevelOfControl(profile_, extension_id(), browser_pref, incognito); + result->SetString(keys::kLevelOfControl, level_of_control); - scoped_ptr<DictionaryValue> result(new DictionaryValue); + // Retrieve pref value. PrefTransformerInterface* transformer = PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); - result->Set(kValue, transformer->BrowserToExtensionPref(pref->GetValue())); - result->SetString(kLevelOfControl, level_of_control); + result->Set(keys::kValue, + transformer->BrowserToExtensionPref(pref->GetValue())); + + // Retrieve incognito status. if (incognito) { ExtensionPrefs* ep = profile_->GetExtensionService()->extension_prefs(); - result->SetBoolean(kIncognitoSpecific, + result->SetBoolean(keys::kIncognitoSpecific, ep->HasIncognitoPrefValue(browser_pref)); } + result_.reset(result.release()); return true; } @@ -401,7 +414,7 @@ bool SetPreferenceFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); Value* value = NULL; - EXTENSION_FUNCTION_VALIDATE(details->Get(kValue, &value)); + EXTENSION_FUNCTION_VALIDATE(details->Get(keys::kValue, &value)); ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; if (details->HasKey(keys::kScopeKey)) { @@ -412,6 +425,7 @@ bool SetPreferenceFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); } + // Check incognito scope. bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || scope == kExtensionPrefsScopeIncognitoSessionOnly); if (incognito) { @@ -435,20 +449,16 @@ bool SetPreferenceFunction::RunImpl() { return false; } + // Obtain pref. std::string browser_pref; - ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; - EXTENSION_FUNCTION_VALIDATE( - PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( - pref_key, &browser_pref, &permission)); - if (!GetExtension()->HasAPIPermission(permission)) { - error_ = ExtensionErrorUtils::FormatErrorMessage( - keys::kPermissionErrorMessage, pref_key); + if (!ValidateBrowserPref(pref_key, &browser_pref)) return false; - } ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); const PrefService::Preference* pref = prefs->pref_service()->FindPreference(browser_pref.c_str()); CHECK(pref); + + // Validate new value. EXTENSION_FUNCTION_VALIDATE(value->GetType() == pref->GetType()); PrefTransformerInterface* transformer = PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); @@ -461,6 +471,7 @@ bool SetPreferenceFunction::RunImpl() { bad_message_ = bad_message; return false; } + prefs->SetExtensionControlledPref(extension_id(), browser_pref, scope, @@ -485,6 +496,7 @@ bool ClearPreferenceFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(helpers::StringToScope(scope_str, &scope)); } + // Check incognito scope. bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || scope == kExtensionPrefsScopeIncognitoSessionOnly); if (incognito) { @@ -500,15 +512,9 @@ bool ClearPreferenceFunction::RunImpl() { } std::string browser_pref; - ExtensionAPIPermission::ID permission = ExtensionAPIPermission::kInvalid; - EXTENSION_FUNCTION_VALIDATE( - PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( - pref_key, &browser_pref, &permission)); - if (!GetExtension()->HasAPIPermission(permission)) { - error_ = ExtensionErrorUtils::FormatErrorMessage( - keys::kPermissionErrorMessage, pref_key); + if (!ValidateBrowserPref(pref_key, &browser_pref)) return false; - } + ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); prefs->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); return true; diff --git a/chrome/browser/extensions/extension_preference_api.h b/chrome/browser/extensions/extension_preference_api.h index e39239f..847e0f1 100644 --- a/chrome/browser/extensions/extension_preference_api.h +++ b/chrome/browser/extensions/extension_preference_api.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -66,21 +66,37 @@ class PrefTransformerInterface { const base::Value* browser_pref) = 0; }; -class GetPreferenceFunction : public SyncExtensionFunction { +// A base class to provide functionality common to the other *PreferenceFunction +// classes. +class PreferenceFunction : public SyncExtensionFunction { + public: + virtual ~PreferenceFunction(); + + protected: + // Given an |extension_pref_key|, provides its |browser_pref_key| from the + // static map in extension_preference.cc. Returns true if the corresponding + // browser pref exists and the extension has the API permission needed to + // modify that pref. Sets |error_| if the extension doesn't have the needed + // permission. + bool ValidateBrowserPref(const std::string& extension_pref_key, + std::string* browser_pref_key); +}; + +class GetPreferenceFunction : public PreferenceFunction { public: virtual ~GetPreferenceFunction(); virtual bool RunImpl() OVERRIDE; DECLARE_EXTENSION_FUNCTION_NAME("types.ChromeSetting.get") }; -class SetPreferenceFunction : public SyncExtensionFunction { +class SetPreferenceFunction : public PreferenceFunction { public: virtual ~SetPreferenceFunction(); virtual bool RunImpl() OVERRIDE; DECLARE_EXTENSION_FUNCTION_NAME("types.ChromeSetting.set") }; -class ClearPreferenceFunction : public SyncExtensionFunction { +class ClearPreferenceFunction : public PreferenceFunction { public: virtual ~ClearPreferenceFunction(); virtual bool RunImpl() OVERRIDE; diff --git a/chrome/browser/extensions/extension_preference_api_constants.cc b/chrome/browser/extensions/extension_preference_api_constants.cc index 9f4d333..aaba59f 100644 --- a/chrome/browser/extensions/extension_preference_api_constants.cc +++ b/chrome/browser/extensions/extension_preference_api_constants.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -10,6 +10,15 @@ const char kIncognitoKey[] = "incognito"; const char kScopeKey[] = "scope"; +const char kNotControllable[] = "not_controllable"; +const char kControlledByOtherExtensions[] = "controlled_by_other_extensions"; +const char kControllableByThisExtension[] = "controllable_by_this_extension"; +const char kControlledByThisExtension[] = "controlled_by_this_extension"; + +const char kIncognitoSpecific[] = "incognitoSpecific"; +const char kLevelOfControl[] = "levelOfControl"; +const char kValue[] = "value"; + const char kIncognitoErrorMessage[] = "You do not have permission to access incognito preferences."; diff --git a/chrome/browser/extensions/extension_preference_api_constants.h b/chrome/browser/extensions/extension_preference_api_constants.h index cc584e1..079a578 100644 --- a/chrome/browser/extensions/extension_preference_api_constants.h +++ b/chrome/browser/extensions/extension_preference_api_constants.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,10 +8,21 @@ namespace extension_preference_api_constants { -// Keys. +// Keys for incoming arguments. extern const char kIncognitoKey[]; extern const char kScopeKey[]; +// Keys for returned parameters. +extern const char kIncognitoSpecific[]; +extern const char kLevelOfControl[]; +extern const char kValue[]; + +// Values for levelOfControl. +extern const char kNotControllable[]; +extern const char kControlledByOtherExtensions[]; +extern const char kControllableByThisExtension[]; +extern const char kControlledByThisExtension[]; + // Errors. extern const char kIncognitoErrorMessage[]; extern const char kIncognitoSessionOnlyErrorMessage[]; diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc index 323aef7..b88fbc4 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -716,6 +716,7 @@ void ProfileManager::RegisterPrefs(PrefService* prefs) { prefs->RegisterStringPref(prefs::kProfileLastUsed, ""); prefs->RegisterIntegerPref(prefs::kProfilesNumCreated, 1); prefs->RegisterListPref(prefs::kProfilesLastActive); + prefs->RegisterBooleanPref(prefs::kInManagedMode, false); } size_t ProfileManager::GetNumberOfProfiles() { @@ -919,7 +920,7 @@ ProfileManagerWithoutInit::ProfileManagerWithoutInit( void ProfileManager::RegisterTestingProfile(Profile* profile, bool add_to_cache) { RegisterProfile(profile, true); - if (add_to_cache){ + if (add_to_cache) { InitProfileUserPrefs(profile); AddProfileToCache(profile); } |