[telemetry] Allow users to authenticate to Cloud Storage using prodaccess.
[chromium-blink-merge.git] / content / renderer / render_process_impl.cc
blob50a4b6e46ea3c25a31a466454062baa06b2921a0
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 #if defined(OS_ANDROID)
40 #include "base/android/sys_utils.h"
41 #endif
43 namespace content {
45 RenderProcessImpl::RenderProcessImpl()
46 : shared_mem_cache_cleaner_(
47 FROM_HERE, base::TimeDelta::FromSeconds(5),
48 this, &RenderProcessImpl::ClearTransportDIBCache),
49 transport_dib_next_sequence_number_(0),
50 enabled_bindings_(0) {
51 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
52 shared_mem_cache_[i] = NULL;
54 #if defined(OS_WIN)
55 // HACK: See http://b/issue?id=1024307 for rationale.
56 if (GetModuleHandle(L"LPK.DLL") == NULL) {
57 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
58 // when buffering into a EMF buffer for printing.
59 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
60 GdiInitializeLanguagePack gdi_init_lpk =
61 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
62 GetModuleHandle(L"GDI32.DLL"),
63 "GdiInitializeLanguagePack"));
64 DCHECK(gdi_init_lpk);
65 if (gdi_init_lpk) {
66 gdi_init_lpk(0);
69 #endif
71 // Out of process dev tools rely upon auto break behavior.
72 webkit_glue::SetJavaScriptFlags("--debugger-auto-break");
74 #if defined(OS_ANDROID)
75 if (base::android::SysUtils::IsLowEndDevice())
76 webkit_glue::SetJavaScriptFlags("--optimize-for-size");
77 #endif
79 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
80 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
81 webkit_glue::SetJavaScriptFlags(
82 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
85 // Turn on cross-site document blocking for renderer processes.
86 SiteIsolationPolicy::SetPolicyEnabled(
87 GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy());
90 RenderProcessImpl::~RenderProcessImpl() {
91 #ifndef NDEBUG
92 int count = blink::WebFrame::instanceCount();
93 if (count)
94 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
95 #endif
97 GetShutDownEvent()->Signal();
98 ClearTransportDIBCache();
101 void RenderProcessImpl::AddBindings(int bindings) {
102 enabled_bindings_ |= bindings;
105 int RenderProcessImpl::GetEnabledBindings() const {
106 return enabled_bindings_;
109 // -----------------------------------------------------------------------------
110 // Platform specific code for dealing with bitmap transport...
112 TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
113 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
114 // POSIX creates transport DIBs in the browser, so we need to do a sync IPC to
115 // get one. The TransportDIB is cached in the browser.
116 TransportDIB::Handle handle;
117 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
118 if (!main_thread()->Send(msg))
119 return NULL;
120 if (handle.fd < 0)
121 return NULL;
122 return TransportDIB::Map(handle);
123 #else
124 // Windows, legacy GTK and Android create transport DIBs inside the
125 // renderer.
126 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
127 #endif
130 void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
131 if (!dib)
132 return;
134 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
135 // On POSIX we need to tell the browser that it can drop a reference to the
136 // shared memory.
137 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
138 main_thread()->Send(msg);
139 #endif
141 delete dib;
144 // -----------------------------------------------------------------------------
147 skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
148 TransportDIB** memory, const gfx::Rect& rect) {
149 int width = rect.width();
150 int height = rect.height();
151 const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width());
152 #if defined(OS_LINUX) || defined(OS_OPENBSD)
153 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
154 #else
155 const size_t max_size = 0;
156 #endif
158 // If the requested size is too big, reduce the height. Ideally we might like
159 // to reduce the width as well to make the size reduction more "balanced", but
160 // it rarely comes up in practice.
161 if ((max_size != 0) && (height * stride > max_size))
162 height = max_size / stride;
164 const size_t size = height * stride;
166 if (!GetTransportDIBFromCache(memory, size)) {
167 *memory = CreateTransportDIB(size);
168 if (!*memory)
169 return NULL;
172 return (*memory)->GetPlatformCanvas(width, height);
175 void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
176 if (PutSharedMemInCache(mem)) {
177 shared_mem_cache_cleaner_.Reset();
178 return;
181 FreeTransportDIB(mem);
184 bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
185 size_t size) {
186 // look for a cached object that is suitable for the requested size.
187 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
188 if (shared_mem_cache_[i] &&
189 size <= shared_mem_cache_[i]->size()) {
190 *mem = shared_mem_cache_[i];
191 shared_mem_cache_[i] = NULL;
192 return true;
196 return false;
199 int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
200 // simple algorithm:
201 // - look for an empty slot to store mem, or
202 // - if full, then replace smallest entry which is smaller than |size|
203 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
204 if (shared_mem_cache_[i] == NULL)
205 return i;
208 size_t smallest_size = size;
209 int smallest_index = -1;
211 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
212 const size_t entry_size = shared_mem_cache_[i]->size();
213 if (entry_size < smallest_size) {
214 smallest_size = entry_size;
215 smallest_index = i;
219 if (smallest_index != -1) {
220 FreeTransportDIB(shared_mem_cache_[smallest_index]);
221 shared_mem_cache_[smallest_index] = NULL;
224 return smallest_index;
227 bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
228 const int slot = FindFreeCacheSlot(mem->size());
229 if (slot == -1)
230 return false;
232 shared_mem_cache_[slot] = mem;
233 return true;
236 void RenderProcessImpl::ClearTransportDIBCache() {
237 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
238 if (shared_mem_cache_[i]) {
239 FreeTransportDIB(shared_mem_cache_[i]);
240 shared_mem_cache_[i] = NULL;
245 } // namespace content