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..607d7283b7754a858166dc0836954c14a4997e3c |
| --- /dev/null |
| +++ b/chrome/browser/extensions/active_tab_permission_manager.cc |
| @@ -0,0 +1,128 @@ |
| +// 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" |
| +#include "chrome/browser/extensions/extension_tab_helper.h" |
| +#include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.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_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> |
| +void SendToProfile(Profile* profile, const E& message) { |
|
Aaron Boodman
2012/06/06 01:03:13
Nit: this would make more sense me if it were name
not at google - send to devlin
2012/06/06 07:38:40
Done.
|
| + 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)); |
|
Aaron Boodman
2012/06/06 01:03:13
Is it possible to pass a heap-allocated message to
not at google - send to devlin
2012/06/06 07:38:40
Everybody always does "Send(new BlahMessage)" so I
|
| + } |
| +} |
| + |
| +} |
| + |
| +ActiveTabPermissionManager::ActiveTabPermissionManager( |
| + TabContentsWrapper* tab_contents) |
| + : WebContentsObserver(tab_contents->web_contents()), |
| + tab_contents_(tab_contents) { |
| + InsertActiveURL(web_contents()->GetURL()); |
| +} |
| + |
| +ActiveTabPermissionManager::~ActiveTabPermissionManager() {} |
| + |
| +void ActiveTabPermissionManager::Grant(const Extension* extension) { |
| + if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab)) |
| + return; |
| + |
| + int tab_id = tab_contents_->extension_tab_helper()->GetTabId(); |
| + |
| + // 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()) |
| + return; |
| + |
| + granted_.Insert(extension); |
|
Aaron Boodman
2012/06/06 01:03:13
What happens if the extension is unloaded?
not at google - send to devlin
2012/06/06 07:38:40
The extension stays around cos it's refcounted, an
|
| + extension->SetTabSpecificHostPermissions(tab_id, active_urls_); |
| + SendToProfile( |
| + tab_contents_->profile(), |
| + ExtensionMsg_SetTabSpecificPermissions(GetPageID() + 1, // mauahaha |
|
Aaron Boodman
2012/06/06 01:03:13
also, i believe the correct spelling is 'muah'. It
Aaron Boodman
2012/06/06 01:03:13
I don't follow... why +1?
not at google - send to devlin
2012/06/06 07:38:40
Oh, I was just testing that the page ID blocking l
|
| + 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::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()) |
| + return; |
| + |
| + int tab_id = tab_contents_->extension_tab_helper()->GetTabId(); |
| + 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(); |
| + |
| + SendToProfile( |
| + tab_contents_->profile(), |
| + ExtensionMsg_ClearTabSpecificPermissions(tab_id, extension_ids)); |
| +} |
| + |
| +void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) { |
| + URLPattern pattern(URLPattern::SCHEME_FILE | |
|
Aaron Boodman
2012/06/06 01:03:13
UserScript::kValidUserScriptSchemes? (ftp is a bit
not at google - send to devlin
2012/06/06 07:38:40
Didn't know about that constant. Done.
|
| + URLPattern::SCHEME_HTTP | |
| + URLPattern::SCHEME_HTTPS); |
| + if (pattern.Parse(url.spec()) == URLPattern::PARSE_SUCCESS) |
| + active_urls_.AddPattern(pattern); |
| +} |
| + |
| +int32 ActiveTabPermissionManager::GetPageID() { |
| + return tab_contents_->web_contents()->GetController().GetActiveEntry()-> |
| + GetPageID(); |
| +} |
| + |
| +} // namespace extensions |