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

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

Issue 10443105: Take 2 at implementing activeTab. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: many more tests Created 8 years, 6 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
(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/active_tab_permission_manager.h"
6
7 #include "base/memory/ref_counted.h"
Aaron Boodman 2012/06/08 05:31:30 Is this used?
not at google - send to devlin 2012/06/12 20:40:51 Done.
8 #include "chrome/browser/extensions/extension_tab_helper.h"
9 #include "chrome/browser/extensions/extension_tab_util.h"
Aaron Boodman 2012/06/08 05:31:30 Is this used?
not at google - send to devlin 2012/06/12 20:40:51 Done.
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/tab_contents/tab_contents.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/extensions/extension_messages.h"
15 #include "chrome/common/extensions/extension_permission_set.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/web_contents.h"
22
23 using content::RenderProcessHost;
24 using content::WebContentsObserver;
25
26 namespace extensions {
27
28 namespace {
29
30 // Sends a copy of |message| to every RenderProcessHost in |profile|.
31 template <class E>
Aaron Boodman 2012/06/08 05:31:30 Why 'E'? How about 'Message' or 'T' (traditional t
not at google - send to devlin 2012/06/12 20:40:51 Done. Dunno why E, weird.
32 void SendToRenderers(Profile* profile, const E& message) {
33 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
34 !i.IsAtEnd(); i.Advance()) {
35 RenderProcessHost* host = i.GetCurrentValue();
36 Profile* host_profile =
37 Profile::FromBrowserContext(host->GetBrowserContext());
38 if (host_profile->IsSameProfile(profile))
39 host->Send(new E(message));
40 }
41 }
42
43 }
44
45 ActiveTabPermissionManager::ActiveTabPermissionManager(
46 TabContents* tab_contents)
47 : WebContentsObserver(tab_contents->web_contents()),
48 tab_contents_(tab_contents) {
49 InsertActiveURL(web_contents()->GetURL());
50 registrar_.Add(this,
51 chrome::NOTIFICATION_EXTENSION_UNLOADED,
52 content::Source<Profile>(tab_contents->profile()));
53 }
54
55 ActiveTabPermissionManager::~ActiveTabPermissionManager() {}
56
57 void ActiveTabPermissionManager::MaybeGrant(const Extension* extension) {
Aaron Boodman 2012/06/08 05:31:30 GrantIfRequested? GrantIfWanted?
not at google - send to devlin 2012/06/12 20:40:51 Done.
58 CHECK(active_urls_.size() > 0);
59 if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab))
60 return;
61
62 // Only need to check the number of permissions here rather than the URLs
63 // themselves, because the set can only ever grow.
64 const URLPatternSet* old_permissions =
65 extension->GetTabSpecificHostPermissions(tab_id());
66 if (old_permissions && old_permissions->size() == active_urls_.size())
Aaron Boodman 2012/06/08 05:31:30 Add parens to made desired order of operation clea
not at google - send to devlin 2012/06/12 20:40:51 Done.
67 return;
68
69 granted_.Insert(extension);
70 extension->SetTabSpecificHostPermissions(tab_id(), active_urls_);
71 SendToRenderers(
72 tab_contents_->profile(),
73 ExtensionMsg_SetTabSpecificPermissions(GetPageID(),
74 tab_id(),
75 extension->id(),
76 active_urls_));
77 }
78
79 void ActiveTabPermissionManager::DidCommitProvisionalLoadForFrame(
80 int64 frame_id,
81 bool is_main_frame,
82 const GURL& url,
83 content::PageTransition transition_type,
84 content::RenderViewHost* render_view_host) {
85 if (is_main_frame)
86 ClearActiveURLsAndNotify();
87 InsertActiveURL(url);
88 }
89
90 void ActiveTabPermissionManager::WebContentsDestroyed(
91 content::WebContents* web_contents) {
92 ClearActiveURLsAndNotify();
93 }
94
95 void ActiveTabPermissionManager::Observe(
96 int type,
97 const content::NotificationSource& source,
98 const content::NotificationDetails& details) {
99 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
100 const Extension* extension =
101 content::Details<UnloadedExtensionInfo>(details)->extension;
102 extension->ClearTabSpecificHostPermissions(tab_id());
103 SendToRenderers(
104 tab_contents_->profile(),
105 ExtensionMsg_ClearTabSpecificPermissions(
106 tab_id(),
107 std::vector<std::string>(1, extension->id())));
108 granted_.Remove(extension->id());
109 }
110
111 void ActiveTabPermissionManager::ClearActiveURLsAndNotify() {
112 active_urls_.ClearPatterns();
113
114 // The rest of this method deals with informing the extension(s) that the
115 // permissions for this tab need to be cleared. No need if there aren't any.
116 if (granted_.is_empty())
Aaron Boodman 2012/06/08 05:31:30 Nit: I think this is clear w/o the comment.
not at google - send to devlin 2012/06/12 20:40:51 Done.
117 return;
118
119 std::vector<std::string> extension_ids;
120
121 for (ExtensionSet::const_iterator it = granted_.begin();
122 it != granted_.end(); ++it) {
123 (*it)->ClearTabSpecificHostPermissions(tab_id());
124 extension_ids.push_back((*it)->id());
125 }
126 granted_.Clear();
127
128 SendToRenderers(
129 tab_contents_->profile(),
130 ExtensionMsg_ClearTabSpecificPermissions(tab_id(), extension_ids));
131 }
132
133 void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) {
134 URLPattern pattern(UserScript::kValidUserScriptSchemes);
135 if (pattern.Parse(url.spec()) == URLPattern::PARSE_SUCCESS)
Aaron Boodman 2012/06/08 05:31:30 If this doesn't parse something has gone very wron
not at google - send to devlin 2012/06/12 20:40:51 Some legitimate reasons it won't parse is for thin
136 active_urls_.AddPattern(pattern);
137 }
138
139 int32 ActiveTabPermissionManager::tab_id() {
Aaron Boodman 2012/06/08 05:31:30 This method should be PascalCase because GetTabId
not at google - send to devlin 2012/06/12 20:40:51 I think I'll change the one on ExtensionTabHelper
140 return tab_contents_->extension_tab_helper()->GetTabId();
141 }
142
143 int32 ActiveTabPermissionManager::GetPageID() {
144 return tab_contents_->web_contents()->GetController().GetActiveEntry()->
145 GetPageID();
146 }
147
148 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698