Skip to content

Commit

Permalink
Enable NPAPI if policy has plugin policies.
Browse files Browse the repository at this point in the history
BUG=472648
TEST=manual - verify that NPAPI plugins appear in chrome://plugins when either a machine or user plugin policy is set.

Review URL: https://codereview.chromium.org/1071713004

Cr-Commit-Position: refs/heads/master@{#324754}
  • Loading branch information
wfh-chromium authored and Commit bot committed Apr 11, 2015
1 parent cd7a39a commit 26d09db
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 34 deletions.
5 changes: 5 additions & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ source_set("browser") {
".",
"//chrome")
}
if (enable_plugins) {
sources += rebase_path(gypi_values.chrome_browser_policy_plugin_sources,
".",
"//chrome")
}
if (is_android || is_ios) {
sources += rebase_path(gypi_values.chrome_browser_policy_mobile_sources,
".",
Expand Down
46 changes: 46 additions & 0 deletions chrome/browser/plugins/enable_npapi_plugins_policy_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 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/plugins/enable_npapi_plugins_policy_handler.h"

#include "base/logging.h"
#include "base/prefs/pref_value_map.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "policy/policy_constants.h"

namespace policy {

EnableNpapiPluginsPolicyHandler::EnableNpapiPluginsPolicyHandler() {
}

EnableNpapiPluginsPolicyHandler::~EnableNpapiPluginsPolicyHandler() {
}

void EnableNpapiPluginsPolicyHandler::ApplyPolicySettings(
const PolicyMap& policies,
PrefValueMap* prefs) {
const std::string plugin_policies[] = {key::kEnabledPlugins,
key::kPluginsAllowedForUrls,
key::kPluginsBlockedForUrls,
key::kDisabledPluginsExceptions,
key::kDisabledPlugins};

for (auto policy : plugin_policies) {
if (policies.GetValue(policy)) {
prefs->SetBoolean(prefs::kEnableNpapi, true);
break;
}
}
}

bool EnableNpapiPluginsPolicyHandler::CheckPolicySettings(
const PolicyMap& policies,
PolicyErrorMap* prefs) {
return true;
}

} // namespace policy
37 changes: 37 additions & 0 deletions chrome/browser/plugins/enable_npapi_plugins_policy_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2015 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.

#ifndef CHROME_BROWSER_PLUGINS_ENABLE_NPAPI_PLUGINS_POLICY_HANDLER_H_
#define CHROME_BROWSER_PLUGINS_ENABLE_NPAPI_PLUGINS_POLICY_HANDLER_H_

#include "base/macros.h"
#include "components/policy/core/browser/configuration_policy_handler.h"

class PrefValueMap;

namespace policy {

class PolicyErrorMap;
class PolicyMap;

// ConfigurationPolicyHandler for the plugin policies that re-enable NPAPI.
class EnableNpapiPluginsPolicyHandler : public ConfigurationPolicyHandler {
public:
EnableNpapiPluginsPolicyHandler();

~EnableNpapiPluginsPolicyHandler() override;

// ConfigurationPolicyHandler methods:
void ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) override;
// ConfigurationPolicyHandler methods:
bool CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* prefs) override;

DISALLOW_COPY_AND_ASSIGN(EnableNpapiPluginsPolicyHandler);
};

} // namespace policy

#endif // CHROME_BROWSER_PLUGINS_ENABLE_NPAPI_PLUGINS_POLICY_HANDLER_H_
11 changes: 11 additions & 0 deletions chrome/browser/plugins/plugin_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ void PluginPrefs::UpdatePatternsAndNotify(std::set<base::string16>* patterns,
NotifyPluginStatusChanged();
}

void PluginPrefs::EnableNpapi() {
PluginService::GetInstance()->EnableNpapiPlugins();
NotifyPluginStatusChanged();
}

/*static*/
bool PluginPrefs::IsStringMatchedInSet(
const base::string16& name,
Expand Down Expand Up @@ -486,6 +491,9 @@ void PluginPrefs::SetPrefs(PrefService* prefs) {
ListValueToStringSet(prefs_->GetList(prefs::kPluginsEnabledPlugins),
&policy_enabled_plugin_patterns_);

if (prefs_->GetBoolean(prefs::kEnableNpapi))
EnableNpapi();

registrar_.Init(prefs_);

// Because pointers to our own members will remain unchanged for the
Expand All @@ -504,6 +512,9 @@ void PluginPrefs::SetPrefs(PrefService* prefs) {
base::Bind(&PluginPrefs::UpdatePatternsAndNotify,
base::Unretained(this),
&policy_enabled_plugin_patterns_));
registrar_.Add(prefs::kEnableNpapi,
base::Bind(&PluginPrefs::EnableNpapi,
base::Unretained(this)));

NotifyPluginStatusChanged();
}
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/plugins/plugin_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class PluginPrefs : public RefcountedKeyedService {
void UpdatePatternsAndNotify(std::set<base::string16>* patterns,
const std::string& pref_name);

// Called to enable NPAPI if kEnableNpapi gets set by policy.
void EnableNpapi();

// Allows unit tests to directly set enforced plugin patterns.
void SetPolicyEnforcedPluginPatterns(
const std::set<base::string16>& disabled_patterns,
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/plugins/plugin_prefs_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ void PluginPrefsFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterListPref(prefs::kPluginsEnabledPlugins,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kEnableNpapi,
false,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}

content::BrowserContext* PluginPrefsFactory::GetBrowserContextToUse(
Expand Down
26 changes: 0 additions & 26 deletions chrome/browser/policy/chrome_browser_policy_connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ void ChromeBrowserPolicyConnector::Init(

BrowserPolicyConnector::Init(
local_state, request_context, device_management_service.Pass());

AppendExtraFlagsPerPolicy();
}

ConfigurationPolicyProvider*
Expand Down Expand Up @@ -123,28 +121,4 @@ ConfigurationPolicyProvider*
#endif
}

void ChromeBrowserPolicyConnector::AppendExtraFlagsPerPolicy() {
PolicyService* policy_service = GetPolicyService();
PolicyNamespace chrome_ns = PolicyNamespace(POLICY_DOMAIN_CHROME, "");
const PolicyMap& chrome_policy = policy_service->GetPolicies(chrome_ns);
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

if (command_line->HasSwitch(switches::kEnableNpapi))
return;

// The list of Plugin related policies that re-enable NPAPI. Remove once NPAPI
// is dead.
const std::string plugin_policies[] = { key::kEnabledPlugins,
key::kPluginsAllowedForUrls,
key::kPluginsBlockedForUrls,
key::kDisabledPluginsExceptions,
key::kDisabledPlugins };
for (auto policy : plugin_policies) {
if (chrome_policy.GetValue(policy)) {
command_line->AppendSwitch(switches::kEnableNpapi);
break;
}
}
}

} // namespace policy
2 changes: 0 additions & 2 deletions chrome/browser/policy/chrome_browser_policy_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
private:
ConfigurationPolicyProvider* CreatePlatformProvider();

void AppendExtraFlagsPerPolicy();

DISALLOW_COPY_AND_ASSIGN(ChromeBrowserPolicyConnector);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
#include "extensions/common/manifest.h"
#endif

#if defined(ENABLE_PLUGINS)
#include "chrome/browser/plugins/enable_npapi_plugins_policy_handler.h"
#endif

namespace policy {

namespace {
Expand Down Expand Up @@ -598,6 +602,10 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList(
new extensions::ExtensionSettingsPolicyHandler(chrome_schema)));
#endif

#if defined(ENABLE_PLUGINS)
handlers->AddHandler(make_scoped_ptr(new EnableNpapiPluginsPolicyHandler()));
#endif

#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
handlers->AddHandler(make_scoped_ptr(new DiskCacheDirPolicyHandler()));

Expand Down
8 changes: 8 additions & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,11 @@
'browser/upgrade_detector_impl.cc',
'browser/upgrade_detector_impl.h',
],
# Policy sources used if plugins are enabled.
'chrome_browser_policy_plugin_sources': [
'browser/plugins/enable_npapi_plugins_policy_handler.cc',
'browser/plugins/enable_npapi_plugins_policy_handler.h',
],
'chrome_browser_predictor_sources': [
'browser/predictors/autocomplete_action_predictor.cc',
'browser/predictors/autocomplete_action_predictor.h',
Expand Down Expand Up @@ -3291,6 +3296,9 @@
['OS=="win" or OS=="mac" or desktop_linux==1', {
'sources': [ '<@(chrome_browser_policy_desktop_sources)' ],
}],
['enable_plugins==1', {
'sources': [ '<@(chrome_browser_policy_plugin_sources)' ],
}],
['OS=="android" or OS=="ios"', { # Mobile.
'sources': [ '<@(chrome_browser_policy_mobile_sources)' ],
}, { # Non-mobile.
Expand Down
3 changes: 3 additions & 0 deletions chrome/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,9 @@ const char kPluginsDisabledPluginsExceptions[] =
// List pref containing names of plugins that are enabled by policy.
const char kPluginsEnabledPlugins[] = "plugins.plugins_enabled";

// Whether NPAPI plugins are enabled.
const char kEnableNpapi[] = "plugins.enable_npapi";

// When bundled NPAPI Flash is removed, if at that point it is enabled while
// Pepper Flash is disabled, we would like to turn on Pepper Flash. And we will
// want to do so only once.
Expand Down
1 change: 1 addition & 0 deletions chrome/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ extern const char kPluginsPluginsList[];
extern const char kPluginsDisabledPlugins[];
extern const char kPluginsDisabledPluginsExceptions[];
extern const char kPluginsEnabledPlugins[];
extern const char kEnableNpapi[];
extern const char kPluginsMigratedToPepperFlash[];
extern const char kPluginsRemovedOldComponentPepperFlashSettings[];
#if !defined(OS_ANDROID)
Expand Down
10 changes: 9 additions & 1 deletion content/browser/plugin_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,9 @@ void PluginServiceImpl::GetInternalPlugins(
}

bool PluginServiceImpl::NPAPIPluginsSupported() {
if (npapi_plugins_enabled_)
return true;

static bool command_line_checked = false;

if (!command_line_checked) {
Expand All @@ -812,8 +815,13 @@ void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
PluginList::Singleton()->DisablePluginsDiscovery();
}

void PluginServiceImpl::EnableNpapiPluginsForTesting() {
void PluginServiceImpl::EnableNpapiPlugins() {
npapi_plugins_enabled_ = true;
RefreshPlugins();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&PluginService::PurgePluginListCache,
static_cast<BrowserContext*>(NULL), false));
}

#if defined(OS_MACOSX)
Expand Down
2 changes: 1 addition & 1 deletion content/browser/plugin_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CONTENT_EXPORT PluginServiceImpl
void UnregisterInternalPlugin(const base::FilePath& path) override;
void GetInternalPlugins(std::vector<WebPluginInfo>* plugins) override;
bool NPAPIPluginsSupported() override;
void EnableNpapiPluginsForTesting() override;
void EnableNpapiPlugins() override;
void DisablePluginsDiscoveryForTesting() override;
#if defined(OS_MACOSX)
void AppActivated() override;
Expand Down
4 changes: 2 additions & 2 deletions content/public/browser/plugin_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class PluginService {
// This can be called from any thread.
virtual bool NPAPIPluginsSupported() = 0;

// This is equivalent to specifying kEnableNpapi, but is useful for unittests.
virtual void EnableNpapiPluginsForTesting() = 0;
// This is equivalent to specifying kEnableNpapi.
virtual void EnableNpapiPlugins() = 0;

// This is equivalent to specifying kDisablePluginsDiscovery, but is useful
// for unittests.
Expand Down
2 changes: 1 addition & 1 deletion content/test/fake_plugin_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool FakePluginService::NPAPIPluginsSupported() {
return false;
}

void FakePluginService::EnableNpapiPluginsForTesting() {
void FakePluginService::EnableNpapiPlugins() {
}

void FakePluginService::DisablePluginsDiscoveryForTesting() {
Expand Down
2 changes: 1 addition & 1 deletion content/test/fake_plugin_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FakePluginService : public PluginService {
void UnregisterInternalPlugin(const base::FilePath& path) override;
void GetInternalPlugins(std::vector<WebPluginInfo>* plugins) override;
bool NPAPIPluginsSupported() override;
void EnableNpapiPluginsForTesting() override;
void EnableNpapiPlugins() override;
void DisablePluginsDiscoveryForTesting() override;
#if defined(OS_MACOSX)
void AppActivated() override;
Expand Down

0 comments on commit 26d09db

Please sign in to comment.