| 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" | |
| 17 #include "chrome/common/extensions/api/permissions.h" | 15 #include "chrome/common/extensions/api/permissions.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_messages.h" | 18 #include "chrome/common/extensions/extension_messages.h" |
| 21 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h" | |
| 22 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 24 | 21 |
| 25 using content::RenderProcessHost; | 22 using content::RenderProcessHost; |
| 26 using extensions::permissions_api_helpers::PackPermissionSet; | 23 using extensions::permissions_api_helpers::PackPermissionSet; |
| 27 using extensions::PermissionSet; | |
| 28 | 24 |
| 29 namespace extensions { | 25 namespace extensions { |
| 30 | 26 |
| 31 namespace { | 27 namespace { |
| 32 | 28 |
| 33 const char kOnAdded[] = "permissions.onAdded"; | 29 const char kOnAdded[] = "permissions.onAdded"; |
| 34 const char kOnRemoved[] = "permissions.onRemoved"; | 30 const char kOnRemoved[] = "permissions.onRemoved"; |
| 35 | 31 |
| 36 } | 32 } |
| 37 | 33 |
| 38 PermissionsUpdater::PermissionsUpdater(Profile* profile) | 34 PermissionsUpdater::PermissionsUpdater(Profile* profile) |
| 39 : profile_(profile) {} | 35 : profile_(profile) {} |
| 40 | 36 |
| 41 PermissionsUpdater::~PermissionsUpdater() {} | 37 PermissionsUpdater::~PermissionsUpdater() {} |
| 42 | 38 |
| 43 void PermissionsUpdater::AddPermissions( | 39 void PermissionsUpdater::AddPermissions( |
| 44 const Extension* extension, const PermissionSet* permissions) { | 40 const Extension* extension, const PermissionSet* permissions) { |
| 45 scoped_refptr<const PermissionSet> existing( | 41 scoped_refptr<const PermissionSet> existing( |
| 46 extension->GetActivePermissions()); | 42 extension->GetActivePermissions()); |
| 47 scoped_refptr<PermissionSet> total( | 43 scoped_refptr<PermissionSet> total( |
| 48 PermissionSet::CreateUnion(existing, permissions)); | 44 PermissionSet::CreateUnion(existing, permissions)); |
| 49 scoped_refptr<PermissionSet> added( | 45 scoped_refptr<PermissionSet> added( |
| 50 PermissionSet::CreateDifference(total.get(), existing)); | 46 PermissionSet::CreateDifference(total.get(), existing)); |
| 51 | 47 |
| 52 UpdateActivePermissions(extension, total.get()); | 48 UpdateActivePermissions(extension, total.get()); |
| 53 | 49 |
| 54 // Update the granted permissions so we don't auto-disable the extension. | 50 // Update the granted permissions so we don't auto-disable the extension. |
| 55 GrantActivePermissions(extension, false); | 51 GrantActivePermissions(extension); |
| 56 | 52 |
| 57 NotifyPermissionsUpdated(ADDED, extension, added.get()); | 53 NotifyPermissionsUpdated(ADDED, extension, added.get()); |
| 58 } | 54 } |
| 59 | 55 |
| 60 void PermissionsUpdater::RemovePermissions( | 56 void PermissionsUpdater::RemovePermissions( |
| 61 const Extension* extension, const PermissionSet* permissions) { | 57 const Extension* extension, const PermissionSet* permissions) { |
| 62 scoped_refptr<const PermissionSet> existing( | 58 scoped_refptr<const PermissionSet> existing( |
| 63 extension->GetActivePermissions()); | 59 extension->GetActivePermissions()); |
| 64 scoped_refptr<PermissionSet> total( | 60 scoped_refptr<PermissionSet> total( |
| 65 PermissionSet::CreateDifference(existing, permissions)); | 61 PermissionSet::CreateDifference(existing, permissions)); |
| 66 scoped_refptr<PermissionSet> removed( | 62 scoped_refptr<PermissionSet> removed( |
| 67 PermissionSet::CreateDifference(existing, total.get())); | 63 PermissionSet::CreateDifference(existing, total.get())); |
| 68 | 64 |
| 69 // We update the active permissions, and not the granted permissions, because | 65 // We update the active permissions, and not the granted permissions, because |
| 70 // the extension, not the user, removed the permissions. This allows the | 66 // the extension, not the user, removed the permissions. This allows the |
| 71 // extension to add them again without prompting the user. | 67 // extension to add them again without prompting the user. |
| 72 UpdateActivePermissions(extension, total.get()); | 68 UpdateActivePermissions(extension, total.get()); |
| 73 | 69 |
| 74 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); | 70 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); |
| 75 } | 71 } |
| 76 | 72 |
| 77 void PermissionsUpdater::GrantActivePermissions(const Extension* extension, | 73 void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { |
| 78 bool record_oauth2_grant) { | |
| 79 CHECK(extension); | 74 CHECK(extension); |
| 80 | 75 |
| 81 // We only maintain the granted permissions prefs for INTERNAL and LOAD | 76 // We only maintain the granted permissions prefs for INTERNAL and LOAD |
| 82 // extensions. | 77 // extensions. |
| 83 if (extension->location() != Extension::LOAD && | 78 if (extension->location() != Extension::LOAD && |
| 84 extension->location() != Extension::INTERNAL) | 79 extension->location() != Extension::INTERNAL) |
| 85 return; | 80 return; |
| 86 | 81 |
| 87 scoped_refptr<const PermissionSet> permissions = | 82 GetExtensionPrefs()->AddGrantedPermissions( |
| 88 extension->GetActivePermissions(); | 83 extension->id(), 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); | |
| 98 } | 84 } |
| 99 | 85 |
| 100 void PermissionsUpdater::UpdateActivePermissions( | 86 void PermissionsUpdater::UpdateActivePermissions( |
| 101 const Extension* extension, const PermissionSet* permissions) { | 87 const Extension* extension, const PermissionSet* permissions) { |
| 102 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); | 88 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); |
| 103 extension->SetActivePermissions(permissions); | 89 extension->SetActivePermissions(permissions); |
| 104 } | 90 } |
| 105 | 91 |
| 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 | |
| 119 void PermissionsUpdater::DispatchEvent( | 92 void PermissionsUpdater::DispatchEvent( |
| 120 const std::string& extension_id, | 93 const std::string& extension_id, |
| 121 const char* event_name, | 94 const char* event_name, |
| 122 const PermissionSet* changed_permissions) { | 95 const PermissionSet* changed_permissions) { |
| 123 if (!profile_ || !profile_->GetExtensionEventRouter()) | 96 if (!profile_ || !profile_->GetExtensionEventRouter()) |
| 124 return; | 97 return; |
| 125 | 98 |
| 126 ListValue value; | 99 ListValue value; |
| 127 scoped_ptr<api::permissions::Permissions> permissions = | 100 scoped_ptr<api::permissions::Permissions> permissions = |
| 128 PackPermissionSet(changed_permissions); | 101 PackPermissionSet(changed_permissions); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 149 |
| 177 // Trigger the onAdded and onRemoved events in the extension. | 150 // Trigger the onAdded and onRemoved events in the extension. |
| 178 DispatchEvent(extension->id(), event_name, changed); | 151 DispatchEvent(extension->id(), event_name, changed); |
| 179 } | 152 } |
| 180 | 153 |
| 181 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { | 154 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { |
| 182 return profile_->GetExtensionService()->extension_prefs(); | 155 return profile_->GetExtensionService()->extension_prefs(); |
| 183 } | 156 } |
| 184 | 157 |
| 185 } // namespace extensions | 158 } // namespace extensions |
| OLD | NEW |