Chromium Code Reviews| Index: chrome/browser/extensions/active_tab_permission_manager.cc |
| diff --git a/chrome/browser/extensions/active_tab_permission_manager.cc b/chrome/browser/extensions/active_tab_permission_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1eab725f3417ed26db011544dabf9956c438718a |
| --- /dev/null |
| +++ b/chrome/browser/extensions/active_tab_permission_manager.cc |
| @@ -0,0 +1,148 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/active_tab_permission_manager.h" |
| + |
| +#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.
|
| +#include "chrome/browser/extensions/extension_tab_helper.h" |
| +#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.
|
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/extension_messages.h" |
| +#include "chrome/common/extensions/extension_permission_set.h" |
| +#include "content/public/browser/navigation_entry.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +using content::RenderProcessHost; |
| +using content::WebContentsObserver; |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +// Sends a copy of |message| to every RenderProcessHost in |profile|. |
| +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.
|
| +void SendToRenderers(Profile* profile, const E& message) { |
| + for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| + !i.IsAtEnd(); i.Advance()) { |
| + RenderProcessHost* host = i.GetCurrentValue(); |
| + Profile* host_profile = |
| + Profile::FromBrowserContext(host->GetBrowserContext()); |
| + if (host_profile->IsSameProfile(profile)) |
| + host->Send(new E(message)); |
| + } |
| +} |
| + |
| +} |
| + |
| +ActiveTabPermissionManager::ActiveTabPermissionManager( |
| + TabContents* tab_contents) |
| + : WebContentsObserver(tab_contents->web_contents()), |
| + tab_contents_(tab_contents) { |
| + InsertActiveURL(web_contents()->GetURL()); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| + content::Source<Profile>(tab_contents->profile())); |
| +} |
| + |
| +ActiveTabPermissionManager::~ActiveTabPermissionManager() {} |
| + |
| +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.
|
| + CHECK(active_urls_.size() > 0); |
| + if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab)) |
| + return; |
| + |
| + // Only need to check the number of permissions here rather than the URLs |
| + // themselves, because the set can only ever grow. |
| + const URLPatternSet* old_permissions = |
| + extension->GetTabSpecificHostPermissions(tab_id()); |
| + 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.
|
| + return; |
| + |
| + granted_.Insert(extension); |
| + extension->SetTabSpecificHostPermissions(tab_id(), active_urls_); |
| + SendToRenderers( |
| + tab_contents_->profile(), |
| + ExtensionMsg_SetTabSpecificPermissions(GetPageID(), |
| + tab_id(), |
| + extension->id(), |
| + active_urls_)); |
| +} |
| + |
| +void ActiveTabPermissionManager::DidCommitProvisionalLoadForFrame( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& url, |
| + content::PageTransition transition_type, |
| + content::RenderViewHost* render_view_host) { |
| + if (is_main_frame) |
| + ClearActiveURLsAndNotify(); |
| + InsertActiveURL(url); |
| +} |
| + |
| +void ActiveTabPermissionManager::WebContentsDestroyed( |
| + content::WebContents* web_contents) { |
| + ClearActiveURLsAndNotify(); |
| +} |
| + |
| +void ActiveTabPermissionManager::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| + const Extension* extension = |
| + content::Details<UnloadedExtensionInfo>(details)->extension; |
| + extension->ClearTabSpecificHostPermissions(tab_id()); |
| + SendToRenderers( |
| + tab_contents_->profile(), |
| + ExtensionMsg_ClearTabSpecificPermissions( |
| + tab_id(), |
| + std::vector<std::string>(1, extension->id()))); |
| + granted_.Remove(extension->id()); |
| +} |
| + |
| +void ActiveTabPermissionManager::ClearActiveURLsAndNotify() { |
| + active_urls_.ClearPatterns(); |
| + |
| + // The rest of this method deals with informing the extension(s) that the |
| + // permissions for this tab need to be cleared. No need if there aren't any. |
| + 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.
|
| + return; |
| + |
| + std::vector<std::string> extension_ids; |
| + |
| + for (ExtensionSet::const_iterator it = granted_.begin(); |
| + it != granted_.end(); ++it) { |
| + (*it)->ClearTabSpecificHostPermissions(tab_id()); |
| + extension_ids.push_back((*it)->id()); |
| + } |
| + granted_.Clear(); |
| + |
| + SendToRenderers( |
| + tab_contents_->profile(), |
| + ExtensionMsg_ClearTabSpecificPermissions(tab_id(), extension_ids)); |
| +} |
| + |
| +void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) { |
| + URLPattern pattern(UserScript::kValidUserScriptSchemes); |
| + 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
|
| + active_urls_.AddPattern(pattern); |
| +} |
| + |
| +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
|
| + return tab_contents_->extension_tab_helper()->GetTabId(); |
| +} |
| + |
| +int32 ActiveTabPermissionManager::GetPageID() { |
| + return tab_contents_->web_contents()->GetController().GetActiveEntry()-> |
| + GetPageID(); |
| +} |
| + |
| +} // namespace extensions |