Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / ppapi / nacl_irt / ppapi_dispatcher.cc
blob343c71c2655f9c9ab01fe6d4bca941ab42d5541b
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 "ppapi/nacl_irt/ppapi_dispatcher.h"
7 #include <map>
8 #include <set>
10 #include "build/build_config.h"
11 // Need to include this before most other files because it defines
12 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define
13 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the
14 // ViewMsgLog et al. functions.
16 #include "base/command_line.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "components/tracing/child_trace_message_filter.h"
21 #include "ipc/ipc_channel_handle.h"
22 #include "ipc/ipc_logging.h"
23 #include "ipc/ipc_message.h"
24 #include "ppapi/c/ppp.h"
25 #include "ppapi/c/ppp_instance.h"
26 #include "ppapi/nacl_irt/manifest_service.h"
27 #include "ppapi/nacl_irt/plugin_startup.h"
28 #include "ppapi/proxy/plugin_dispatcher.h"
29 #include "ppapi/proxy/plugin_globals.h"
30 #include "ppapi/proxy/plugin_message_filter.h"
31 #include "ppapi/proxy/plugin_proxy_delegate.h"
32 #include "ppapi/proxy/resource_reply_thread_registrar.h"
34 #if defined(IPC_MESSAGE_LOG_ENABLED)
35 #include "base/containers/hash_tables.h"
37 LogFunctionMap g_log_function_mapping;
39 #define IPC_MESSAGE_MACROS_LOG_ENABLED
40 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
41 g_log_function_mapping[msg_id] = logger
43 #endif
44 #include "ppapi/proxy/ppapi_messages.h"
46 namespace ppapi {
48 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop,
49 base::WaitableEvent* shutdown_event,
50 int browser_ipc_fd,
51 int renderer_ipc_fd)
52 : next_plugin_dispatcher_id_(0),
53 message_loop_(io_loop),
54 shutdown_event_(shutdown_event),
55 renderer_ipc_fd_(renderer_ipc_fd) {
56 #if defined(IPC_MESSAGE_LOG_ENABLED)
57 IPC::Logging::set_log_function_map(&g_log_function_mapping);
58 #endif
60 IPC::ChannelHandle channel_handle(
61 "NaCl IPC", base::FileDescriptor(browser_ipc_fd, false));
63 proxy::PluginGlobals* globals = proxy::PluginGlobals::Get();
64 // Delay initializing the SyncChannel until after we add filters. This
65 // ensures that the filters won't miss any messages received by
66 // the channel.
67 channel_ =
68 IPC::SyncChannel::Create(this, GetIPCMessageLoop(), GetShutdownEvent());
69 scoped_refptr<ppapi::proxy::PluginMessageFilter> plugin_filter(
70 new ppapi::proxy::PluginMessageFilter(
71 NULL, globals->resource_reply_thread_registrar()));
72 channel_->AddFilter(plugin_filter.get());
73 globals->RegisterResourceMessageFilters(plugin_filter.get());
75 channel_->AddFilter(
76 new tracing::ChildTraceMessageFilter(message_loop_.get()));
77 channel_->Init(channel_handle, IPC::Channel::MODE_SERVER, true);
80 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() {
81 return message_loop_.get();
84 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() {
85 return shutdown_event_;
88 IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote(
89 base::PlatformFile handle,
90 base::ProcessId peer_pid,
91 bool should_close_source) {
92 return IPC::InvalidPlatformFileForTransit();
95 std::set<PP_Instance>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
96 return &instances_;
99 uint32 PpapiDispatcher::Register(proxy::PluginDispatcher* plugin_dispatcher) {
100 if (!plugin_dispatcher ||
101 plugin_dispatchers_.size() >= std::numeric_limits<uint32>::max()) {
102 return 0;
105 uint32 id = 0;
106 do {
107 // Although it is unlikely, make sure that we won't cause any trouble
108 // when the counter overflows.
109 id = next_plugin_dispatcher_id_++;
110 } while (id == 0 ||
111 plugin_dispatchers_.find(id) != plugin_dispatchers_.end());
112 plugin_dispatchers_[id] = plugin_dispatcher;
113 return id;
116 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) {
117 plugin_dispatchers_.erase(plugin_dispatcher_id);
120 IPC::Sender* PpapiDispatcher::GetBrowserSender() {
121 return this;
124 std::string PpapiDispatcher::GetUILanguage() {
125 NOTIMPLEMENTED();
126 return std::string();
129 void PpapiDispatcher::PreCacheFont(const void* logfontw) {
130 NOTIMPLEMENTED();
133 void PpapiDispatcher::SetActiveURL(const std::string& url) {
134 NOTIMPLEMENTED();
137 PP_Resource PpapiDispatcher::CreateBrowserFont(
138 proxy::Connection connection,
139 PP_Instance instance,
140 const PP_BrowserFont_Trusted_Description& desc,
141 const Preferences& prefs) {
142 NOTIMPLEMENTED();
143 return 0;
146 bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) {
147 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg)
148 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher,
149 OnMsgInitializeNaClDispatcher)
150 // All other messages are simply forwarded to a PluginDispatcher.
151 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg))
152 IPC_END_MESSAGE_MAP()
153 return true;
156 void PpapiDispatcher::OnChannelError() {
157 exit(1);
160 bool PpapiDispatcher::Send(IPC::Message* msg) {
161 return channel_->Send(msg);
164 void PpapiDispatcher::OnMsgInitializeNaClDispatcher(
165 const PpapiNaClPluginArgs& args) {
166 static bool command_line_and_logging_initialized = false;
167 if (command_line_and_logging_initialized) {
168 LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin.";
169 return;
172 command_line_and_logging_initialized = true;
173 base::CommandLine::Init(0, NULL);
174 for (size_t i = 0; i < args.switch_names.size(); ++i) {
175 DCHECK(i < args.switch_values.size());
176 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
177 args.switch_names[i], args.switch_values[i]);
179 logging::LoggingSettings settings;
180 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
181 logging::InitLogging(settings);
183 proxy::PluginGlobals::Get()->set_keepalive_throttle_interval_milliseconds(
184 args.keepalive_throttle_interval_milliseconds);
186 // Tell the process-global GetInterface which interfaces it can return to the
187 // plugin.
188 proxy::InterfaceList::SetProcessGlobalPermissions(args.permissions);
190 int32_t error = ::PPP_InitializeModule(
191 0 /* module */,
192 &proxy::PluginDispatcher::GetBrowserInterface);
193 if (error)
194 ::exit(error);
196 proxy::PluginDispatcher* dispatcher =
197 new proxy::PluginDispatcher(::PPP_GetInterface, args.permissions,
198 args.off_the_record);
199 IPC::ChannelHandle channel_handle(
200 "nacl",
201 base::FileDescriptor(renderer_ipc_fd_, false));
202 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId,
203 channel_handle, false)) {
204 delete dispatcher;
205 return;
207 // From here, the dispatcher will manage its own lifetime according to the
208 // lifetime of the attached channel.
210 // Notify the renderer process, if necessary.
211 ManifestService* manifest_service = GetManifestService();
212 if (manifest_service)
213 manifest_service->StartupInitializationComplete();
216 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
217 const IPC::Message& msg) {
218 // The first parameter should be a plugin dispatcher ID.
219 PickleIterator iter(msg);
220 uint32 id = 0;
221 if (!iter.ReadUInt32(&id)) {
222 NOTREACHED();
223 return;
225 std::map<uint32, proxy::PluginDispatcher*>::iterator dispatcher =
226 plugin_dispatchers_.find(id);
227 if (dispatcher != plugin_dispatchers_.end())
228 dispatcher->second->OnMessageReceived(msg);
231 } // namespace ppapi