[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / renderer / renderer_main_platform_delegate_mac.mm
blob03957a9019e825f890b3158523f354c9abe6962d
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/renderer_main_platform_delegate.h"
7 #include <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h>
9 #include <objc/runtime.h>
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #import "base/mac/foundation_util.h"
14 #import "base/mac/crash_logging.h"
15 #include "base/mac/scoped_cftyperef.h"
16 #include "base/sys_string_conversions.h"
17 #include "content/common/sandbox_mac.h"
18 #include "content/public/common/content_switches.h"
19 #import "content/public/common/injection_test_mac.h"
20 #include "content/common/sandbox_init_mac.h"
22 namespace content {
24 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
25     const MainFunctionParams& parameters)
26         : parameters_(parameters) {
29 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
32 // TODO(mac-port): Any code needed to initialize a process for purposes of
33 // running a renderer needs to also be reflected in chrome_main.cc for
34 // --single-process support.
35 void RendererMainPlatformDelegate::PlatformInitialize() {
36   // Initialize NSApplication up front.  Without this call, drawing of
37   // native UI elements (e.g. buttons) in WebKit will explode.
38   [NSApplication sharedApplication];
40   if (![NSThread isMultiThreaded]) {
41     NSString* string = @"";
42     [NSThread detachNewThreadSelector:@selector(length)
43                              toTarget:string
44                            withObject:nil];
45   }
47   // Debugging for http://crbug.com/152566
48   base::mac::ScopedCFTypeRef<TISInputSourceRef> input_source(
49       TISCopyCurrentKeyboardInputSource());
50   base::mac::ScopedCFTypeRef<CFStringRef> description(
51       CFCopyDescription(input_source));
52   base::mac::SetCrashKeyValue(@"tis_input_source",
53                               base::mac::CFToNSCast(description));
55   input_source.reset(TISCopyCurrentKeyboardLayoutInputSource());
56   description.reset(CFCopyDescription(input_source));
57   base::mac::SetCrashKeyValue(@"tis_layout_source",
58                               base::mac::CFToNSCast(description));
61 void RendererMainPlatformDelegate::PlatformUninitialize() {
64 static void LogTestMessage(std::string message, bool is_error) {
65   if (is_error)
66     LOG(ERROR) << message;
67   else
68     LOG(INFO) << message;
71 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
72   const CommandLine& command_line = parameters_.command_line;
74   if (command_line.HasSwitch(switches::kTestSandbox)) {
75     std::string bundle_path =
76     command_line.GetSwitchValueNative(switches::kTestSandbox);
77     if (bundle_path.empty()) {
78       NOTREACHED() << "Bad bundle path";
79       return false;
80     }
81     NSBundle* tests_bundle =
82         [NSBundle bundleWithPath:base::SysUTF8ToNSString(bundle_path)];
83     if (![tests_bundle load]) {
84       NOTREACHED() << "Failed to load bundle";
85       return false;
86     }
87     sandbox_tests_bundle_ = [tests_bundle retain];
88     [objc_getClass("RendererSandboxTestsRunner") setLogFunction:LogTestMessage];
89   }
90   return true;
93 bool RendererMainPlatformDelegate::EnableSandbox() {
94   return InitializeSandbox();
97 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) {
98   Class tests_runner = objc_getClass("RendererSandboxTestsRunner");
99   if (tests_runner) {
100     if (![tests_runner runTests])
101       LOG(ERROR) << "Running renderer with failing sandbox tests!";
102     [sandbox_tests_bundle_ unload];
103     [sandbox_tests_bundle_ release];
104     sandbox_tests_bundle_ = nil;
105   }
108 }  // namespace content