Remove unused Device Motion IPC messages.
[chromium-blink-merge.git] / content / renderer / render_process_impl.cc
blob20d181b26820fac540103530006d64798a4cffaa
1 // Copyright (c) 2012 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 "content/renderer/render_process_impl.h"
7 #include "build/build_config.h"
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #include <objidl.h>
12 #include <mlang.h>
13 #endif
15 #include "base/basictypes.h"
16 #include "base/command_line.h"
17 #include "base/compiler_specific.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/sys_info.h"
21 #include "content/child/child_thread.h"
22 #include "content/child/npapi/plugin_instance.h"
23 #include "content/child/npapi/plugin_lib.h"
24 #include "content/child/site_isolation_policy.h"
25 #include "content/common/view_messages.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/renderer/content_renderer_client.h"
28 #include "ipc/ipc_channel.h"
29 #include "ipc/ipc_message_utils.h"
30 #include "skia/ext/platform_canvas.h"
31 #include "third_party/WebKit/public/web/WebFrame.h"
32 #include "ui/surface/transport_dib.h"
33 #include "webkit/glue/webkit_glue.h"
35 #if defined(OS_MACOSX)
36 #include "base/mac/mac_util.h"
37 #endif
39 namespace content {
41 RenderProcessImpl::RenderProcessImpl()
42 : shared_mem_cache_cleaner_(
43 FROM_HERE, base::TimeDelta::FromSeconds(5),
44 this, &RenderProcessImpl::ClearTransportDIBCache),
45 transport_dib_next_sequence_number_(0),
46 enabled_bindings_(0) {
47 in_process_plugins_ = InProcessPlugins();
48 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
49 shared_mem_cache_[i] = NULL;
51 #if defined(OS_WIN)
52 // HACK: See http://b/issue?id=1024307 for rationale.
53 if (GetModuleHandle(L"LPK.DLL") == NULL) {
54 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
55 // when buffering into a EMF buffer for printing.
56 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
57 GdiInitializeLanguagePack gdi_init_lpk =
58 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
59 GetModuleHandle(L"GDI32.DLL"),
60 "GdiInitializeLanguagePack"));
61 DCHECK(gdi_init_lpk);
62 if (gdi_init_lpk) {
63 gdi_init_lpk(0);
66 #endif
68 // Out of process dev tools rely upon auto break behavior.
69 webkit_glue::SetJavaScriptFlags("--debugger-auto-break");
71 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
72 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
73 webkit_glue::SetJavaScriptFlags(
74 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
77 // Turn on cross-site document blocking for renderer processes.
78 SiteIsolationPolicy::SetPolicyEnabled(
79 GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy());
82 RenderProcessImpl::~RenderProcessImpl() {
83 #ifndef NDEBUG
84 int count = WebKit::WebFrame::instanceCount();
85 if (count)
86 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
87 #endif
89 GetShutDownEvent()->Signal();
90 ClearTransportDIBCache();
93 bool RenderProcessImpl::InProcessPlugins() {
94 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
95 #if defined(OS_LINUX) || defined(OS_OPENBSD)
96 // Plugin processes require a UI message loop, and the Linux message loop
97 // implementation only allows one UI loop per process.
98 if (command_line.HasSwitch(switches::kInProcessPlugins))
99 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
100 return command_line.HasSwitch(switches::kInProcessPlugins);
101 #else
102 return command_line.HasSwitch(switches::kInProcessPlugins) ||
103 command_line.HasSwitch(switches::kSingleProcess);
104 #endif
107 void RenderProcessImpl::AddBindings(int bindings) {
108 enabled_bindings_ |= bindings;
111 int RenderProcessImpl::GetEnabledBindings() const {
112 return enabled_bindings_;
115 // -----------------------------------------------------------------------------
116 // Platform specific code for dealing with bitmap transport...
118 TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
119 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
120 // POSIX creates transport DIBs in the browser, so we need to do a sync IPC to
121 // get one. The TransportDIB is cached in the browser.
122 TransportDIB::Handle handle;
123 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
124 if (!main_thread()->Send(msg))
125 return NULL;
126 if (handle.fd < 0)
127 return NULL;
128 return TransportDIB::Map(handle);
129 #else
130 // Windows, legacy GTK and Android create transport DIBs inside the
131 // renderer.
132 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
133 #endif
136 void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
137 if (!dib)
138 return;
140 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
141 // On POSIX we need to tell the browser that it can drop a reference to the
142 // shared memory.
143 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
144 main_thread()->Send(msg);
145 #endif
147 delete dib;
150 // -----------------------------------------------------------------------------
153 skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
154 TransportDIB** memory, const gfx::Rect& rect) {
155 int width = rect.width();
156 int height = rect.height();
157 const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width());
158 #if defined(OS_LINUX) || defined(OS_OPENBSD)
159 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
160 #else
161 const size_t max_size = 0;
162 #endif
164 // If the requested size is too big, reduce the height. Ideally we might like
165 // to reduce the width as well to make the size reduction more "balanced", but
166 // it rarely comes up in practice.
167 if ((max_size != 0) && (height * stride > max_size))
168 height = max_size / stride;
170 const size_t size = height * stride;
172 if (!GetTransportDIBFromCache(memory, size)) {
173 *memory = CreateTransportDIB(size);
174 if (!*memory)
175 return NULL;
178 return (*memory)->GetPlatformCanvas(width, height);
181 void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
182 if (PutSharedMemInCache(mem)) {
183 shared_mem_cache_cleaner_.Reset();
184 return;
187 FreeTransportDIB(mem);
190 bool RenderProcessImpl::UseInProcessPlugins() const {
191 return in_process_plugins_;
194 bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
195 size_t size) {
196 // look for a cached object that is suitable for the requested size.
197 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
198 if (shared_mem_cache_[i] &&
199 size <= shared_mem_cache_[i]->size()) {
200 *mem = shared_mem_cache_[i];
201 shared_mem_cache_[i] = NULL;
202 return true;
206 return false;
209 int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
210 // simple algorithm:
211 // - look for an empty slot to store mem, or
212 // - if full, then replace smallest entry which is smaller than |size|
213 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
214 if (shared_mem_cache_[i] == NULL)
215 return i;
218 size_t smallest_size = size;
219 int smallest_index = -1;
221 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
222 const size_t entry_size = shared_mem_cache_[i]->size();
223 if (entry_size < smallest_size) {
224 smallest_size = entry_size;
225 smallest_index = i;
229 if (smallest_index != -1) {
230 FreeTransportDIB(shared_mem_cache_[smallest_index]);
231 shared_mem_cache_[smallest_index] = NULL;
234 return smallest_index;
237 bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
238 const int slot = FindFreeCacheSlot(mem->size());
239 if (slot == -1)
240 return false;
242 shared_mem_cache_[slot] = mem;
243 return true;
246 void RenderProcessImpl::ClearTransportDIBCache() {
247 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
248 if (shared_mem_cache_[i]) {
249 FreeTransportDIB(shared_mem_cache_[i]);
250 shared_mem_cache_[i] = NULL;
255 } // namespace content