1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/extension_message_filter.h"
7 #include "chrome/common/extensions/extension_messages.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/resource_dispatcher_host.h"
11 #include "extensions/browser/event_router.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/process_manager.h"
14 #include "ipc/ipc_message_macros.h"
16 using content::BrowserThread
;
17 using content::RenderProcessHost
;
19 namespace extensions
{
21 ExtensionMessageFilter::ExtensionMessageFilter(int render_process_id
,
22 content::BrowserContext
* context
)
23 : render_process_id_(render_process_id
), browser_context_(context
) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
27 ExtensionMessageFilter::~ExtensionMessageFilter() {}
29 void ExtensionMessageFilter::OverrideThreadForMessage(
30 const IPC::Message
& message
,
31 BrowserThread::ID
* thread
) {
32 switch (message
.type()) {
33 case ExtensionHostMsg_AddListener::ID
:
34 case ExtensionHostMsg_RemoveListener::ID
:
35 case ExtensionHostMsg_AddLazyListener::ID
:
36 case ExtensionHostMsg_RemoveLazyListener::ID
:
37 case ExtensionHostMsg_AddFilteredListener::ID
:
38 case ExtensionHostMsg_RemoveFilteredListener::ID
:
39 case ExtensionHostMsg_ShouldSuspendAck::ID
:
40 case ExtensionHostMsg_SuspendAck::ID
:
41 *thread
= BrowserThread::UI
;
48 bool ExtensionMessageFilter::OnMessageReceived(const IPC::Message
& message
,
49 bool* message_was_ok
) {
51 IPC_BEGIN_MESSAGE_MAP_EX(ExtensionMessageFilter
, message
, *message_was_ok
)
52 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener
,
53 OnExtensionAddListener
)
54 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveListener
,
55 OnExtensionRemoveListener
)
56 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyListener
,
57 OnExtensionAddLazyListener
)
58 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyListener
,
59 OnExtensionRemoveLazyListener
)
60 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddFilteredListener
,
61 OnExtensionAddFilteredListener
)
62 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveFilteredListener
,
63 OnExtensionRemoveFilteredListener
)
64 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ShouldSuspendAck
,
65 OnExtensionShouldSuspendAck
)
66 IPC_MESSAGE_HANDLER(ExtensionHostMsg_SuspendAck
,
67 OnExtensionSuspendAck
)
68 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GenerateUniqueID
,
69 OnExtensionGenerateUniqueID
)
70 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ResumeRequests
,
71 OnExtensionResumeRequests
);
72 IPC_MESSAGE_UNHANDLED(handled
= false)
77 void ExtensionMessageFilter::OnExtensionAddListener(
78 const std::string
& extension_id
,
79 const std::string
& event_name
) {
80 RenderProcessHost
* process
= RenderProcessHost::FromID(render_process_id_
);
83 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
86 router
->AddEventListener(event_name
, process
, extension_id
);
89 void ExtensionMessageFilter::OnExtensionRemoveListener(
90 const std::string
& extension_id
,
91 const std::string
& event_name
) {
92 RenderProcessHost
* process
= RenderProcessHost::FromID(render_process_id_
);
95 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
98 router
->RemoveEventListener(event_name
, process
, extension_id
);
101 void ExtensionMessageFilter::OnExtensionAddLazyListener(
102 const std::string
& extension_id
, const std::string
& event_name
) {
103 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
106 router
->AddLazyEventListener(event_name
, extension_id
);
109 void ExtensionMessageFilter::OnExtensionRemoveLazyListener(
110 const std::string
& extension_id
, const std::string
& event_name
) {
111 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
114 router
->RemoveLazyEventListener(event_name
, extension_id
);
117 void ExtensionMessageFilter::OnExtensionAddFilteredListener(
118 const std::string
& extension_id
,
119 const std::string
& event_name
,
120 const base::DictionaryValue
& filter
,
122 RenderProcessHost
* process
= RenderProcessHost::FromID(render_process_id_
);
125 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
128 router
->AddFilteredEventListener(
129 event_name
, process
, extension_id
, filter
, lazy
);
132 void ExtensionMessageFilter::OnExtensionRemoveFilteredListener(
133 const std::string
& extension_id
,
134 const std::string
& event_name
,
135 const base::DictionaryValue
& filter
,
137 RenderProcessHost
* process
= RenderProcessHost::FromID(render_process_id_
);
140 EventRouter
* router
= ExtensionSystem::Get(browser_context_
)->event_router();
143 router
->RemoveFilteredEventListener(
144 event_name
, process
, extension_id
, filter
, lazy
);
147 void ExtensionMessageFilter::OnExtensionShouldSuspendAck(
148 const std::string
& extension_id
, int sequence_id
) {
149 ProcessManager
* process_manager
=
150 ExtensionSystem::Get(browser_context_
)->process_manager();
152 process_manager
->OnShouldSuspendAck(extension_id
, sequence_id
);
155 void ExtensionMessageFilter::OnExtensionSuspendAck(
156 const std::string
& extension_id
) {
157 ProcessManager
* process_manager
=
158 ExtensionSystem::Get(browser_context_
)->process_manager();
160 process_manager
->OnSuspendAck(extension_id
);
163 void ExtensionMessageFilter::OnExtensionGenerateUniqueID(int* unique_id
) {
164 static int next_unique_id
= 0;
165 *unique_id
= ++next_unique_id
;
168 void ExtensionMessageFilter::OnExtensionResumeRequests(int route_id
) {
169 content::ResourceDispatcherHost::Get()->ResumeBlockedRequestsForRoute(
170 render_process_id_
, route_id
);
173 } // namespace extensions