| 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/active_tab_permission_granter.h" | 5 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 content::WebContents* web_contents, int tab_id, Profile* profile) | 30 content::WebContents* web_contents, int tab_id, Profile* profile) |
| 31 : WebContentsObserver(web_contents), tab_id_(tab_id) { | 31 : WebContentsObserver(web_contents), tab_id_(tab_id) { |
| 32 registrar_.Add(this, | 32 registrar_.Add(this, |
| 33 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 33 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 34 content::Source<Profile>(profile)); | 34 content::Source<Profile>(profile)); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ActiveTabPermissionGranter::~ActiveTabPermissionGranter() {} | 37 ActiveTabPermissionGranter::~ActiveTabPermissionGranter() {} |
| 38 | 38 |
| 39 void ActiveTabPermissionGranter::GrantIfRequested(const Extension* extension) { | 39 void ActiveTabPermissionGranter::GrantIfRequested(const Extension* extension) { |
| 40 if (!extension->HasAPIPermission(extensions::APIPermission::kActiveTab) && | 40 if (granted_extensions_.Contains(extension->id())) |
| 41 !extension->HasAPIPermission(extensions::APIPermission::kTabCapture)) { | |
| 42 return; | 41 return; |
| 42 |
| 43 APIPermissionSet new_apis; |
| 44 URLPatternSet new_hosts; |
| 45 |
| 46 if (extension->HasAPIPermission(APIPermission::kActiveTab)) { |
| 47 URLPattern pattern(UserScript::ValidUserScriptSchemes()); |
| 48 // Pattern parsing could fail if this is an unsupported URL e.g. chrome://. |
| 49 if (pattern.Parse(web_contents()->GetURL().spec()) == |
| 50 URLPattern::PARSE_SUCCESS) { |
| 51 new_hosts.AddPattern(pattern); |
| 52 new_apis.insert(APIPermission::kTab); |
| 53 granted_extensions_.Insert(extension); |
| 54 } |
| 43 } | 55 } |
| 44 | 56 |
| 45 if (IsGranted(extension)) | 57 if (extension->HasAPIPermission(APIPermission::kTabCapture)) |
| 46 return; | 58 new_apis.insert(APIPermission::kTabCaptureForTab); |
| 47 | 59 |
| 48 URLPattern pattern(UserScript::ValidUserScriptSchemes()); | 60 if (!new_apis.empty()) { |
| 49 if (pattern.Parse(web_contents()->GetURL().spec()) != | 61 scoped_refptr<const PermissionSet> new_permissions = |
| 50 URLPattern::PARSE_SUCCESS) { | 62 new PermissionSet(new_apis, new_hosts, URLPatternSet()); |
| 51 // Pattern parsing could fail if this is an unsupported URL e.g. chrome://. | 63 PermissionsData::UpdateTabSpecificPermissions(extension, |
| 52 return; | 64 tab_id_, |
| 65 new_permissions); |
| 66 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(), |
| 67 tab_id_, |
| 68 extension->id(), |
| 69 new_hosts)); |
| 53 } | 70 } |
| 54 | |
| 55 APIPermissionSet new_apis; | |
| 56 new_apis.insert(APIPermission::kTab); | |
| 57 URLPatternSet new_hosts; | |
| 58 new_hosts.AddPattern(pattern); | |
| 59 scoped_refptr<const PermissionSet> new_permissions = | |
| 60 new PermissionSet(new_apis, new_hosts, URLPatternSet()); | |
| 61 | |
| 62 PermissionsData::UpdateTabSpecificPermissions(extension, | |
| 63 tab_id_, | |
| 64 new_permissions); | |
| 65 granted_extensions_.Insert(extension); | |
| 66 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(), | |
| 67 tab_id_, | |
| 68 extension->id(), | |
| 69 new_hosts)); | |
| 70 } | |
| 71 | |
| 72 bool ActiveTabPermissionGranter::IsGranted(const Extension* extension) { | |
| 73 return granted_extensions_.Contains(extension->id()); | |
| 74 } | 71 } |
| 75 | 72 |
| 76 void ActiveTabPermissionGranter::DidNavigateMainFrame( | 73 void ActiveTabPermissionGranter::DidNavigateMainFrame( |
| 77 const content::LoadCommittedDetails& details, | 74 const content::LoadCommittedDetails& details, |
| 78 const content::FrameNavigateParams& params) { | 75 const content::FrameNavigateParams& params) { |
| 79 if (details.is_in_page) | 76 if (details.is_in_page) |
| 80 return; | 77 return; |
| 81 DCHECK(details.is_main_frame); // important: sub-frames don't get granted! | 78 DCHECK(details.is_main_frame); // important: sub-frames don't get granted! |
| 82 ClearActiveExtensionsAndNotify(); | 79 ClearActiveExtensionsAndNotify(); |
| 83 } | 80 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 113 | 110 |
| 114 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id_, extension_ids)); | 111 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id_, extension_ids)); |
| 115 granted_extensions_.Clear(); | 112 granted_extensions_.Clear(); |
| 116 } | 113 } |
| 117 | 114 |
| 118 int32 ActiveTabPermissionGranter::GetPageID() { | 115 int32 ActiveTabPermissionGranter::GetPageID() { |
| 119 return web_contents()->GetController().GetVisibleEntry()->GetPageID(); | 116 return web_contents()->GetController().GetVisibleEntry()->GetPageID(); |
| 120 } | 117 } |
| 121 | 118 |
| 122 } // namespace extensions | 119 } // namespace extensions |
| OLD | NEW |