Move extension_messages.h to extensions/common.
[chromium-blink-merge.git] / extensions / browser / extension_message_filter.cc
blobbcd56219d9fc6d49107f9cd40c8b5ccaad4b2687
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 "content/public/browser/browser_thread.h"
8 #include "content/public/browser/render_process_host.h"
9 #include "content/public/browser/resource_dispatcher_host.h"
10 #include "extensions/browser/event_router.h"
11 #include "extensions/browser/extension_system.h"
12 #include "extensions/browser/process_manager.h"
13 #include "extensions/common/extension_messages.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 : BrowserMessageFilter(ExtensionMsgStart),
24 render_process_id_(render_process_id),
25 browser_context_(context) {
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
29 ExtensionMessageFilter::~ExtensionMessageFilter() {}
31 void ExtensionMessageFilter::OverrideThreadForMessage(
32 const IPC::Message& message,
33 BrowserThread::ID* thread) {
34 switch (message.type()) {
35 case ExtensionHostMsg_AddListener::ID:
36 case ExtensionHostMsg_RemoveListener::ID:
37 case ExtensionHostMsg_AddLazyListener::ID:
38 case ExtensionHostMsg_RemoveLazyListener::ID:
39 case ExtensionHostMsg_AddFilteredListener::ID:
40 case ExtensionHostMsg_RemoveFilteredListener::ID:
41 case ExtensionHostMsg_ShouldSuspendAck::ID:
42 case ExtensionHostMsg_SuspendAck::ID:
43 *thread = BrowserThread::UI;
44 break;
45 default:
46 break;
50 bool ExtensionMessageFilter::OnMessageReceived(const IPC::Message& message,
51 bool* message_was_ok) {
52 bool handled = true;
53 IPC_BEGIN_MESSAGE_MAP_EX(ExtensionMessageFilter, message, *message_was_ok)
54 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener,
55 OnExtensionAddListener)
56 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveListener,
57 OnExtensionRemoveListener)
58 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyListener,
59 OnExtensionAddLazyListener)
60 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyListener,
61 OnExtensionRemoveLazyListener)
62 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddFilteredListener,
63 OnExtensionAddFilteredListener)
64 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveFilteredListener,
65 OnExtensionRemoveFilteredListener)
66 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ShouldSuspendAck,
67 OnExtensionShouldSuspendAck)
68 IPC_MESSAGE_HANDLER(ExtensionHostMsg_SuspendAck,
69 OnExtensionSuspendAck)
70 IPC_MESSAGE_HANDLER(ExtensionHostMsg_GenerateUniqueID,
71 OnExtensionGenerateUniqueID)
72 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ResumeRequests,
73 OnExtensionResumeRequests);
74 IPC_MESSAGE_UNHANDLED(handled = false)
75 IPC_END_MESSAGE_MAP()
76 return handled;
79 void ExtensionMessageFilter::OnExtensionAddListener(
80 const std::string& extension_id,
81 const std::string& event_name) {
82 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
83 if (!process)
84 return;
85 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
86 if (!router)
87 return;
88 router->AddEventListener(event_name, process, extension_id);
91 void ExtensionMessageFilter::OnExtensionRemoveListener(
92 const std::string& extension_id,
93 const std::string& event_name) {
94 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
95 if (!process)
96 return;
97 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
98 if (!router)
99 return;
100 router->RemoveEventListener(event_name, process, extension_id);
103 void ExtensionMessageFilter::OnExtensionAddLazyListener(
104 const std::string& extension_id, const std::string& event_name) {
105 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
106 if (!router)
107 return;
108 router->AddLazyEventListener(event_name, extension_id);
111 void ExtensionMessageFilter::OnExtensionRemoveLazyListener(
112 const std::string& extension_id, const std::string& event_name) {
113 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
114 if (!router)
115 return;
116 router->RemoveLazyEventListener(event_name, extension_id);
119 void ExtensionMessageFilter::OnExtensionAddFilteredListener(
120 const std::string& extension_id,
121 const std::string& event_name,
122 const base::DictionaryValue& filter,
123 bool lazy) {
124 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
125 if (!process)
126 return;
127 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
128 if (!router)
129 return;
130 router->AddFilteredEventListener(
131 event_name, process, extension_id, filter, lazy);
134 void ExtensionMessageFilter::OnExtensionRemoveFilteredListener(
135 const std::string& extension_id,
136 const std::string& event_name,
137 const base::DictionaryValue& filter,
138 bool lazy) {
139 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
140 if (!process)
141 return;
142 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
143 if (!router)
144 return;
145 router->RemoveFilteredEventListener(
146 event_name, process, extension_id, filter, lazy);
149 void ExtensionMessageFilter::OnExtensionShouldSuspendAck(
150 const std::string& extension_id, int sequence_id) {
151 ProcessManager* process_manager =
152 ExtensionSystem::Get(browser_context_)->process_manager();
153 if (process_manager)
154 process_manager->OnShouldSuspendAck(extension_id, sequence_id);
157 void ExtensionMessageFilter::OnExtensionSuspendAck(
158 const std::string& extension_id) {
159 ProcessManager* process_manager =
160 ExtensionSystem::Get(browser_context_)->process_manager();
161 if (process_manager)
162 process_manager->OnSuspendAck(extension_id);
165 void ExtensionMessageFilter::OnExtensionGenerateUniqueID(int* unique_id) {
166 static int next_unique_id = 0;
167 *unique_id = ++next_unique_id;
170 void ExtensionMessageFilter::OnExtensionResumeRequests(int route_id) {
171 content::ResourceDispatcherHost::Get()->ResumeBlockedRequestsForRoute(
172 render_process_id_, route_id);
175 } // namespace extensions