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

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: uninstall, tests, aa 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"
8 #include "chrome/browser/extensions/extension_tab_helper.h"
9 #include "chrome/browser/extensions/extension_tab_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.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>
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 TabContentsWrapper* tab_contents)
47 : WebContentsObserver(tab_contents->web_contents()),
48 tab_contents_(tab_contents) {
49 InsertActiveURL(web_contents()->GetURL());
50 }
51
52 ActiveTabPermissionManager::~ActiveTabPermissionManager() {}
53
54 void ActiveTabPermissionManager::MaybeGrant(const Extension* extension) {
55 if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab))
56 return;
57
58 int tab_id = tab_contents_->extension_tab_helper()->GetTabId();
59
60 // Only need to check the number of permissions here rather than the URLs
61 // themselves, because the set can only ever grow.
62 const URLPatternSet* old_permissions =
63 extension->GetTabSpecificHostPermissions(tab_id);
64 if (old_permissions && old_permissions->size() == active_urls_.size())
65 return;
66
67 granted_.Insert(extension);
68 extension->SetTabSpecificHostPermissions(tab_id, active_urls_);
69 SendToRenderers(
70 tab_contents_->profile(),
71 ExtensionMsg_SetTabSpecificPermissions(GetPageID(),
72 tab_id,
73 extension->id(),
74 active_urls_));
75 }
76
77 void ActiveTabPermissionManager::DidCommitProvisionalLoadForFrame(
78 int64 frame_id,
79 bool is_main_frame,
80 const GURL& url,
81 content::PageTransition transition_type,
82 content::RenderViewHost* render_view_host) {
83 if (is_main_frame)
84 ClearActiveURLsAndNotify();
85 InsertActiveURL(url);
86 }
87
88 void ActiveTabPermissionManager::WebContentsDestroyed(
89 content::WebContents* web_contents) {
90 ClearActiveURLsAndNotify();
91 }
92
93 void ActiveTabPermissionManager::Observe(
94 int type,
95 const content::NotificationSource& source,
96 const content::NotificationDetails& details) {
97 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
98 const Extension* extension =
99 content::Details<UnloadedExtensionInfo>(details)->extension;
100 granted_.Remove(extension->id());
101 }
102
103 void ActiveTabPermissionManager::ClearActiveURLsAndNotify() {
104 active_urls_.ClearPatterns();
105
106 // The rest of this method deals with informing the extension(s) that the
107 // permissions for this tab need to be cleared. No need if there aren't any.
108 if (granted_.is_empty())
109 return;
110
111 int tab_id = tab_contents_->extension_tab_helper()->GetTabId();
112 std::vector<std::string> extension_ids;
113
114 for (ExtensionSet::const_iterator it = granted_.begin();
115 it != granted_.end(); ++it) {
116 (*it)->ClearTabSpecificHostPermissions(tab_id);
117 extension_ids.push_back((*it)->id());
118 }
119 granted_.Clear();
120
121 SendToRenderers(
122 tab_contents_->profile(),
123 ExtensionMsg_ClearTabSpecificPermissions(tab_id, extension_ids));
124 }
125
126 void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) {
127 URLPattern pattern(UserScript::kValidUserScriptSchemes);
128 if (pattern.Parse(url.spec()) == URLPattern::PARSE_SUCCESS)
129 active_urls_.AddPattern(pattern);
130 }
131
132 int32 ActiveTabPermissionManager::GetPageID() {
133 return tab_contents_->web_contents()->GetController().GetActiveEntry()->
134 GetPageID();
135 }
136
137 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698