| OLD | NEW |
| 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" |
| 14 #import "base/mac/mac_util.h" |
| 13 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 16 #include "content/common/sandbox_mac.h" | 18 #include "content/common/sandbox_mac.h" |
| 17 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 18 #import "content/public/common/injection_test_mac.h" | 20 #import "content/public/common/injection_test_mac.h" |
| 19 #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" |
| 20 | 31 |
| 21 namespace content { | 32 namespace content { |
| 22 | 33 |
| 23 namespace { | 34 namespace { |
| 24 | 35 |
| 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 |
| 25 // You are about to read a pretty disgusting hack. In a static initializer, | 72 // You are about to read a pretty disgusting hack. In a static initializer, |
| 26 // CoreFoundation decides to connect with cfprefsd(8) using Mach IPC. There is | 73 // CoreFoundation decides to connect with cfprefsd(8) using Mach IPC. There is |
| 27 // no public way to close this Mach port after-the-fact, nor a way to stop it | 74 // no public way to close this Mach port after-the-fact, nor a way to stop it |
| 28 // from happening since it is done pre-main in dyld. But the address of the | 75 // from happening since it is done pre-main in dyld. But the address of the |
| 29 // CFMachPort can be found in the run loop's string description. Below, that | 76 // CFMachPort can be found in the run loop's string description. Below, that |
| 30 // address is parsed, cast, and then used to invalidate the Mach port to | 77 // address is parsed, cast, and then used to invalidate the Mach port to |
| 31 // disable communication with cfprefsd. | 78 // disable communication with cfprefsd. |
| 32 void DisconnectCFNotificationCenter() { | 79 void DisconnectCFNotificationCenter() { |
| 33 base::ScopedCFTypeRef<CFStringRef> run_loop_description( | 80 base::ScopedCFTypeRef<CFStringRef> run_loop_description( |
| 34 CFCopyDescription(CFRunLoopGetCurrent())); | 81 CFCopyDescription(CFRunLoopGetCurrent())); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 : parameters_(parameters) { | 157 : parameters_(parameters) { |
| 111 } | 158 } |
| 112 | 159 |
| 113 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() { | 160 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() { |
| 114 } | 161 } |
| 115 | 162 |
| 116 // TODO(mac-port): Any code needed to initialize a process for purposes of | 163 // TODO(mac-port): Any code needed to initialize a process for purposes of |
| 117 // running a renderer needs to also be reflected in chrome_main.cc for | 164 // running a renderer needs to also be reflected in chrome_main.cc for |
| 118 // --single-process support. | 165 // --single-process support. |
| 119 void RendererMainPlatformDelegate::PlatformInitialize() { | 166 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 |
| 120 if (![NSThread isMultiThreaded]) { | 171 if (![NSThread isMultiThreaded]) { |
| 121 NSString* string = @""; | 172 NSString* string = @""; |
| 122 [NSThread detachNewThreadSelector:@selector(length) | 173 [NSThread detachNewThreadSelector:@selector(length) |
| 123 toTarget:string | 174 toTarget:string |
| 124 withObject:nil]; | 175 withObject:nil]; |
| 125 } | 176 } |
| 126 } | 177 } |
| 127 | 178 |
| 128 void RendererMainPlatformDelegate::PlatformUninitialize() { | 179 void RendererMainPlatformDelegate::PlatformUninitialize() { |
| 129 } | 180 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 151 NOTREACHED() << "Failed to load bundle"; | 202 NOTREACHED() << "Failed to load bundle"; |
| 152 return false; | 203 return false; |
| 153 } | 204 } |
| 154 sandbox_tests_bundle_ = [tests_bundle retain]; | 205 sandbox_tests_bundle_ = [tests_bundle retain]; |
| 155 [objc_getClass("RendererSandboxTestsRunner") setLogFunction:LogTestMessage]; | 206 [objc_getClass("RendererSandboxTestsRunner") setLogFunction:LogTestMessage]; |
| 156 } | 207 } |
| 157 return true; | 208 return true; |
| 158 } | 209 } |
| 159 | 210 |
| 160 bool RendererMainPlatformDelegate::EnableSandbox() { | 211 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 |
| 161 // Enable the sandbox. | 241 // Enable the sandbox. |
| 162 bool sandbox_initialized = InitializeSandbox(); | 242 bool sandbox_initialized = InitializeSandbox(); |
| 163 | 243 |
| 164 // The sandbox is now engaged. Make sure that the renderer has not connected | 244 if (needs_ime_hack) { |
| 165 // itself to Cocoa. | 245 // After the sandbox is initialized, call into TIS. Doing this before |
| 166 CHECK(NSApp == nil); | 246 // the sandbox is in place will open up renderer access to the |
| 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 } |
| 167 | 257 |
| 168 DisconnectCFNotificationCenter(); | 258 DisconnectCFNotificationCenter(); |
| 169 | 259 |
| 170 return sandbox_initialized; | 260 return sandbox_initialized; |
| 171 } | 261 } |
| 172 | 262 |
| 173 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) { | 263 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) { |
| 174 Class tests_runner = objc_getClass("RendererSandboxTestsRunner"); | 264 Class tests_runner = objc_getClass("RendererSandboxTestsRunner"); |
| 175 if (tests_runner) { | 265 if (tests_runner) { |
| 176 if (![tests_runner runTests]) | 266 if (![tests_runner runTests]) |
| 177 LOG(ERROR) << "Running renderer with failing sandbox tests!"; | 267 LOG(ERROR) << "Running renderer with failing sandbox tests!"; |
| 178 [sandbox_tests_bundle_ unload]; | 268 [sandbox_tests_bundle_ unload]; |
| 179 [sandbox_tests_bundle_ release]; | 269 [sandbox_tests_bundle_ release]; |
| 180 sandbox_tests_bundle_ = nil; | 270 sandbox_tests_bundle_ = nil; |
| 181 } | 271 } |
| 182 } | 272 } |
| 183 | 273 |
| 184 } // namespace content | 274 } // namespace content |
| OLD | NEW |