Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chrome/browser/extensions/active_tab_permission_granter.cc

Issue 10880064: Make extension commands grant the activeTab permission. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_manager.h" 5 #include "chrome/browser/extensions/active_tab_permission_granter.h"
6 6
7 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_system.h" 8 #include "chrome/browser/extensions/extension_system.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sessions/session_id.h" 10 #include "chrome/browser/sessions/session_id.h"
11 #include "chrome/browser/ui/tab_contents/tab_contents.h" 11 #include "chrome/browser/ui/tab_contents/tab_contents.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/extensions/extension_messages.h" 14 #include "chrome/common/extensions/extension_messages.h"
15 #include "chrome/common/extensions/permissions/permission_set.h" 15 #include "chrome/common/extensions/permissions/permission_set.h"
16 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/navigation_details.h" 17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/render_process_host.h" 20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 22
23 using content::RenderProcessHost; 23 using content::RenderProcessHost;
24 using content::WebContentsObserver; 24 using content::WebContentsObserver;
25 25
26 namespace extensions { 26 namespace extensions {
27 27
28 ActiveTabPermissionManager::ActiveTabPermissionManager( 28 ActiveTabPermissionGranter::ActiveTabPermissionGranter(
29 content::WebContents* web_contents, int tab_id, Profile* profile) 29 content::WebContents* web_contents, int tab_id, Profile* profile)
30 : WebContentsObserver(web_contents), tab_id_(tab_id) { 30 : WebContentsObserver(web_contents), tab_id_(tab_id) {
31 registrar_.Add(this, 31 registrar_.Add(this,
32 chrome::NOTIFICATION_EXTENSION_UNLOADED, 32 chrome::NOTIFICATION_EXTENSION_UNLOADED,
33 content::Source<Profile>(profile)); 33 content::Source<Profile>(profile));
34 } 34 }
35 35
36 ActiveTabPermissionManager::~ActiveTabPermissionManager() {} 36 ActiveTabPermissionGranter::~ActiveTabPermissionGranter() {}
37 37
38 void ActiveTabPermissionManager::GrantIfRequested(const Extension* extension) { 38 void ActiveTabPermissionGranter::GrantIfRequested(const Extension* extension) {
39 if (!extension->HasAPIPermission(extensions::APIPermission::kActiveTab)) 39 if (!extension->HasAPIPermission(extensions::APIPermission::kActiveTab))
40 return; 40 return;
41 41
42 if (IsGranted(extension)) 42 if (IsGranted(extension))
43 return; 43 return;
44 44
45 URLPattern pattern(UserScript::kValidUserScriptSchemes); 45 URLPattern pattern(UserScript::kValidUserScriptSchemes);
46 if (pattern.Parse(web_contents()->GetURL().spec()) != 46 if (pattern.Parse(web_contents()->GetURL().spec()) !=
47 URLPattern::PARSE_SUCCESS) { 47 URLPattern::PARSE_SUCCESS) {
48 // Pattern parsing could fail if this is an unsupported URL e.g. chrome://. 48 // Pattern parsing could fail if this is an unsupported URL e.g. chrome://.
49 return; 49 return;
50 } 50 }
51 51
52 APIPermissionSet new_apis; 52 APIPermissionSet new_apis;
53 new_apis.insert(APIPermission::kTab); 53 new_apis.insert(APIPermission::kTab);
54 URLPatternSet new_hosts; 54 URLPatternSet new_hosts;
55 new_hosts.AddPattern(pattern); 55 new_hosts.AddPattern(pattern);
56 scoped_refptr<const PermissionSet> new_permissions = 56 scoped_refptr<const PermissionSet> new_permissions =
57 new PermissionSet(new_apis, new_hosts, URLPatternSet()); 57 new PermissionSet(new_apis, new_hosts, URLPatternSet());
58 58
59 extension->UpdateTabSpecificPermissions(tab_id_, new_permissions); 59 extension->UpdateTabSpecificPermissions(tab_id_, new_permissions);
60 granted_extensions_.Insert(extension); 60 granted_extensions_.Insert(extension);
61 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(), 61 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(),
62 tab_id_, 62 tab_id_,
63 extension->id(), 63 extension->id(),
64 new_hosts)); 64 new_hosts));
65 } 65 }
66 66
67 bool ActiveTabPermissionManager::IsGranted(const Extension* extension) { 67 bool ActiveTabPermissionGranter::IsGranted(const Extension* extension) {
68 return granted_extensions_.Contains(extension->id()); 68 return granted_extensions_.Contains(extension->id());
69 } 69 }
70 70
71 void ActiveTabPermissionManager::DidNavigateMainFrame( 71 void ActiveTabPermissionGranter::DidNavigateMainFrame(
72 const content::LoadCommittedDetails& details, 72 const content::LoadCommittedDetails& details,
73 const content::FrameNavigateParams& params) { 73 const content::FrameNavigateParams& params) {
74 if (details.is_in_page) 74 if (details.is_in_page)
75 return; 75 return;
76 DCHECK(details.is_main_frame); // important: sub-frames don't get granted! 76 DCHECK(details.is_main_frame); // important: sub-frames don't get granted!
77 ClearActiveExtensionsAndNotify(); 77 ClearActiveExtensionsAndNotify();
78 } 78 }
79 79
80 void ActiveTabPermissionManager::WebContentsDestroyed( 80 void ActiveTabPermissionGranter::WebContentsDestroyed(
81 content::WebContents* web_contents) { 81 content::WebContents* web_contents) {
82 ClearActiveExtensionsAndNotify(); 82 ClearActiveExtensionsAndNotify();
83 } 83 }
84 84
85 void ActiveTabPermissionManager::Observe( 85 void ActiveTabPermissionGranter::Observe(
86 int type, 86 int type,
87 const content::NotificationSource& source, 87 const content::NotificationSource& source,
88 const content::NotificationDetails& details) { 88 const content::NotificationDetails& details) {
89 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 89 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
90 const Extension* extension = 90 const Extension* extension =
91 content::Details<UnloadedExtensionInfo>(details)->extension; 91 content::Details<UnloadedExtensionInfo>(details)->extension;
92 // Note: don't need to clear the permissions (nor tell the renderer about it) 92 // Note: don't need to clear the permissions (nor tell the renderer about it)
93 // because it's being unloaded anyway. 93 // because it's being unloaded anyway.
94 granted_extensions_.Remove(extension->id()); 94 granted_extensions_.Remove(extension->id());
95 } 95 }
96 96
97 void ActiveTabPermissionManager::ClearActiveExtensionsAndNotify() { 97 void ActiveTabPermissionGranter::ClearActiveExtensionsAndNotify() {
98 if (granted_extensions_.is_empty()) 98 if (granted_extensions_.is_empty())
99 return; 99 return;
100 100
101 std::vector<std::string> extension_ids; 101 std::vector<std::string> extension_ids;
102 102
103 for (ExtensionSet::const_iterator it = granted_extensions_.begin(); 103 for (ExtensionSet::const_iterator it = granted_extensions_.begin();
104 it != granted_extensions_.end(); ++it) { 104 it != granted_extensions_.end(); ++it) {
105 (*it)->ClearTabSpecificPermissions(tab_id_); 105 (*it)->ClearTabSpecificPermissions(tab_id_);
106 extension_ids.push_back((*it)->id()); 106 extension_ids.push_back((*it)->id());
107 } 107 }
108 108
109 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id_, extension_ids)); 109 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id_, extension_ids));
110 granted_extensions_.Clear(); 110 granted_extensions_.Clear();
111 } 111 }
112 112
113 int32 ActiveTabPermissionManager::GetPageID() { 113 int32 ActiveTabPermissionGranter::GetPageID() {
114 return web_contents()->GetController().GetActiveEntry()->GetPageID(); 114 return web_contents()->GetController().GetActiveEntry()->GetPageID();
115 } 115 }
116 116
117 } // namespace extensions 117 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/active_tab_permission_granter.h ('k') | chrome/browser/extensions/active_tab_permission_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698