• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include <windows.h>
6 
7 #include <memory>
8 
9 #include "include/cef_command_line.h"
10 #include "include/cef_sandbox_win.h"
11 #include "tests/cefclient/browser/main_context_impl.h"
12 #include "tests/cefclient/browser/main_message_loop_multithreaded_win.h"
13 #include "tests/cefclient/browser/root_window_manager.h"
14 #include "tests/cefclient/browser/test_runner.h"
15 #include "tests/shared/browser/client_app_browser.h"
16 #include "tests/shared/browser/main_message_loop_external_pump.h"
17 #include "tests/shared/browser/main_message_loop_std.h"
18 #include "tests/shared/common/client_app_other.h"
19 #include "tests/shared/common/client_switches.h"
20 #include "tests/shared/renderer/client_app_renderer.h"
21 
22 // When generating projects with CMake the CEF_USE_SANDBOX value will be defined
23 // automatically if using the required compiler version. Pass -DUSE_SANDBOX=OFF
24 // to the CMake command-line to disable use of the sandbox.
25 // Uncomment this line to manually enable sandbox support.
26 // #define CEF_USE_SANDBOX 1
27 
28 #if defined(CEF_USE_SANDBOX)
29 // The cef_sandbox.lib static library may not link successfully with all VS
30 // versions.
31 #pragma comment(lib, "cef_sandbox.lib")
32 #endif
33 
34 namespace client {
35 namespace {
36 
RunMain(HINSTANCE hInstance,int nCmdShow)37 int RunMain(HINSTANCE hInstance, int nCmdShow) {
38   // Enable High-DPI support on Windows 7 or newer.
39   CefEnableHighDPISupport();
40 
41   CefMainArgs main_args(hInstance);
42 
43   void* sandbox_info = nullptr;
44 
45 #if defined(CEF_USE_SANDBOX)
46   // Manage the life span of the sandbox information object. This is necessary
47   // for sandbox support on Windows. See cef_sandbox_win.h for complete details.
48   CefScopedSandboxInfo scoped_sandbox;
49   sandbox_info = scoped_sandbox.sandbox_info();
50 #endif
51 
52   // Parse command-line arguments.
53   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
54   command_line->InitFromString(::GetCommandLineW());
55 
56   // Create a ClientApp of the correct type.
57   CefRefPtr<CefApp> app;
58   ClientApp::ProcessType process_type = ClientApp::GetProcessType(command_line);
59   if (process_type == ClientApp::BrowserProcess)
60     app = new ClientAppBrowser();
61   else if (process_type == ClientApp::RendererProcess)
62     app = new ClientAppRenderer();
63   else if (process_type == ClientApp::OtherProcess)
64     app = new ClientAppOther();
65 
66   // Execute the secondary process, if any.
67   int exit_code = CefExecuteProcess(main_args, app, sandbox_info);
68   if (exit_code >= 0)
69     return exit_code;
70 
71   // Create the main context object.
72   auto context = std::make_unique<MainContextImpl>(command_line, true);
73 
74   CefSettings settings;
75 
76 #if !defined(CEF_USE_SANDBOX)
77   settings.no_sandbox = true;
78 #endif
79 
80   // Applications should specify a unique GUID here to enable trusted downloads.
81   CefString(&settings.application_client_id_for_file_scanning)
82       .FromString("9A8DE24D-B822-4C6C-8259-5A848FEA1E68");
83 
84   // Populate the settings based on command line arguments.
85   context->PopulateSettings(&settings);
86 
87   // Create the main message loop object.
88   std::unique_ptr<MainMessageLoop> message_loop;
89   if (settings.multi_threaded_message_loop)
90     message_loop.reset(new MainMessageLoopMultithreadedWin);
91   else if (settings.external_message_pump)
92     message_loop = MainMessageLoopExternalPump::Create();
93   else
94     message_loop.reset(new MainMessageLoopStd);
95 
96   // Initialize CEF.
97   context->Initialize(main_args, settings, app, sandbox_info);
98 
99   // Register scheme handlers.
100   test_runner::RegisterSchemeHandlers();
101 
102   auto window_config = std::make_unique<RootWindowConfig>();
103   window_config->always_on_top =
104       command_line->HasSwitch(switches::kAlwaysOnTop);
105   window_config->with_controls =
106       !command_line->HasSwitch(switches::kHideControls);
107   window_config->with_osr =
108       settings.windowless_rendering_enabled ? true : false;
109 
110   // Create the first window.
111   context->GetRootWindowManager()->CreateRootWindow(std::move(window_config));
112 
113   // Run the message loop. This will block until Quit() is called by the
114   // RootWindowManager after all windows have been destroyed.
115   int result = message_loop->Run();
116 
117   // Shut down CEF.
118   context->Shutdown();
119 
120   // Release objects in reverse order of creation.
121   message_loop.reset();
122   context.reset();
123 
124   return result;
125 }
126 
127 }  // namespace
128 }  // namespace client
129 
130 // Program entry point function.
wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)131 int APIENTRY wWinMain(HINSTANCE hInstance,
132                       HINSTANCE hPrevInstance,
133                       LPTSTR lpCmdLine,
134                       int nCmdShow) {
135   UNREFERENCED_PARAMETER(hPrevInstance);
136   UNREFERENCED_PARAMETER(lpCmdLine);
137   return client::RunMain(hInstance, nCmdShow);
138 }
139