Fix import error in mac_platform_backend.py
[chromium-blink-merge.git] / extensions / browser / extension_message_filter.cc
blob3a1c0d23ea3a58d850f5c070669760cf4f6a138e
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;
42 break;
43 default:
44 break;
48 bool ExtensionMessageFilter::OnMessageReceived(const IPC::Message& message,
49 bool* message_was_ok) {
50 bool handled = true;
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)
73 IPC_END_MESSAGE_MAP()
74 return handled;
77 void ExtensionMessageFilter::OnExtensionAddListener(
78 const std::string& extension_id,
79 const std::string& event_name) {
80 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
81 if (!process)
82 return;
83 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
84 if (!router)
85 return;
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_);
93 if (!process)
94 return;
95 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
96 if (!router)
97 return;
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();
104 if (!router)
105 return;
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();
112 if (!router)
113 return;
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,
121 bool lazy) {
122 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
123 if (!process)
124 return;
125 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
126 if (!router)
127 return;
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,
136 bool lazy) {
137 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
138 if (!process)
139 return;
140 EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
141 if (!router)
142 return;
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();
151 if (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();
159 if (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