pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 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 | #include "chrome/browser/plugins/plugin_policy_handler.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 9 | #include "base/stl_util.h" |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 10 | #include "base/strings/pattern.h" |
| 11 | #include "base/strings/string_util.h" |
| 12 | #include "base/values.h" |
pastarmovj | 9823e58a | 2016-11-16 14:18:13 | [diff] [blame] | 13 | #include "chrome/browser/plugins/plugin_metadata.h" |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 14 | #include "chrome/browser/plugins/plugin_prefs.h" |
| 15 | #include "chrome/common/chrome_content_client.h" |
| 16 | #include "chrome/common/pref_names.h" |
| 17 | #include "components/content_settings/core/common/pref_names.h" |
| 18 | #include "components/policy/core/browser/policy_error_map.h" |
| 19 | #include "components/strings/grit/components_strings.h" |
| 20 | #include "content/public/common/content_constants.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 24 | // Retrieves a list typed policy or nullptr if not present or not a list. |
| 25 | const base::ListValue* GetListPolicy(const policy::PolicyMap& policies, |
| 26 | const std::string& policy) { |
| 27 | const base::Value* value = policies.GetValue(policy); |
| 28 | if (!value) |
| 29 | return nullptr; |
| 30 | |
| 31 | const base::ListValue* policy_value = nullptr; |
| 32 | value->GetAsList(&policy_value); |
| 33 | return policy_value; |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | PluginPolicyHandler::PluginPolicyHandler() {} |
| 39 | |
| 40 | PluginPolicyHandler::~PluginPolicyHandler() {} |
| 41 | |
| 42 | void PluginPolicyHandler::ProcessPolicy(const policy::PolicyMap& policies, |
| 43 | PrefValueMap* prefs, |
| 44 | const std::string& policy, |
| 45 | bool disable_pdf_plugin, |
| 46 | ContentSetting flash_content_setting) { |
| 47 | const base::ListValue* plugins = GetListPolicy(policies, policy); |
| 48 | if (!plugins) |
| 49 | return; |
| 50 | |
| 51 | const int size = plugins->GetSize(); |
| 52 | for (int i = 0; i < size; ++i) { |
| 53 | std::string plugin; |
| 54 | if (!plugins->GetString(i, &plugin)) |
| 55 | continue; |
Lucas Furukawa Gadani | e67d603 | 2017-06-02 20:32:52 | [diff] [blame] | 56 | if ((base::MatchPattern(ChromeContentClient::kPDFExtensionPluginName, |
| 57 | plugin) || |
| 58 | base::MatchPattern(ChromeContentClient::kPDFInternalPluginName, |
| 59 | plugin)) && |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 60 | !policies.GetValue(policy::key::kAlwaysOpenPdfExternally)) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 61 | prefs->SetValue(prefs::kPluginsAlwaysOpenPdfExternally, |
Sylvain Defresne | 47ea273 | 2019-01-30 18:21:37 | [diff] [blame] | 62 | base::Value(disable_pdf_plugin)); |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 63 | } |
pastarmovj | 9823e58a | 2016-11-16 14:18:13 | [diff] [blame] | 64 | if ((base::MatchPattern( |
| 65 | PluginMetadata::kAdobeFlashPlayerGroupName, plugin) || |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 66 | base::MatchPattern(content::kFlashPluginName, plugin)) && |
| 67 | !policies.GetValue(policy::key::kDefaultPluginsSetting)) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 68 | prefs->SetValue(prefs::kManagedDefaultPluginsSetting, |
Sylvain Defresne | 47ea273 | 2019-01-30 18:21:37 | [diff] [blame] | 69 | base::Value(flash_content_setting)); |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bool PluginPolicyHandler::CheckPolicySettings(const policy::PolicyMap& policies, |
| 75 | policy::PolicyErrorMap* errors) { |
| 76 | const std::string checked_policies[] = { |
| 77 | policy::key::kEnabledPlugins, policy::key::kDisabledPlugins, |
| 78 | policy::key::kDisabledPluginsExceptions}; |
| 79 | bool ok = true; |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 80 | for (size_t i = 0; i < base::size(checked_policies); ++i) { |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 81 | const base::Value* value = policies.GetValue(checked_policies[i]); |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 82 | if (value && !value->is_list()) { |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 83 | errors->AddError(checked_policies[i], IDS_POLICY_TYPE_ERROR, |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 84 | base::Value::GetTypeName(base::Value::Type::LIST)); |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 85 | ok = false; |
| 86 | } |
| 87 | } |
| 88 | return ok; |
| 89 | } |
| 90 | |
| 91 | void PluginPolicyHandler::ApplyPolicySettings(const policy::PolicyMap& policies, |
| 92 | PrefValueMap* prefs) { |
| 93 | // This order makes enabled plugins take precedence as is now. |
| 94 | ProcessPolicy(policies, prefs, policy::key::kDisabledPlugins, true, |
| 95 | CONTENT_SETTING_BLOCK); |
| 96 | ProcessPolicy(policies, prefs, policy::key::kEnabledPlugins, false, |
| 97 | CONTENT_SETTING_ALLOW); |
| 98 | |
| 99 | // Finally check if any of the two is in the exceptions list and remove the |
| 100 | // policy changes. |
| 101 | const base::ListValue* plugins = |
| 102 | GetListPolicy(policies, policy::key::kDisabledPluginsExceptions); |
| 103 | if (!plugins) |
| 104 | return; |
| 105 | const int size = plugins->GetSize(); |
| 106 | for (int i = 0; i < size; ++i) { |
| 107 | std::string plugin; |
| 108 | if (!plugins->GetString(i, &plugin)) |
| 109 | continue; |
Lucas Furukawa Gadani | e67d603 | 2017-06-02 20:32:52 | [diff] [blame] | 110 | if ((base::MatchPattern(ChromeContentClient::kPDFExtensionPluginName, |
| 111 | plugin) || |
| 112 | base::MatchPattern(ChromeContentClient::kPDFInternalPluginName, |
| 113 | plugin)) && |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 114 | !policies.GetValue(policy::key::kAlwaysOpenPdfExternally)) { |
| 115 | prefs->RemoveValue(prefs::kPluginsAlwaysOpenPdfExternally); |
| 116 | } |
pastarmovj | 9823e58a | 2016-11-16 14:18:13 | [diff] [blame] | 117 | if ((base::MatchPattern( |
| 118 | PluginMetadata::kAdobeFlashPlayerGroupName, plugin) || |
pastarmovj | c6c75ce | 2016-10-17 19:21:10 | [diff] [blame] | 119 | base::MatchPattern(content::kFlashPluginName, plugin)) && |
| 120 | !policies.GetValue(policy::key::kDefaultPluginsSetting)) { |
| 121 | prefs->RemoveValue(prefs::kManagedDefaultPluginsSetting); |
| 122 | } |
| 123 | } |
| 124 | } |