blob: 7b79c9e5ac0e60335feca333295c30b719a0d6b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// Copyright (c) 2009 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_message_service.h"
#include "base/singleton.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extension.h"
#include "chrome/browser/extensions/extension_view.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/resource_message_filter.h"
#include "chrome/common/render_messages.h"
// This class acts as the port to an extension process. It is basically just
// gymnastics to get access to the IPC::Channel (not the ChannelProxy) belonging
// to an ExtensionView.
// Created on the UI thread, but accessed fully on the IO thread.
class ExtensionMessageService::ExtensionFilter :
public IPC::ChannelProxy::MessageFilter {
public:
ExtensionFilter(const std::string& extension_id, int routing_id) :
extension_id_(extension_id), routing_id_(routing_id), channel_(NULL) {
}
~ExtensionFilter() {
ExtensionMessageService::GetInstance()->OnExtensionUnregistered(this);
}
virtual void OnFilterAdded(IPC::Channel* channel) {
channel_ = channel;
ExtensionMessageService::GetInstance()->OnExtensionRegistered(this);
}
virtual void OnChannelClosing() {
channel_ = NULL;
}
bool Send(IPC::Message* message) {
if (!channel_) {
delete message;
return false;
}
return channel_->Send(message);
}
IPC::Channel* channel() { return channel_; }
const std::string& extension_id() { return extension_id_; }
int routing_id() { return routing_id_; }
private:
std::string extension_id_;
int routing_id_;
IPC::Channel* channel_;
};
ExtensionMessageService* ExtensionMessageService::GetInstance() {
return Singleton<ExtensionMessageService>::get();
}
ExtensionMessageService::ExtensionMessageService()
: next_channel_id_(0) {
}
void ExtensionMessageService::RegisterExtensionView(ExtensionView* view) {
view->render_view_host()->process()->channel()->AddFilter(
new ExtensionFilter(view->extension()->id(),
view->render_view_host()->routing_id()));
}
void ExtensionMessageService::OnExtensionRegistered(ExtensionFilter* filter) {
extensions_[filter->extension_id()] = filter;
}
void ExtensionMessageService::OnExtensionUnregistered(ExtensionFilter* filter) {
// TODO(mpcomplete): support multiple filters per extension_id
//DCHECK(extensions_[filter->extension_id()] == filter);
extensions_.erase(filter->extension_id());
// Close any channels that share this filter.
for (MessageChannelMap::iterator it = channels_.begin();
it != channels_.end(); ) {
MessageChannelMap::iterator current = it++;
if (current->second.extension_port == filter)
channels_.erase(current);
}
}
int ExtensionMessageService::OpenChannelToExtension(
const std::string& extension_id, ResourceMessageFilter* renderer_port) {
DCHECK(MessageLoop::current() ==
ChromeThread::GetMessageLoop(ChromeThread::IO));
ExtensionMap::iterator extension_port = extensions_.find(extension_id);
if (extension_port == extensions_.end())
return -1;
int channel_id = next_channel_id_++;
MessageChannel channel;
channel.renderer_port = renderer_port;
channel.extension_port = extension_port->second;
channels_[channel_id] = channel;
return channel_id;
}
void ExtensionMessageService::PostMessage(
int channel_id, const std::string& message) {
DCHECK(MessageLoop::current() ==
ChromeThread::GetMessageLoop(ChromeThread::IO));
MessageChannelMap::iterator iter = channels_.find(channel_id);
if (iter == channels_.end())
return;
MessageChannel& channel = iter->second;
channel.extension_port->Send(new ViewMsg_HandleExtensionMessage(
channel.extension_port->routing_id(), message, channel_id));
}
void ExtensionMessageService::RendererShutdown(
ResourceMessageFilter* renderer_port) {
DCHECK(MessageLoop::current() ==
ChromeThread::GetMessageLoop(ChromeThread::IO));
// Close any channels that share this filter.
// TODO(mpcomplete): should we notify the other side of the port?
for (MessageChannelMap::iterator it = channels_.begin();
it != channels_.end(); ) {
MessageChannelMap::iterator current = it++;
if (current->second.renderer_port == renderer_port)
channels_.erase(current);
}
}
|