| 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..c4de2530c7454934c5a50c8d3e8b1d701f94b81f
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/active_tab_permission_manager.cc
|
| @@ -0,0 +1,137 @@
|
| +// 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_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>
|
| +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(
|
| + TabContentsWrapper* tab_contents)
|
| + : WebContentsObserver(tab_contents->web_contents()),
|
| + tab_contents_(tab_contents) {
|
| + InsertActiveURL(web_contents()->GetURL());
|
| +}
|
| +
|
| +ActiveTabPermissionManager::~ActiveTabPermissionManager() {}
|
| +
|
| +void ActiveTabPermissionManager::MaybeGrant(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);
|
| + 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;
|
| + 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())
|
| + 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();
|
| +
|
| + 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)
|
| + active_urls_.AddPattern(pattern);
|
| +}
|
| +
|
| +int32 ActiveTabPermissionManager::GetPageID() {
|
| + return tab_contents_->web_contents()->GetController().GetActiveEntry()->
|
| + GetPageID();
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|