Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: content/renderer/renderer_main_platform_delegate_mac.mm

Issue 177243021: [Mac] Remove NSApplication from the renderer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/renderer/renderer_main.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/renderer_main_platform_delegate.h" 5 #include "content/renderer/renderer_main_platform_delegate.h"
6 6
7 #include <Carbon/Carbon.h> 7 #include <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h> 8 #import <Cocoa/Cocoa.h>
9 #include <objc/runtime.h> 9 #include <objc/runtime.h>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #import "base/mac/foundation_util.h" 13 #import "base/mac/foundation_util.h"
14 #import "base/mac/mac_util.h" 14 #import "base/mac/mac_util.h"
Mark Mentovai 2014/02/26 18:48:09 Still needed? foundation_util too?
Robert Sesek 2014/02/26 18:50:56 Done.
15 #include "base/mac/scoped_cftyperef.h" 15 #include "base/mac/scoped_cftyperef.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
18 #include "content/common/sandbox_mac.h" 18 #include "content/common/sandbox_mac.h"
19 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
20 #import "content/public/common/injection_test_mac.h" 20 #import "content/public/common/injection_test_mac.h"
21 #include "content/common/sandbox_init_mac.h" 21 #include "content/common/sandbox_init_mac.h"
22 #include "third_party/mach_override/mach_override.h"
23
24 extern "C" {
25 // SPI logging functions for CF that are exported externally.
26 void CFLog(int32_t level, CFStringRef format, ...);
27 void _CFLogvEx(void* log_func, void* copy_desc_func,
28 CFDictionaryRef format_options, int32_t level,
29 CFStringRef format, va_list args);
30 } // extern "C"
31 22
32 namespace content { 23 namespace content {
33 24
34 namespace { 25 namespace {
35 26
36 // This leaked array stores the text input services input and layout sources,
37 // which is returned in CrTISCreateInputSourceList(). This list is computed
38 // right after the sandbox is initialized.
39 CFArrayRef g_text_input_services_source_list_ = NULL;
40
41 CFArrayRef CrTISCreateInputSourceList(
42 CFDictionaryRef properties,
43 Boolean includeAllInstalled) {
44 DCHECK(g_text_input_services_source_list_);
45 // Callers assume ownership of the result, so increase the retain count.
46 CFRetain(g_text_input_services_source_list_);
47 return g_text_input_services_source_list_;
48 }
49
50 // Text Input Services expects to be able to XPC to HIServices, but the
51 // renderer sandbox blocks that. TIS then becomes very vocal about this on
52 // every new renderer startup, so filter out those log messages.
53 void CrRendererCFLog(int32_t level, CFStringRef format, ...) {
54 const CFStringRef kAnnoyingLogMessages[] = {
55 CFSTR("Error received in message reply handler: %s\n"),
56 CFSTR("Connection Invalid error for service %s.\n"),
57 };
58
59 for (size_t i = 0; i < arraysize(kAnnoyingLogMessages); ++i) {
60 if (CFStringCompare(format, kAnnoyingLogMessages[i], 0) ==
61 kCFCompareEqualTo) {
62 return;
63 }
64 }
65
66 va_list args;
67 va_start(args, format);
68 _CFLogvEx(NULL, NULL, NULL, level, format, args);
69 va_end(args);
70 }
71
72 // You are about to read a pretty disgusting hack. In a static initializer, 27 // You are about to read a pretty disgusting hack. In a static initializer,
73 // CoreFoundation decides to connect with cfprefsd(8) using Mach IPC. There is 28 // CoreFoundation decides to connect with cfprefsd(8) using Mach IPC. There is
74 // no public way to close this Mach port after-the-fact, nor a way to stop it 29 // no public way to close this Mach port after-the-fact, nor a way to stop it
75 // from happening since it is done pre-main in dyld. But the address of the 30 // from happening since it is done pre-main in dyld. But the address of the
76 // CFMachPort can be found in the run loop's string description. Below, that 31 // CFMachPort can be found in the run loop's string description. Below, that
77 // address is parsed, cast, and then used to invalidate the Mach port to 32 // address is parsed, cast, and then used to invalidate the Mach port to
78 // disable communication with cfprefsd. 33 // disable communication with cfprefsd.
79 void DisconnectCFNotificationCenter() { 34 void DisconnectCFNotificationCenter() {
80 base::ScopedCFTypeRef<CFStringRef> run_loop_description( 35 base::ScopedCFTypeRef<CFStringRef> run_loop_description(
81 CFCopyDescription(CFRunLoopGetCurrent())); 36 CFCopyDescription(CFRunLoopGetCurrent()));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 : parameters_(parameters) { 112 : parameters_(parameters) {
158 } 113 }
159 114
160 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() { 115 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
161 } 116 }
162 117
163 // TODO(mac-port): Any code needed to initialize a process for purposes of 118 // TODO(mac-port): Any code needed to initialize a process for purposes of
164 // running a renderer needs to also be reflected in chrome_main.cc for 119 // running a renderer needs to also be reflected in chrome_main.cc for
165 // --single-process support. 120 // --single-process support.
166 void RendererMainPlatformDelegate::PlatformInitialize() { 121 void RendererMainPlatformDelegate::PlatformInitialize() {
167 // Initialize NSApplication up front. Without this call, drawing of
168 // native UI elements (e.g. buttons) in WebKit will explode.
169 [NSApplication sharedApplication];
170
171 if (![NSThread isMultiThreaded]) { 122 if (![NSThread isMultiThreaded]) {
172 NSString* string = @""; 123 NSString* string = @"";
173 [NSThread detachNewThreadSelector:@selector(length) 124 [NSThread detachNewThreadSelector:@selector(length)
174 toTarget:string 125 toTarget:string
175 withObject:nil]; 126 withObject:nil];
176 } 127 }
177 } 128 }
178 129
179 void RendererMainPlatformDelegate::PlatformUninitialize() { 130 void RendererMainPlatformDelegate::PlatformUninitialize() {
180 } 131 }
(...skipping 21 matching lines...) Expand all
202 NOTREACHED() << "Failed to load bundle"; 153 NOTREACHED() << "Failed to load bundle";
203 return false; 154 return false;
204 } 155 }
205 sandbox_tests_bundle_ = [tests_bundle retain]; 156 sandbox_tests_bundle_ = [tests_bundle retain];
206 [objc_getClass("RendererSandboxTestsRunner") setLogFunction:LogTestMessage]; 157 [objc_getClass("RendererSandboxTestsRunner") setLogFunction:LogTestMessage];
207 } 158 }
208 return true; 159 return true;
209 } 160 }
210 161
211 bool RendererMainPlatformDelegate::EnableSandbox() { 162 bool RendererMainPlatformDelegate::EnableSandbox() {
212 // rdar://9251340 http://openradar.me/9251340
213 // See http://crbug.com/31225 and http://crbug.com/152566
214 // To check if this is broken:
215 // 1. Enable Multi language input (simplified chinese)
216 // 2. Ensure "Show/Hide Trackpad Handwriting" shortcut works.
217 // (ctrl+shift+space).
218 // 3. Now open a new tab in Google Chrome or start Google Chrome
219 // 4. Try ctrl+shift+space shortcut again. Shortcut will not work, IME will
220 // either not appear or (worse) not disappear on ctrl-shift-space.
221 // (Run `ps aux | grep Chinese` (10.6/10.7) or `ps aux | grep Trackpad`
222 // and then kill that pid to make it go away.)
223 //
224 // Chinese Handwriting was introduced in 10.6 and is confirmed broken on
225 // 10.6, 10.7, and 10.8. It's fixed on 10.9.
226 bool needs_ime_hack = base::mac::IsOSMountainLionOrEarlier();
227
228 if (needs_ime_hack) {
229 mach_error_t err = mach_override_ptr(
230 (void*)&TISCreateInputSourceList,
231 (void*)&CrTISCreateInputSourceList,
232 NULL);
233 CHECK_EQ(err_none, err);
234
235 // Override the private CFLog function so that the console is not spammed
236 // by TIS failing to connect to HIServices over XPC.
237 err = mach_override_ptr((void*)&CFLog, (void*)&CrRendererCFLog, NULL);
238 CHECK_EQ(err_none, err);
239 }
240
241 // Enable the sandbox. 163 // Enable the sandbox.
242 bool sandbox_initialized = InitializeSandbox(); 164 bool sandbox_initialized = InitializeSandbox();
243 165
244 if (needs_ime_hack) { 166 // The sandbox is now engaged. Make sure that the renderer has not connected
245 // After the sandbox is initialized, call into TIS. Doing this before 167 // itself to Cocoa.
246 // the sandbox is in place will open up renderer access to the 168 CHECK(NSApp == nil);
247 // pasteboard and an XPC connection to "com.apple.hiservices-xpcservice".
248 base::ScopedCFTypeRef<TISInputSourceRef> layout_source(
249 TISCopyCurrentKeyboardLayoutInputSource());
250 base::ScopedCFTypeRef<TISInputSourceRef> input_source(
251 TISCopyCurrentKeyboardInputSource());
252
253 CFTypeRef source_list[] = { layout_source.get(), input_source.get() };
254 g_text_input_services_source_list_ = CFArrayCreate(kCFAllocatorDefault,
255 source_list, arraysize(source_list), &kCFTypeArrayCallBacks);
256 }
257 169
258 DisconnectCFNotificationCenter(); 170 DisconnectCFNotificationCenter();
259 171
260 return sandbox_initialized; 172 return sandbox_initialized;
261 } 173 }
262 174
263 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) { 175 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) {
264 Class tests_runner = objc_getClass("RendererSandboxTestsRunner"); 176 Class tests_runner = objc_getClass("RendererSandboxTestsRunner");
265 if (tests_runner) { 177 if (tests_runner) {
266 if (![tests_runner runTests]) 178 if (![tests_runner runTests])
267 LOG(ERROR) << "Running renderer with failing sandbox tests!"; 179 LOG(ERROR) << "Running renderer with failing sandbox tests!";
268 [sandbox_tests_bundle_ unload]; 180 [sandbox_tests_bundle_ unload];
269 [sandbox_tests_bundle_ release]; 181 [sandbox_tests_bundle_ release];
270 sandbox_tests_bundle_ = nil; 182 sandbox_tests_bundle_ = nil;
271 } 183 }
272 } 184 }
273 185
274 } // namespace content 186 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/renderer_main.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698