[email protected] | c333e79 | 2012-01-06 16:57:39 | [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/extensions/permissions_updater.h" |
| 6 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
| 10 | #include "base/values.h" |
[email protected] | 76aeb177 | 2012-01-20 22:14:16 | [diff] [blame] | 11 | #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 12 | #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 6386cf5 | 2012-09-07 04:26:37 | [diff] [blame] | 14 | #include "chrome/common/extensions/api/permissions.h" |
[email protected] | c97496b | 2012-10-01 16:20:19 | [diff] [blame] | 15 | #include "content/public/browser/notification_observer.h" |
| 16 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 17 | #include "content/public/browser/notification_service.h" |
| 18 | #include "content/public/browser/render_process_host.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 19 | #include "extensions/browser/event_router.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 20 | #include "extensions/browser/extension_prefs.h" |
limasdf | d006992 | 2015-10-20 17:07:15 | [diff] [blame] | 21 | #include "extensions/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 23 | #include "extensions/common/extension_messages.h" |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 24 | #include "extensions/common/manifest_handlers/permissions_parser.h" |
| 25 | #include "extensions/common/permissions/permission_set.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 26 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 27 | |
| 28 | using content::RenderProcessHost; |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 29 | using extensions::permissions_api_helpers::PackPermissionSet; |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 30 | |
| 31 | namespace extensions { |
| 32 | |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 33 | namespace permissions = api::permissions; |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 34 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | // Returns a PermissionSet that has the active permissions of the extension, |
| 38 | // bounded to its current manifest. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 39 | std::unique_ptr<const PermissionSet> GetBoundedActivePermissions( |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 40 | const Extension* extension, |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 41 | const PermissionSet* active_permissions) { |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 42 | // If the extension has used the optional permissions API, it will have a |
| 43 | // custom set of active permissions defined in the extension prefs. Here, |
| 44 | // we update the extension's active permissions based on the prefs. |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 45 | if (!active_permissions) |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 46 | return extension->permissions_data()->active_permissions().Clone(); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 47 | |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 48 | const PermissionSet& required_permissions = |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 49 | PermissionsParser::GetRequiredPermissions(extension); |
| 50 | |
| 51 | // We restrict the active permissions to be within the bounds defined in the |
| 52 | // extension's manifest. |
| 53 | // a) active permissions must be a subset of optional + default permissions |
| 54 | // b) active permissions must contains all default permissions |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 55 | std::unique_ptr<const PermissionSet> total_permissions = |
rdevlin.cronin | e01ec2c | 2015-09-17 21:27:28 | [diff] [blame] | 56 | PermissionSet::CreateUnion( |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 57 | required_permissions, |
| 58 | PermissionsParser::GetOptionalPermissions(extension)); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 59 | |
| 60 | // Make sure the active permissions contain no more than optional + default. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 61 | std::unique_ptr<const PermissionSet> adjusted_active = |
rdevlin.cronin | e01ec2c | 2015-09-17 21:27:28 | [diff] [blame] | 62 | PermissionSet::CreateIntersection(*total_permissions, |
| 63 | *active_permissions); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 64 | |
| 65 | // Make sure the active permissions contain the default permissions. |
rdevlin.cronin | e01ec2c | 2015-09-17 21:27:28 | [diff] [blame] | 66 | adjusted_active = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 67 | PermissionSet::CreateUnion(required_permissions, *adjusted_active); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 68 | |
| 69 | return adjusted_active; |
| 70 | } |
| 71 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 72 | } // namespace |
| 73 | |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 74 | PermissionsUpdater::PermissionsUpdater(content::BrowserContext* browser_context) |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 75 | : browser_context_(browser_context), init_flag_(INIT_FLAG_NONE) { |
| 76 | } |
| 77 | |
| 78 | PermissionsUpdater::PermissionsUpdater(content::BrowserContext* browser_context, |
| 79 | InitFlag init_flag) |
| 80 | : browser_context_(browser_context), init_flag_(init_flag) { |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 81 | } |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 82 | |
| 83 | PermissionsUpdater::~PermissionsUpdater() {} |
| 84 | |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 85 | void PermissionsUpdater::AddPermissions(const Extension* extension, |
| 86 | const PermissionSet& permissions) { |
| 87 | const PermissionSet& active = |
| 88 | extension->permissions_data()->active_permissions(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 89 | std::unique_ptr<const PermissionSet> total = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 90 | PermissionSet::CreateUnion(active, permissions); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 91 | std::unique_ptr<const PermissionSet> added = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 92 | PermissionSet::CreateDifference(*total, active); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 93 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 94 | std::unique_ptr<const PermissionSet> new_withheld = |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 95 | PermissionSet::CreateDifference( |
| 96 | extension->permissions_data()->withheld_permissions(), permissions); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 97 | SetPermissions(extension, std::move(total), std::move(new_withheld)); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 98 | |
| 99 | // Update the granted permissions so we don't auto-disable the extension. |
[email protected] | 009633c | 2013-03-07 22:08:28 | [diff] [blame] | 100 | GrantActivePermissions(extension); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 101 | |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 102 | NotifyPermissionsUpdated(ADDED, extension, *added); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 103 | } |
| 104 | |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 105 | void PermissionsUpdater::RemovePermissions(const Extension* extension, |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 106 | const PermissionSet& to_remove, |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 107 | RemoveType remove_type) { |
| 108 | // We should only be revoking revokable permissions. |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 109 | CHECK(GetRevokablePermissions(extension)->Contains(to_remove)); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 110 | |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 111 | const PermissionSet& active = |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 112 | extension->permissions_data()->active_permissions(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 113 | std::unique_ptr<const PermissionSet> remaining = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 114 | PermissionSet::CreateDifference(active, to_remove); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 115 | |
| 116 | // Move any granted permissions that were in the withheld set back to the |
| 117 | // withheld set so they can be added back later. |
| 118 | // Any revoked permission that isn't from the optional permissions can only |
| 119 | // be a withheld permission. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 120 | std::unique_ptr<const PermissionSet> removed_withheld = |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 121 | PermissionSet::CreateDifference( |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 122 | to_remove, PermissionsParser::GetOptionalPermissions(extension)); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 123 | std::unique_ptr<const PermissionSet> withheld = PermissionSet::CreateUnion( |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 124 | *removed_withheld, extension->permissions_data()->withheld_permissions()); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 125 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 126 | SetPermissions(extension, std::move(remaining), std::move(withheld)); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 127 | |
| 128 | // We might not want to revoke the granted permissions because the extension, |
| 129 | // not the user, removed the permissions. This allows the extension to add |
| 130 | // them again without prompting the user. |
| 131 | if (remove_type == REMOVE_HARD) { |
| 132 | ExtensionPrefs::Get(browser_context_) |
| 133 | ->RemoveGrantedPermissions(extension->id(), to_remove); |
| 134 | } |
| 135 | |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 136 | NotifyPermissionsUpdated(REMOVED, extension, to_remove); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void PermissionsUpdater::RemovePermissionsUnsafe( |
| 140 | const Extension* extension, |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 141 | const PermissionSet& to_remove) { |
| 142 | const PermissionSet& active = |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 143 | extension->permissions_data()->active_permissions(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 144 | std::unique_ptr<const PermissionSet> total = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 145 | PermissionSet::CreateDifference(active, to_remove); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 146 | // |successfully_removed| might not equal |to_remove| if |to_remove| contains |
| 147 | // permissions the extension didn't have. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 148 | std::unique_ptr<const PermissionSet> successfully_removed = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 149 | PermissionSet::CreateDifference(active, *total); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 150 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 151 | SetPermissions(extension, std::move(total), nullptr); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 152 | NotifyPermissionsUpdated(REMOVED, extension, *successfully_removed); |
rdevlin.cronin | 77cb0ef | 2015-09-16 17:03:48 | [diff] [blame] | 153 | } |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 154 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 155 | std::unique_ptr<const PermissionSet> |
| 156 | PermissionsUpdater::GetRevokablePermissions(const Extension* extension) const { |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 157 | // The user can revoke any permissions they granted. In other words, any |
| 158 | // permissions the extension didn't start with can be revoked. |
| 159 | const PermissionSet& required = |
| 160 | PermissionsParser::GetRequiredPermissions(extension); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 161 | std::unique_ptr<const PermissionSet> granted; |
| 162 | std::unique_ptr<const PermissionSet> withheld; |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 163 | ScriptingPermissionsModifier(browser_context_, make_scoped_refptr(extension)) |
rdevlin.cronin | 738501d | 2015-11-05 21:51:36 | [diff] [blame] | 164 | .WithholdPermissions(required, &granted, &withheld, true); |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 165 | return PermissionSet::CreateDifference( |
| 166 | extension->permissions_data()->active_permissions(), *granted); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 009633c | 2013-03-07 22:08:28 | [diff] [blame] | 169 | void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 170 | CHECK(extension); |
| 171 | |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 172 | ExtensionPrefs::Get(browser_context_) |
| 173 | ->AddGrantedPermissions( |
| 174 | extension->id(), extension->permissions_data()->active_permissions()); |
[email protected] | f746b3f | 2012-07-03 17:53:37 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 177 | void PermissionsUpdater::InitializePermissions(const Extension* extension) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 178 | std::unique_ptr<const PermissionSet> bounded_wrapper; |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 179 | const PermissionSet* bounded_active = nullptr; |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 180 | // If |extension| is a transient dummy extension, we do not want to look for |
| 181 | // it in preferences. |
| 182 | if (init_flag_ & INIT_FLAG_TRANSIENT) { |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 183 | bounded_active = &extension->permissions_data()->active_permissions(); |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 184 | } else { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 185 | std::unique_ptr<const PermissionSet> active_permissions = |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 186 | ExtensionPrefs::Get(browser_context_) |
| 187 | ->GetActivePermissions(extension->id()); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 188 | bounded_wrapper = |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 189 | GetBoundedActivePermissions(extension, active_permissions.get()); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 190 | bounded_active = bounded_wrapper.get(); |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 191 | } |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 192 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 193 | std::unique_ptr<const PermissionSet> granted_permissions; |
| 194 | std::unique_ptr<const PermissionSet> withheld_permissions; |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 195 | ScriptingPermissionsModifier(browser_context_, make_scoped_refptr(extension)) |
| 196 | .WithholdPermissions(*bounded_active, &granted_permissions, |
| 197 | &withheld_permissions, |
rdevlin.cronin | 738501d | 2015-11-05 21:51:36 | [diff] [blame] | 198 | (init_flag_ & INIT_FLAG_TRANSIENT) != 0); |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 199 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 200 | SetPermissions(extension, std::move(granted_permissions), |
| 201 | std::move(withheld_permissions)); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void PermissionsUpdater::SetPermissions( |
| 205 | const Extension* extension, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 206 | std::unique_ptr<const PermissionSet> active, |
| 207 | std::unique_ptr<const PermissionSet> withheld) { |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 208 | DCHECK(active); |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 209 | const PermissionSet& active_weak = *active; |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 210 | if (withheld) { |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 211 | extension->permissions_data()->SetPermissions(std::move(active), |
| 212 | std::move(withheld)); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 213 | } else { |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 214 | extension->permissions_data()->SetActivePermissions(std::move(active)); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 215 | } |
| 216 | |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 217 | if ((init_flag_ & INIT_FLAG_TRANSIENT) == 0) { |
| 218 | ExtensionPrefs::Get(browser_context_) |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 219 | ->SetActivePermissions(extension->id(), active_weak); |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 220 | } |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void PermissionsUpdater::DispatchEvent( |
| 224 | const std::string& extension_id, |
kalman | ef20c65 | 2015-07-06 22:18:33 | [diff] [blame] | 225 | events::HistogramValue histogram_value, |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 226 | const char* event_name, |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 227 | const PermissionSet& changed_permissions) { |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 228 | EventRouter* event_router = EventRouter::Get(browser_context_); |
| 229 | if (!event_router) |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 230 | return; |
| 231 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 232 | std::unique_ptr<base::ListValue> value(new base::ListValue()); |
| 233 | std::unique_ptr<api::permissions::Permissions> permissions = |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 234 | PackPermissionSet(changed_permissions); |
dcheng | 5d09049 | 2016-06-09 17:53:34 | [diff] [blame] | 235 | value->Append(permissions->ToValue()); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 236 | std::unique_ptr<Event> event( |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 237 | new Event(histogram_value, event_name, std::move(value))); |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 238 | event->restrict_to_browser_context = browser_context_; |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 239 | event_router->DispatchEventToExtension(extension_id, std::move(event)); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void PermissionsUpdater::NotifyPermissionsUpdated( |
| 243 | EventType event_type, |
| 244 | const Extension* extension, |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 245 | const PermissionSet& changed) { |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 246 | DCHECK((init_flag_ & INIT_FLAG_TRANSIENT) == 0); |
rdevlin.cronin | e2d0fd0 | 2015-09-24 22:35:49 | [diff] [blame] | 247 | if (changed.IsEmpty()) |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 248 | return; |
| 249 | |
| 250 | UpdatedExtensionPermissionsInfo::Reason reason; |
kalman | ef20c65 | 2015-07-06 22:18:33 | [diff] [blame] | 251 | events::HistogramValue histogram_value; |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 252 | const char* event_name = NULL; |
| 253 | |
| 254 | if (event_type == REMOVED) { |
| 255 | reason = UpdatedExtensionPermissionsInfo::REMOVED; |
kalman | ef20c65 | 2015-07-06 22:18:33 | [diff] [blame] | 256 | histogram_value = events::PERMISSIONS_ON_REMOVED; |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 257 | event_name = permissions::OnRemoved::kEventName; |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 258 | } else { |
| 259 | CHECK_EQ(ADDED, event_type); |
| 260 | reason = UpdatedExtensionPermissionsInfo::ADDED; |
kalman | ef20c65 | 2015-07-06 22:18:33 | [diff] [blame] | 261 | histogram_value = events::PERMISSIONS_ON_ADDED; |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 262 | event_name = permissions::OnAdded::kEventName; |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // Notify other APIs or interested parties. |
| 266 | UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( |
| 267 | extension, changed, reason); |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 268 | Profile* profile = Profile::FromBrowserContext(browser_context_); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 269 | content::NotificationService::current()->Notify( |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 270 | extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 271 | content::Source<Profile>(profile), |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 272 | content::Details<UpdatedExtensionPermissionsInfo>(&info)); |
| 273 | |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 274 | ExtensionMsg_UpdatePermissions_Params params; |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 275 | params.extension_id = extension->id(); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 276 | params.active_permissions = ExtensionMsg_PermissionSetStruct( |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 277 | extension->permissions_data()->active_permissions()); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 278 | params.withheld_permissions = ExtensionMsg_PermissionSetStruct( |
rdevlin.cronin | d630c30 | 2015-09-30 20:19:33 | [diff] [blame] | 279 | extension->permissions_data()->withheld_permissions()); |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 280 | |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 281 | // Send the new permissions to the renderers. |
| 282 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 283 | !i.IsAtEnd(); i.Advance()) { |
| 284 | RenderProcessHost* host = i.GetCurrentValue(); |
[email protected] | 8d42db5 | 2014-06-20 21:50:50 | [diff] [blame] | 285 | if (profile->IsSameProfile( |
| 286 | Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 287 | host->Send(new ExtensionMsg_UpdatePermissions(params)); |
[email protected] | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 288 | } |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Trigger the onAdded and onRemoved events in the extension. |
kalman | ef20c65 | 2015-07-06 22:18:33 | [diff] [blame] | 292 | DispatchEvent(extension->id(), histogram_value, event_name, changed); |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 293 | } |
| 294 | |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 295 | } // namespace extensions |