| 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 #include "chrome/browser/extensions/permissions_updater.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/extension_event_router.h" |
| 11 #include "chrome/browser/extensions/extension_permissions_api_helpers.h" |
| 12 #include "chrome/browser/extensions/extension_prefs.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/extensions/extension_messages.h" |
| 18 #include "chrome/common/extensions/extension_permission_set.h" |
| 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/render_process_host.h" |
| 21 |
| 22 using content::RenderProcessHost; |
| 23 using extensions::permissions_api::PackPermissionsToValue; |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kOnAdded[] = "permissions.onAdded"; |
| 30 const char kOnRemoved[] = "permissions.onRemoved"; |
| 31 |
| 32 } |
| 33 |
| 34 PermissionsUpdater::PermissionsUpdater(Profile* profile) |
| 35 : profile_(profile) {} |
| 36 |
| 37 PermissionsUpdater::~PermissionsUpdater() {} |
| 38 |
| 39 void PermissionsUpdater::AddPermissions( |
| 40 const Extension* extension, const ExtensionPermissionSet* permissions) { |
| 41 scoped_refptr<const ExtensionPermissionSet> existing( |
| 42 extension->GetActivePermissions()); |
| 43 scoped_refptr<ExtensionPermissionSet> total( |
| 44 ExtensionPermissionSet::CreateUnion(existing, permissions)); |
| 45 scoped_refptr<ExtensionPermissionSet> added( |
| 46 ExtensionPermissionSet::CreateDifference(total.get(), existing)); |
| 47 |
| 48 UpdateActivePermissions(extension, total.get()); |
| 49 |
| 50 // Update the granted permissions so we don't auto-disable the extension. |
| 51 GrantActivePermissions(extension); |
| 52 |
| 53 NotifyPermissionsUpdated(ADDED, extension, added.get()); |
| 54 } |
| 55 |
| 56 void PermissionsUpdater::RemovePermissions( |
| 57 const Extension* extension, const ExtensionPermissionSet* permissions) { |
| 58 scoped_refptr<const ExtensionPermissionSet> existing( |
| 59 extension->GetActivePermissions()); |
| 60 scoped_refptr<ExtensionPermissionSet> total( |
| 61 ExtensionPermissionSet::CreateDifference(existing, permissions)); |
| 62 scoped_refptr<ExtensionPermissionSet> removed( |
| 63 ExtensionPermissionSet::CreateDifference(existing, total.get())); |
| 64 |
| 65 // We update the active permissions, and not the granted permissions, because |
| 66 // the extension, not the user, removed the permissions. This allows the |
| 67 // extension to add them again without prompting the user. |
| 68 UpdateActivePermissions(extension, total.get()); |
| 69 |
| 70 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); |
| 71 } |
| 72 |
| 73 void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { |
| 74 CHECK(extension); |
| 75 |
| 76 // We only maintain the granted permissions prefs for extensions that can't |
| 77 // silently increase their permissions. |
| 78 if (extension->CanSilentlyIncreasePermissions()) |
| 79 return; |
| 80 |
| 81 GetExtensionPrefs()->AddGrantedPermissions( |
| 82 extension->id(), extension->GetActivePermissions()); |
| 83 } |
| 84 |
| 85 void PermissionsUpdater::UpdateActivePermissions( |
| 86 const Extension* extension, const ExtensionPermissionSet* permissions) { |
| 87 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); |
| 88 extension->SetActivePermissions(permissions); |
| 89 } |
| 90 |
| 91 void PermissionsUpdater::DispatchEvent( |
| 92 const std::string& extension_id, |
| 93 const char* event_name, |
| 94 const ExtensionPermissionSet* changed_permissions) { |
| 95 if (!profile_ || !profile_->GetExtensionEventRouter()) |
| 96 return; |
| 97 |
| 98 ListValue value; |
| 99 value.Append(PackPermissionsToValue(changed_permissions)); |
| 100 std::string json_value; |
| 101 base::JSONWriter::Write(&value, false, &json_value); |
| 102 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 103 extension_id, event_name, json_value, profile_, GURL()); |
| 104 } |
| 105 |
| 106 void PermissionsUpdater::NotifyPermissionsUpdated( |
| 107 EventType event_type, |
| 108 const Extension* extension, |
| 109 const ExtensionPermissionSet* changed) { |
| 110 if (!changed || changed->IsEmpty()) |
| 111 return; |
| 112 |
| 113 UpdatedExtensionPermissionsInfo::Reason reason; |
| 114 const char* event_name = NULL; |
| 115 |
| 116 if (event_type == REMOVED) { |
| 117 reason = UpdatedExtensionPermissionsInfo::REMOVED; |
| 118 event_name = kOnRemoved; |
| 119 } else { |
| 120 CHECK_EQ(ADDED, event_type); |
| 121 reason = UpdatedExtensionPermissionsInfo::ADDED; |
| 122 event_name = kOnAdded; |
| 123 } |
| 124 |
| 125 // Notify other APIs or interested parties. |
| 126 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo( |
| 127 extension, changed, reason); |
| 128 content::NotificationService::current()->Notify( |
| 129 chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, |
| 130 content::Source<Profile>(profile_), |
| 131 content::Details<UpdatedExtensionPermissionsInfo>(&info)); |
| 132 |
| 133 // Send the new permissions to the renderers. |
| 134 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 135 !i.IsAtEnd(); i.Advance()) { |
| 136 RenderProcessHost* host = i.GetCurrentValue(); |
| 137 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); |
| 138 if (profile_->IsSameProfile(profile)) |
| 139 host->Send(new ExtensionMsg_UpdatePermissions( |
| 140 static_cast<int>(reason), |
| 141 extension->id(), |
| 142 changed->apis(), |
| 143 changed->explicit_hosts(), |
| 144 changed->scriptable_hosts())); |
| 145 } |
| 146 |
| 147 // Trigger the onAdded and onRemoved events in the extension. |
| 148 DispatchEvent(extension->id(), event_name, changed); |
| 149 } |
| 150 |
| 151 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { |
| 152 return profile_->GetExtensionService()->extension_prefs(); |
| 153 } |
| 154 |
| 155 } // namespace extensions |
| OLD | NEW |