Skip to content

Commit

Permalink
Add tabId, type, and timeStamp parameters to extension webRequest eve…
Browse files Browse the repository at this point in the history
…nts.

Also added support for filtering based on resource type, tabId, and windowId.

BUG=60101
TEST=covered by apitests

Review URL: http://codereview.chromium.org/6685010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79066 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mpcomplete@chromium.org committed Mar 22, 2011
1 parent 2862086 commit fc4facd
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 37 deletions.
5 changes: 5 additions & 0 deletions chrome/browser/browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "chrome/browser/download/download_file_manager.h"
#include "chrome/browser/download/save_file_manager.h"
#include "chrome/browser/extensions/extension_event_router_forwarder.h"
#include "chrome/browser/extensions/extension_tab_id_map.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/google/google_url_tracker.h"
#include "chrome/browser/gpu_process_host_ui_shim.h"
Expand Down Expand Up @@ -135,6 +136,8 @@ BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
net_log_.reset(new ChromeNetLog);

extension_event_router_forwarder_ = new ExtensionEventRouterForwarder;

ExtensionTabIdMap::GetInstance()->Init();
}

BrowserProcessImpl::~BrowserProcessImpl() {
Expand Down Expand Up @@ -198,6 +201,8 @@ BrowserProcessImpl::~BrowserProcessImpl() {
resource_dispatcher_host()->Shutdown();
}

ExtensionTabIdMap::GetInstance()->Shutdown();

// The policy providers managed by |browser_policy_connector_| need to shut
// down while the IO and FILE threads are still alive.
browser_policy_connector_.reset();
Expand Down
150 changes: 150 additions & 0 deletions chrome/browser/extensions/extension_tab_id_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) 2011 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/extension_tab_id_map.h"

#include "content/browser/browser_thread.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/render_process_host.h"
#include "content/common/notification_registrar.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_service.h"

// ExtensionTabIdMap is a Singleton, so it doesn't need refcounting.
DISABLE_RUNNABLE_METHOD_REFCOUNT(ExtensionTabIdMap);

//
// ExtensionTabIdMap::TabObserver
//

// This class listens for notifications about new and closed tabs on the UI
// thread, and notifies the ExtensionTabIdMap on the IO thread. It should only
// ever be accessed on the UI thread.
class ExtensionTabIdMap::TabObserver : public NotificationObserver {
public:
TabObserver();
~TabObserver();

private:
// NotificationObserver interface.
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details);

NotificationRegistrar registrar_;
};

ExtensionTabIdMap::TabObserver::TabObserver() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::TAB_PARENTED,
NotificationService::AllSources());
}

ExtensionTabIdMap::TabObserver::~TabObserver() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

void ExtensionTabIdMap::TabObserver::Observe(
NotificationType type, const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB: {
TabContents* contents = Source<TabContents>(source).ptr();
RenderViewHost* host = Details<RenderViewHost>(details).ptr();
// TODO(mpcmoplete): How can we tell if window_id is bogus? It may not
// have been set yet.
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
ExtensionTabIdMap::GetInstance(),
&ExtensionTabIdMap::SetTabAndWindowId,
host->process()->id(), host->routing_id(),
contents->controller().session_id().id(),
contents->controller().window_id().id()));
break;
}
case NotificationType::TAB_PARENTED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
RenderViewHost* host = controller->tab_contents()->render_view_host();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
ExtensionTabIdMap::GetInstance(),
&ExtensionTabIdMap::SetTabAndWindowId,
host->process()->id(), host->routing_id(),
controller->session_id().id(),
controller->window_id().id()));
break;
}
case NotificationType::RENDER_VIEW_HOST_DELETED: {
RenderViewHost* host = Details<RenderViewHost>(details).ptr();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
ExtensionTabIdMap::GetInstance(),
&ExtensionTabIdMap::ClearTabAndWindowId,
host->process()->id(), host->routing_id()));
break;
}
default:
NOTREACHED();
return;
}
}

//
// ExtensionTabIdMap
//

ExtensionTabIdMap::ExtensionTabIdMap() : observer_(NULL) {
}

ExtensionTabIdMap::~ExtensionTabIdMap() {
}

// static
ExtensionTabIdMap* ExtensionTabIdMap::GetInstance() {
return Singleton<ExtensionTabIdMap>::get();
}

void ExtensionTabIdMap::Init() {
observer_ = new TabObserver;
}

void ExtensionTabIdMap::Shutdown() {
delete observer_;
}

void ExtensionTabIdMap::SetTabAndWindowId(
int render_process_host_id, int routing_id, int tab_id, int window_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
RenderId render_id(render_process_host_id, routing_id);
map_[render_id] = TabAndWindowId(tab_id, window_id);
}

void ExtensionTabIdMap::ClearTabAndWindowId(
int render_process_host_id, int routing_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
RenderId render_id(render_process_host_id, routing_id);
map_.erase(render_id);
}

bool ExtensionTabIdMap::GetTabAndWindowId(
int render_process_host_id, int routing_id, int* tab_id, int* window_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
RenderId render_id(render_process_host_id, routing_id);
TabAndWindowIdMap::iterator iter = map_.find(render_id);
if (iter != map_.end()) {
*tab_id = iter->second.first;
*window_id = iter->second.second;
return true;
}
return false;
}
56 changes: 56 additions & 0 deletions chrome/browser/extensions/extension_tab_id_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2011 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.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_ID_MAP_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_ID_MAP_H_
#pragma once

#include <map>
#include <utility>

#include "base/basictypes.h"
#include "base/singleton.h"

// This class keeps track of a map between renderer IDs and tab/window IDs, for
// use on the IO thread. All methods should be called on the IO thread except
// for Init and Shutdown.
class ExtensionTabIdMap {
public:
static ExtensionTabIdMap* GetInstance();

// These are called on the UI thread to start and stop listening to tab
// notifications.
void Init();
void Shutdown();

// Looks up the tab and window ID for a given render view. Returns true
// if we have the IDs in our map. Called on the IO thread.
bool GetTabAndWindowId(
int render_process_host_id, int routing_id, int* tab_id, int* window_id);

private:
class TabObserver;
friend class TabObserver;
friend struct DefaultSingletonTraits<ExtensionTabIdMap>;

typedef std::pair<int, int> RenderId;
typedef std::pair<int, int> TabAndWindowId;
typedef std::map<RenderId, TabAndWindowId> TabAndWindowIdMap;

ExtensionTabIdMap();
~ExtensionTabIdMap();

// Adds or removes a render view from our map.
void SetTabAndWindowId(
int render_process_host_id, int routing_id, int tab_id, int window_id);
void ClearTabAndWindowId(
int render_process_host_id, int routing_id);

TabObserver* observer_;
TabAndWindowIdMap map_;

DISALLOW_COPY_AND_ASSIGN(ExtensionTabIdMap);
};

#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_ID_MAP_H_
Loading

0 comments on commit fc4facd

Please sign in to comment.