| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/permissions_updater.h" | 5 #include "chrome/browser/extensions/permissions_updater.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 10 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
| 11 #include "chrome/browser/extensions/extension_event_router.h" | 11 #include "chrome/browser/extensions/extension_event_router.h" |
| 12 #include "chrome/browser/extensions/extension_prefs.h" | 12 #include "chrome/browser/extensions/extension_prefs.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/signin/token_service.h" |
| 16 #include "chrome/browser/signin/token_service_factory.h" |
| 15 #include "chrome/common/extensions/api/permissions.h" | 17 #include "chrome/common/extensions/api/permissions.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_messages.h" | 20 #include "chrome/common/extensions/extension_messages.h" |
| 21 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h" |
| 19 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 21 | 24 |
| 22 using content::RenderProcessHost; | 25 using content::RenderProcessHost; |
| 23 using extensions::permissions_api_helpers::PackPermissionSet; | 26 using extensions::permissions_api_helpers::PackPermissionSet; |
| 27 using extensions::PermissionSet; |
| 24 | 28 |
| 25 namespace extensions { | 29 namespace extensions { |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 const char kOnAdded[] = "permissions.onAdded"; | 33 const char kOnAdded[] = "permissions.onAdded"; |
| 30 const char kOnRemoved[] = "permissions.onRemoved"; | 34 const char kOnRemoved[] = "permissions.onRemoved"; |
| 31 | 35 |
| 32 } | 36 } |
| 33 | 37 |
| 34 PermissionsUpdater::PermissionsUpdater(Profile* profile) | 38 PermissionsUpdater::PermissionsUpdater(Profile* profile) |
| 35 : profile_(profile) {} | 39 : profile_(profile) {} |
| 36 | 40 |
| 37 PermissionsUpdater::~PermissionsUpdater() {} | 41 PermissionsUpdater::~PermissionsUpdater() {} |
| 38 | 42 |
| 39 void PermissionsUpdater::AddPermissions( | 43 void PermissionsUpdater::AddPermissions( |
| 40 const Extension* extension, const PermissionSet* permissions) { | 44 const Extension* extension, const PermissionSet* permissions) { |
| 41 scoped_refptr<const PermissionSet> existing( | 45 scoped_refptr<const PermissionSet> existing( |
| 42 extension->GetActivePermissions()); | 46 extension->GetActivePermissions()); |
| 43 scoped_refptr<PermissionSet> total( | 47 scoped_refptr<PermissionSet> total( |
| 44 PermissionSet::CreateUnion(existing, permissions)); | 48 PermissionSet::CreateUnion(existing, permissions)); |
| 45 scoped_refptr<PermissionSet> added( | 49 scoped_refptr<PermissionSet> added( |
| 46 PermissionSet::CreateDifference(total.get(), existing)); | 50 PermissionSet::CreateDifference(total.get(), existing)); |
| 47 | 51 |
| 48 UpdateActivePermissions(extension, total.get()); | 52 UpdateActivePermissions(extension, total.get()); |
| 49 | 53 |
| 50 // Update the granted permissions so we don't auto-disable the extension. | 54 // Update the granted permissions so we don't auto-disable the extension. |
| 51 GrantActivePermissions(extension); | 55 GrantActivePermissions(extension, false); |
| 52 | 56 |
| 53 NotifyPermissionsUpdated(ADDED, extension, added.get()); | 57 NotifyPermissionsUpdated(ADDED, extension, added.get()); |
| 54 } | 58 } |
| 55 | 59 |
| 56 void PermissionsUpdater::RemovePermissions( | 60 void PermissionsUpdater::RemovePermissions( |
| 57 const Extension* extension, const PermissionSet* permissions) { | 61 const Extension* extension, const PermissionSet* permissions) { |
| 58 scoped_refptr<const PermissionSet> existing( | 62 scoped_refptr<const PermissionSet> existing( |
| 59 extension->GetActivePermissions()); | 63 extension->GetActivePermissions()); |
| 60 scoped_refptr<PermissionSet> total( | 64 scoped_refptr<PermissionSet> total( |
| 61 PermissionSet::CreateDifference(existing, permissions)); | 65 PermissionSet::CreateDifference(existing, permissions)); |
| 62 scoped_refptr<PermissionSet> removed( | 66 scoped_refptr<PermissionSet> removed( |
| 63 PermissionSet::CreateDifference(existing, total.get())); | 67 PermissionSet::CreateDifference(existing, total.get())); |
| 64 | 68 |
| 65 // We update the active permissions, and not the granted permissions, because | 69 // We update the active permissions, and not the granted permissions, because |
| 66 // the extension, not the user, removed the permissions. This allows the | 70 // the extension, not the user, removed the permissions. This allows the |
| 67 // extension to add them again without prompting the user. | 71 // extension to add them again without prompting the user. |
| 68 UpdateActivePermissions(extension, total.get()); | 72 UpdateActivePermissions(extension, total.get()); |
| 69 | 73 |
| 70 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); | 74 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); |
| 71 } | 75 } |
| 72 | 76 |
| 73 void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { | 77 void PermissionsUpdater::GrantActivePermissions(const Extension* extension, |
| 78 bool record_oauth2_grant) { |
| 74 CHECK(extension); | 79 CHECK(extension); |
| 75 | 80 |
| 76 // We only maintain the granted permissions prefs for INTERNAL and LOAD | 81 // We only maintain the granted permissions prefs for INTERNAL and LOAD |
| 77 // extensions. | 82 // extensions. |
| 78 if (extension->location() != Extension::LOAD && | 83 if (extension->location() != Extension::LOAD && |
| 79 extension->location() != Extension::INTERNAL) | 84 extension->location() != Extension::INTERNAL) |
| 80 return; | 85 return; |
| 81 | 86 |
| 82 GetExtensionPrefs()->AddGrantedPermissions( | 87 scoped_refptr<const PermissionSet> permissions = |
| 83 extension->id(), extension->GetActivePermissions()); | 88 extension->GetActivePermissions(); |
| 89 if (record_oauth2_grant) { |
| 90 RecordOAuth2Grant(extension); |
| 91 } else { |
| 92 scoped_refptr<PermissionSet> scopes = |
| 93 new PermissionSet(permissions->scopes()); |
| 94 permissions = PermissionSet::CreateDifference(permissions, scopes); |
| 95 } |
| 96 |
| 97 GetExtensionPrefs()->AddGrantedPermissions(extension->id(), permissions); |
| 84 } | 98 } |
| 85 | 99 |
| 86 void PermissionsUpdater::UpdateActivePermissions( | 100 void PermissionsUpdater::UpdateActivePermissions( |
| 87 const Extension* extension, const PermissionSet* permissions) { | 101 const Extension* extension, const PermissionSet* permissions) { |
| 88 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); | 102 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); |
| 89 extension->SetActivePermissions(permissions); | 103 extension->SetActivePermissions(permissions); |
| 90 } | 104 } |
| 91 | 105 |
| 106 void PermissionsUpdater::RecordOAuth2Grant(const Extension* extension) { |
| 107 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
| 108 OAuth2MintTokenFlow* flow = new OAuth2MintTokenFlow( |
| 109 profile_->GetRequestContext(), NULL, OAuth2MintTokenFlow::Parameters( |
| 110 token_service->GetOAuth2LoginRefreshToken(), |
| 111 extension->id(), |
| 112 extension->oauth2_info().client_id, |
| 113 extension->oauth2_info().scopes, |
| 114 OAuth2MintTokenFlow::MODE_RECORD_GRANT)); |
| 115 // |flow| will delete itself. |
| 116 flow->FireAndForget(); |
| 117 } |
| 118 |
| 92 void PermissionsUpdater::DispatchEvent( | 119 void PermissionsUpdater::DispatchEvent( |
| 93 const std::string& extension_id, | 120 const std::string& extension_id, |
| 94 const char* event_name, | 121 const char* event_name, |
| 95 const PermissionSet* changed_permissions) { | 122 const PermissionSet* changed_permissions) { |
| 96 if (!profile_ || !profile_->GetExtensionEventRouter()) | 123 if (!profile_ || !profile_->GetExtensionEventRouter()) |
| 97 return; | 124 return; |
| 98 | 125 |
| 99 ListValue value; | 126 ListValue value; |
| 100 scoped_ptr<api::permissions::Permissions> permissions = | 127 scoped_ptr<api::permissions::Permissions> permissions = |
| 101 PackPermissionSet(changed_permissions); | 128 PackPermissionSet(changed_permissions); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 176 |
| 150 // Trigger the onAdded and onRemoved events in the extension. | 177 // Trigger the onAdded and onRemoved events in the extension. |
| 151 DispatchEvent(extension->id(), event_name, changed); | 178 DispatchEvent(extension->id(), event_name, changed); |
| 152 } | 179 } |
| 153 | 180 |
| 154 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { | 181 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { |
| 155 return profile_->GetExtensionService()->extension_prefs(); | 182 return profile_->GetExtensionService()->extension_prefs(); |
| 156 } | 183 } |
| 157 | 184 |
| 158 } // namespace extensions | 185 } // namespace extensions |
| OLD | NEW |