From 0f0192261299f7c03053bbd1f56fa5c95d4ca013 Mon Sep 17 00:00:00 2001
From: "jeremy@chromium.org"
 <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Wed, 4 Feb 2009 17:59:06 +0000
Subject: Make chrome/renderer/render_main.cc cross-platform + OS X bringup.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9141 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/chrome.xcodeproj/project.pbxproj            | 24 +++++--
 chrome/common/resource_bundle.h                    |  3 +
 chrome/common/sandbox_init_wrapper.cc              | 15 +----
 chrome/renderer/renderer.vcproj                    |  8 +++
 chrome/renderer/renderer_main.cc                   | 53 ++++-----------
 chrome/renderer/renderer_main_platform_delegate.h  | 39 +++++++++++
 .../renderer_main_platform_delegate_mac.cc         | 44 ++++++++++++
 .../renderer_main_platform_delegate_win.cc         | 78 ++++++++++++++++++++++
 8 files changed, 204 insertions(+), 60 deletions(-)
 create mode 100755 chrome/renderer/renderer_main_platform_delegate.h
 create mode 100644 chrome/renderer/renderer_main_platform_delegate_mac.cc
 create mode 100755 chrome/renderer/renderer_main_platform_delegate_win.cc

(limited to 'chrome')

diff --git a/chrome/chrome.xcodeproj/project.pbxproj b/chrome/chrome.xcodeproj/project.pbxproj
index 079868c9..56566e8 100644
--- a/chrome/chrome.xcodeproj/project.pbxproj
+++ b/chrome/chrome.xcodeproj/project.pbxproj
@@ -247,6 +247,8 @@
 		B503E1030F017C1000547DC6 /* librenderer.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D640CEB0EAE86BD00EBCFC0 /* librenderer.a */; };
 		B507AC1F0F0048E10060FEE8 /* ipc_sync_message.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBBA0E9D4C9F009A6919 /* ipc_sync_message.cc */; };
 		B507AC440F004B610060FEE8 /* ipc_sync_message_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBBC0E9D4C9F009A6919 /* ipc_sync_message_unittest.cc */; };
+		B51F6D150F37C4DC00152D66 /* renderer_main_platform_delegate_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = B51F6D130F37C4DC00152D66 /* renderer_main_platform_delegate_mac.cc */; };
+		B51F6D2E0F37D04200152D66 /* renderer_main.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D640CD90EAE868600EBCFC0 /* renderer_main.cc */; };
 		B52E29BE0F0AA333008AD1C8 /* l10n_util_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBCB0E9D4C9F009A6919 /* l10n_util_unittest.cc */; };
 		B54BD8FC0ED622C00093FD54 /* mach_message_source_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = B54BD8FA0ED622C00093FD54 /* mach_message_source_mac.cc */; };
 		B555B2230F21506300F751B9 /* metrics_log.cc in Sources */ = {isa = PBXBuildFile; fileRef = B555B2200F21504D00F751B9 /* metrics_log.cc */; };
@@ -1908,6 +1910,9 @@
 		A7CBAD370F322A7E00360BF5 /* shell_dialogs_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = shell_dialogs_mac.mm; path = cocoa/shell_dialogs_mac.mm; sourceTree = "<group>"; };
 		A9C335E39D39A7DE087850FC /* url_pattern_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = url_pattern_unittest.cc; path = common/extensions/url_pattern_unittest.cc; sourceTree = SOURCE_ROOT; };
 		B503E0FB0F01764800547DC6 /* user_script_slave_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = user_script_slave_unittest.cc; sourceTree = "<group>"; };
+		B51F6D110F37C4DC00152D66 /* renderer_main_platform_delegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = renderer_main_platform_delegate.h; sourceTree = "<group>"; };
+		B51F6D120F37C4DC00152D66 /* renderer_main_platform_delegate_win.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = renderer_main_platform_delegate_win.cc; sourceTree = "<group>"; };
+		B51F6D130F37C4DC00152D66 /* renderer_main_platform_delegate_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = renderer_main_platform_delegate_mac.cc; sourceTree = "<group>"; };
 		B54BD8FA0ED622C00093FD54 /* mach_message_source_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mach_message_source_mac.cc; sourceTree = "<group>"; };
 		B54BD8FB0ED622C00093FD54 /* mach_message_source_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mach_message_source_mac.h; sourceTree = "<group>"; };
 		B555B2160F21504D00F751B9 /* metrics_service.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = metrics_service.h; path = metrics/metrics_service.h; sourceTree = "<group>"; };
@@ -2385,6 +2390,9 @@
 				4D640CD70EAE868600EBCFC0 /* render_widget_unittest.cc */,
 				4D640CD80EAE868600EBCFC0 /* renderer_glue.cc */,
 				4D640CD90EAE868600EBCFC0 /* renderer_main.cc */,
+				B51F6D110F37C4DC00152D66 /* renderer_main_platform_delegate.h */,
+				B51F6D120F37C4DC00152D66 /* renderer_main_platform_delegate_win.cc */,
+				B51F6D130F37C4DC00152D66 /* renderer_main_platform_delegate_mac.cc */,
 				4D640CDA0EAE868600EBCFC0 /* renderer_resources.h */,
 				4D640CC90EAE868600EBCFC0 /* user_script_slave.cc */,
 				4D640CCA0EAE868600EBCFC0 /* user_script_slave.h */,
@@ -4500,19 +4508,21 @@
 			buildActionMask = 2147483647;
 			files = (
 				4D640CF30EAE86E300EBCFC0 /* about_handler.cc in Sources */,
+				3380A69B0F2E91AE004EF74F /* child_process.cc in Sources */,
+				3380A69F0F2E91E5004EF74F /* chrome_plugin_lib.cc in Sources */,
 				E45076D90F1538E4003BE099 /* dom_automation_controller.cc in Sources */,
 				E45076C90F1537F5003BE099 /* dom_ui_bindings.cc in Sources */,
 				E45076CB0F15380C003BE099 /* external_host_bindings.cc in Sources */,
-				B503E0F00F0175FD00547DC6 /* user_script_slave.cc in Sources */,
+				3380A6A30F2E9207004EF74F /* ipc_sync_channel.cc in Sources */,
 				E45076CF0F153871003BE099 /* localized_error.cc in Sources */,
 				4D640CF40EAE86EA00EBCFC0 /* render_dns_queue.cc in Sources */,
-				4D640CF50EAE86EF00EBCFC0 /* visitedlink_slave.cc in Sources */,
-				3380A69B0F2E91AE004EF74F /* child_process.cc in Sources */,
-				3380A69D0F2E91D4004EF74F /* render_thread.cc in Sources */,
-				3380A69F0F2E91E5004EF74F /* chrome_plugin_lib.cc in Sources */,
-				3380A6A10F2E91F9004EF74F /* render_process.cc in Sources */,
-				3380A6A30F2E9207004EF74F /* ipc_sync_channel.cc in Sources */,
+				B503E0F00F0175FD00547DC6 /* user_script_slave.cc in Sources */,
 				3380A9D70F2FC8F9004EF74F /* render_dns_master.cc in Sources */,
+				B51F6D2E0F37D04200152D66 /* renderer_main.cc in Sources */,
+				B51F6D150F37C4DC00152D66 /* renderer_main_platform_delegate_mac.cc in Sources */,
+				3380A6A10F2E91F9004EF74F /* render_process.cc in Sources */,
+				3380A69D0F2E91D4004EF74F /* render_thread.cc in Sources */,
+				4D640CF50EAE86EF00EBCFC0 /* visitedlink_slave.cc in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
diff --git a/chrome/common/resource_bundle.h b/chrome/common/resource_bundle.h
index 7af3ac8..0143ec2 100644
--- a/chrome/common/resource_bundle.h
+++ b/chrome/common/resource_bundle.h
@@ -119,6 +119,9 @@ class ResourceBundle {
 #elif defined(OS_LINUX)
   // Linux will use base::DataPack.  TODO(evanm): finish this.
   typedef base::DataPack* DataHandle;
+#elif defined(OS_MACOSX)
+  // TODO(port): Implement resource loading on OS X.
+  typedef void* DataHandle;
 #endif
 
   // Ctor/dtor are private, since we're a singleton.
diff --git a/chrome/common/sandbox_init_wrapper.cc b/chrome/common/sandbox_init_wrapper.cc
index f72c2df..e7228aa 100644
--- a/chrome/common/sandbox_init_wrapper.cc
+++ b/chrome/common/sandbox_init_wrapper.cc
@@ -7,12 +7,6 @@
 #include "base/command_line.h"
 #include "chrome/common/chrome_switches.h"
 
-#if defined(OS_MACOSX)
-extern "C" {
-#include <sandbox.h>
-}
-#endif
-
 #if defined(OS_WIN)
 
 void SandboxInitWrapper::SetServices(sandbox::SandboxInterfaceInfo* info) {
@@ -37,13 +31,8 @@ void SandboxInitWrapper::InitializeSandbox(const CommandLine& command_line,
 #if defined(OS_WIN)
       target_services_->Init();
 #elif defined(OS_MACOSX)
-      // TODO(pinkerton): note, this leaks |error_buff|. What do we want to
-      // do with the error? Pass it back to main?
-      char* error_buff;
-      int error = sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
-                               &error_buff);
-      if (error)
-        exit(-1);
+      // Nothing to do here for OS X, see renderer_main_platform_delegate_mac.cc
+      // For Sandbox initialization.
 #endif
     }
   }
diff --git a/chrome/renderer/renderer.vcproj b/chrome/renderer/renderer.vcproj
index 06d85bd..289fc28 100644
--- a/chrome/renderer/renderer.vcproj
+++ b/chrome/renderer/renderer.vcproj
@@ -302,6 +302,14 @@
 			>
 		</File>
 		<File
+			RelativePath=".\renderer_main_platform_delegate.h"
+			>
+		</File>
+		<File
+			RelativePath=".\renderer_main_platform_delegate_win.cc"
+			>
+		</File>
+		<File
 			RelativePath=".\renderer_resources.h"
 			>
 		</File>
diff --git a/chrome/renderer/renderer_main.cc b/chrome/renderer/renderer_main.cc
index 9635d7a..7d28ecf 100644
--- a/chrome/renderer/renderer_main.cc
+++ b/chrome/renderer/renderer_main.cc
@@ -15,10 +15,11 @@
 #include "chrome/common/logging_chrome.h"
 #include "chrome/common/main_function_params.h"
 #include "chrome/common/resource_bundle.h"
+#if defined(OS_WIN)
 #include "chrome/common/win_util.h"
+#endif
+#include "chrome/renderer/renderer_main_platform_delegate.h"
 #include "chrome/renderer/render_process.h"
-#include "chrome/test/injection_test_dll.h"
-#include "sandbox/src/sandbox.h"
 
 #include "chromium_strings.h"
 #include "generated_resources.h"
@@ -37,19 +38,22 @@ static void HandleRendererErrorTestParameters(const CommandLine& command_line) {
     *bad_pointer = 0;
   }
 
+#if defined(OS_WIN)
   if (command_line.HasSwitch(switches::kRendererStartupDialog)) {
     std::wstring title = l10n_util::GetString(IDS_PRODUCT_NAME);
     title += L" renderer";  // makes attaching to process easier
     MessageBox(NULL, L"renderer starting...", title.c_str(),
                MB_OK | MB_SETFOREGROUND);
   }
+#else
+  NOTIMPLEMENTED();
+#endif  // !OS_WIN
 }
 
 // mainline routine for running as the Rendererer process
 int RendererMain(const MainFunctionParams& parameters) {
   const CommandLine& parsed_command_line = parameters.command_line_;
-  sandbox::TargetServices* target_services = 
-      parameters.sandbox_info_.TargetServices();
+  RendererMainPlatformDelegate platform(parameters);
 
   StatsScope<StatsCounterTimer>
       startup_timer(chrome::Counters::renderer_main());
@@ -62,22 +66,10 @@ int RendererMain(const MainFunctionParams& parameters) {
   // Initialize the SystemMonitor
   base::SystemMonitor::Start();
 
-  CoInitialize(NULL);
+  platform.PlatformInitialize();
 
-  DLOG(INFO) << "Started renderer with " <<
-    parsed_command_line.command_line_string();
-
-  HMODULE sandbox_test_module = NULL;
   bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
-  if (target_services && !no_sandbox) {
-    // The command line might specify a test dll to load.
-    if (parsed_command_line.HasSwitch(switches::kTestSandbox)) {
-      std::wstring test_dll_name =
-        parsed_command_line.GetSwitchValue(switches::kTestSandbox);
-      sandbox_test_module = LoadLibrary(test_dll_name.c_str());
-      DCHECK(sandbox_test_module);
-    }
-  }
+  platform.InitSandboxTests(no_sandbox);
 
   HandleRendererErrorTestParameters(parsed_command_line);
 
@@ -86,28 +78,10 @@ int RendererMain(const MainFunctionParams& parameters) {
   if (RenderProcess::GlobalInit(channel_name)) {
     bool run_loop = true;
     if (!no_sandbox) {
-      if (target_services) {
-        target_services->LowerToken();
-      } else {
-        run_loop = false;
-      }
+      run_loop = platform.EnableSandbox();
     }
 
-    if (sandbox_test_module) {
-      RunRendererTests run_security_tests =
-          reinterpret_cast<RunRendererTests>(GetProcAddress(sandbox_test_module,
-                                                            kRenderTestCall));
-      DCHECK(run_security_tests);
-      if (run_security_tests) {
-        int test_count = 0;
-        DLOG(INFO) << "Running renderer security tests";
-        BOOL result = run_security_tests(&test_count);
-        DCHECK(result) << "Test number " << test_count << " has failed.";
-        // If we are in release mode, debug or crash the process.
-        if (!result)
-          __debugbreak();
-      }
-    }
+    platform.RunSandboxTests();
 
     startup_timer.Stop();  // End of Startup Time Measurement.
 
@@ -119,8 +93,7 @@ int RendererMain(const MainFunctionParams& parameters) {
 
     RenderProcess::GlobalCleanup();
   }
-
-  CoUninitialize();
+  platform.PlatformUninitialize();
   return 0;
 }
 
diff --git a/chrome/renderer/renderer_main_platform_delegate.h b/chrome/renderer/renderer_main_platform_delegate.h
new file mode 100755
index 0000000..8f1e453
--- /dev/null
+++ b/chrome/renderer/renderer_main_platform_delegate.h
@@ -0,0 +1,39 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_RENDERER_RENDERER_PLATFORM_DELEGATE_H_
+#define CHROME_RENDERER_RENDERER_PLATFORM_DELEGATE_H_
+
+#include "chrome/common/main_function_params.h"
+
+class RendererMainPlatformDelegate {
+ public:
+  RendererMainPlatformDelegate(const MainFunctionParams& parameters);
+  ~RendererMainPlatformDelegate();
+
+  // Called first thing and last thing in the process' lifecycle, i.e. before
+  // the sandbox is enabled.
+  void PlatformInitialize();
+  void PlatformUninitialize();
+
+  // Gives us an opportunity to initialize state used for tests before enabling
+  // the sandbox.
+  bool InitSandboxTests(bool no_sandbox);
+
+  // Initiate Lockdown, returns true on success.
+  bool EnableSandbox();
+
+  // Runs Sandbox tests.
+  void RunSandboxTests();
+
+ private:
+  const MainFunctionParams& parameters_;
+#if defined(OS_WIN)
+  HMODULE sandbox_test_module_;
+#endif
+
+  DISALLOW_COPY_AND_ASSIGN(RendererMainPlatformDelegate);
+};
+
+#endif  // CHROME_RENDERER_RENDERER_PLATFORM_DELEGATE_H_
diff --git a/chrome/renderer/renderer_main_platform_delegate_mac.cc b/chrome/renderer/renderer_main_platform_delegate_mac.cc
new file mode 100644
index 0000000..022cb02
--- /dev/null
+++ b/chrome/renderer/renderer_main_platform_delegate_mac.cc
@@ -0,0 +1,44 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/renderer/renderer_main_platform_delegate.h"
+
+extern "C" {
+#include <sandbox.h>
+}
+
+RendererMainPlatformDelegate::RendererMainPlatformDelegate(
+    const MainFunctionParams& parameters)
+        : parameters_(parameters) {
+}
+
+RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
+}
+
+void RendererMainPlatformDelegate::PlatformInitialize() {
+}
+
+void RendererMainPlatformDelegate::PlatformUninitialize() {
+}
+
+bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
+  return true;
+}
+
+bool RendererMainPlatformDelegate::EnableSandbox() {
+  char* error_buff = NULL;
+  int error = sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
+                           &error_buff);
+  bool success = (error == 0 && error_buff == NULL);
+  if (error == -1) {
+    LOG(ERROR) << "Failed to Initialize Sandbox: " << error_buff;
+  }
+  sandbox_free_error(error_buff);
+  return success;
+}
+
+void RendererMainPlatformDelegate::RunSandboxTests() {
+  // Should run sandbox unit tests.
+  NOTIMPLEMENTED();
+}
diff --git a/chrome/renderer/renderer_main_platform_delegate_win.cc b/chrome/renderer/renderer_main_platform_delegate_win.cc
new file mode 100755
index 0000000..f4280a7
--- /dev/null
+++ b/chrome/renderer/renderer_main_platform_delegate_win.cc
@@ -0,0 +1,78 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/renderer/renderer_main_platform_delegate.h"
+
+#include <windows.h>
+
+#include "base/command_line.h"
+#include "chrome/common/chrome_constants.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/test/injection_test_dll.h"
+#include "sandbox/src/sandbox.h"
+
+RendererMainPlatformDelegate::RendererMainPlatformDelegate(
+    const MainFunctionParams& parameters)
+        : parameters_(parameters),
+          sandbox_test_module_(NULL) {
+}
+
+RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
+}
+
+void RendererMainPlatformDelegate::PlatformInitialize() {
+  CoInitialize(NULL);
+}
+
+void RendererMainPlatformDelegate::PlatformUninitialize() {
+  CoUninitialize();
+}
+
+bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
+  const CommandLine& command_line = parameters_.command_line_;
+
+  DLOG(INFO) << "Started renderer with " << command_line.command_line_string();
+
+  sandbox::TargetServices* target_services =
+      parameters_.sandbox_info_.TargetServices();
+
+  if (target_services && !no_sandbox) {
+      std::wstring test_dll_name =
+          command_line.GetSwitchValue(switches::kTestSandbox);
+    if (!test_dll_name.empty()) {
+      sandbox_test_module_ = LoadLibrary(test_dll_name.c_str());
+      DCHECK(sandbox_test_module_);
+      if (!sandbox_test_module_) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
+
+bool RendererMainPlatformDelegate::EnableSandbox() {
+  sandbox::TargetServices* target_services =
+      parameters_.sandbox_info_.TargetServices();
+
+  if (target_services) {
+    target_services->LowerToken();
+    return true;
+  }
+  return false;
+}
+
+void RendererMainPlatformDelegate::RunSandboxTests() {
+  if (sandbox_test_module_) {
+    RunRendererTests run_security_tests =
+        reinterpret_cast<RunRendererTests>(GetProcAddress(sandbox_test_module_,
+                                                          kRenderTestCall));
+    DCHECK(run_security_tests);
+    if (run_security_tests) {
+      int test_count = 0;
+      DLOG(INFO) << "Running renderer security tests";
+      BOOL result = run_security_tests(&test_count);
+      CHECK(result) << "Test number " << test_count << " has failed.";
+    }
+  }
+}
-- 
cgit v1.1