• 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 "tests/cefclient/browser/main_context_impl.h"
6 
7 #include <algorithm>
8 
9 #include "include/cef_parser.h"
10 #include "tests/shared/browser/client_app_browser.h"
11 #include "tests/shared/common/client_switches.h"
12 
13 namespace client {
14 
15 namespace {
16 
17 // The default URL to load in a browser window.
18 const char kDefaultUrl[] = "http://www.google.com";
19 
20 // Returns the ARGB value for |color|.
ParseColor(const std::string & color)21 cef_color_t ParseColor(const std::string& color) {
22   std::string colorToLower;
23   colorToLower.resize(color.size());
24   std::transform(color.begin(), color.end(), colorToLower.begin(), ::tolower);
25 
26   if (colorToLower == "black")
27     return CefColorSetARGB(255, 0, 0, 0);
28   else if (colorToLower == "blue")
29     return CefColorSetARGB(255, 0, 0, 255);
30   else if (colorToLower == "green")
31     return CefColorSetARGB(255, 0, 255, 0);
32   else if (colorToLower == "red")
33     return CefColorSetARGB(255, 255, 0, 0);
34   else if (colorToLower == "white")
35     return CefColorSetARGB(255, 255, 255, 255);
36 
37   // Use the default color.
38   return 0;
39 }
40 
41 }  // namespace
42 
MainContextImpl(CefRefPtr<CefCommandLine> command_line,bool terminate_when_all_windows_closed)43 MainContextImpl::MainContextImpl(CefRefPtr<CefCommandLine> command_line,
44                                  bool terminate_when_all_windows_closed)
45     : command_line_(command_line),
46       terminate_when_all_windows_closed_(terminate_when_all_windows_closed),
47       initialized_(false),
48       shutdown_(false),
49       background_color_(0),
50       browser_background_color_(0),
51       windowless_frame_rate_(0),
52       use_views_(false) {
53   DCHECK(command_line_.get());
54 
55   // Set the main URL.
56   if (command_line_->HasSwitch(switches::kUrl))
57     main_url_ = command_line_->GetSwitchValue(switches::kUrl);
58   if (main_url_.empty())
59     main_url_ = kDefaultUrl;
60 
61   // Whether windowless (off-screen) rendering will be used.
62   use_windowless_rendering_ =
63       command_line_->HasSwitch(switches::kOffScreenRenderingEnabled);
64 
65   if (use_windowless_rendering_ &&
66       command_line_->HasSwitch(switches::kOffScreenFrameRate)) {
67     windowless_frame_rate_ =
68         atoi(command_line_->GetSwitchValue(switches::kOffScreenFrameRate)
69                  .ToString()
70                  .c_str());
71   }
72 
73   // Whether transparent painting is used with windowless rendering.
74   const bool use_transparent_painting =
75       use_windowless_rendering_ &&
76       command_line_->HasSwitch(switches::kTransparentPaintingEnabled);
77 
78 #if defined(OS_WIN)
79   // Shared texture is only supported on Windows.
80   shared_texture_enabled_ =
81       use_windowless_rendering_ &&
82       command_line_->HasSwitch(switches::kSharedTextureEnabled);
83 #endif
84 
85   external_begin_frame_enabled_ =
86       use_windowless_rendering_ &&
87       command_line_->HasSwitch(switches::kExternalBeginFrameEnabled);
88 
89   if (windowless_frame_rate_ <= 0) {
90 // Choose a reasonable default rate based on the OSR mode.
91 #if defined(OS_WIN)
92     windowless_frame_rate_ = shared_texture_enabled_ ? 60 : 30;
93 #else
94     windowless_frame_rate_ = 30;
95 #endif
96   }
97 
98   // Enable experimental Chrome runtime. See issue #2969 for details.
99   use_chrome_runtime_ =
100       command_line_->HasSwitch(switches::kEnableChromeRuntime);
101 
102   if (use_windowless_rendering_ && use_chrome_runtime_) {
103     LOG(ERROR)
104         << "Windowless rendering is not supported with the Chrome runtime.";
105     use_chrome_runtime_ = false;
106   }
107 
108   // Whether the Views framework will be used.
109   use_views_ = command_line_->HasSwitch(switches::kUseViews);
110 
111   if (use_windowless_rendering_ && use_views_) {
112     LOG(ERROR)
113         << "Windowless rendering is not supported by the Views framework.";
114     use_views_ = false;
115   }
116 
117   if (use_chrome_runtime_ && !use_views_) {
118     // TODO(chrome): Add support for this runtime configuration (e.g. a fully
119     // styled Chrome window with cefclient menu customizations). In the mean
120     // time this can be demo'd with "cefsimple --enable-chrome-runtime".
121     LOG(WARNING) << "Chrome runtime requires the Views framework.";
122     use_views_ = true;
123   }
124 
125   if (use_views_ && command_line->HasSwitch(switches::kHideFrame) &&
126       !command_line_->HasSwitch(switches::kUrl)) {
127     // Use the draggable regions test as the default URL for frameless windows.
128     main_url_ = "http://tests/draggable";
129   }
130 
131   if (command_line_->HasSwitch(switches::kBackgroundColor)) {
132     // Parse the background color value.
133     background_color_ =
134         ParseColor(command_line_->GetSwitchValue(switches::kBackgroundColor));
135   }
136 
137   if (background_color_ == 0 && !use_views_) {
138     // Set an explicit background color.
139     background_color_ = CefColorSetARGB(255, 255, 255, 255);
140   }
141 
142   // |browser_background_color_| should remain 0 to enable transparent painting.
143   if (!use_transparent_painting) {
144     browser_background_color_ = background_color_;
145   }
146 }
147 
~MainContextImpl()148 MainContextImpl::~MainContextImpl() {
149   // The context must either not have been initialized, or it must have also
150   // been shut down.
151   DCHECK(!initialized_ || shutdown_);
152 }
153 
GetConsoleLogPath()154 std::string MainContextImpl::GetConsoleLogPath() {
155   return GetAppWorkingDirectory() + "console.log";
156 }
157 
GetMainURL()158 std::string MainContextImpl::GetMainURL() {
159   return main_url_;
160 }
161 
GetBackgroundColor()162 cef_color_t MainContextImpl::GetBackgroundColor() {
163   return background_color_;
164 }
165 
UseChromeRuntime()166 bool MainContextImpl::UseChromeRuntime() {
167   return use_chrome_runtime_;
168 }
169 
UseViews()170 bool MainContextImpl::UseViews() {
171   return use_views_;
172 }
173 
UseWindowlessRendering()174 bool MainContextImpl::UseWindowlessRendering() {
175   return use_windowless_rendering_;
176 }
177 
TouchEventsEnabled()178 bool MainContextImpl::TouchEventsEnabled() {
179   return command_line_->GetSwitchValue("touch-events") == "enabled";
180 }
181 
PopulateSettings(CefSettings * settings)182 void MainContextImpl::PopulateSettings(CefSettings* settings) {
183   client::ClientAppBrowser::PopulateSettings(command_line_, *settings);
184 
185   if (use_chrome_runtime_)
186     settings->chrome_runtime = true;
187 
188   CefString(&settings->cache_path) =
189       command_line_->GetSwitchValue(switches::kCachePath);
190 
191   if (use_windowless_rendering_)
192     settings->windowless_rendering_enabled = true;
193 
194   if (browser_background_color_ != 0)
195     settings->background_color = browser_background_color_;
196 
197   if (command_line_->HasSwitch("lang")) {
198     // Use the same locale for the Accept-Language HTTP request header.
199     CefString(&settings->accept_language_list) =
200         command_line_->GetSwitchValue("lang");
201   }
202 }
203 
PopulateBrowserSettings(CefBrowserSettings * settings)204 void MainContextImpl::PopulateBrowserSettings(CefBrowserSettings* settings) {
205   settings->windowless_frame_rate = windowless_frame_rate_;
206 
207   if (browser_background_color_ != 0)
208     settings->background_color = browser_background_color_;
209 }
210 
PopulateOsrSettings(OsrRendererSettings * settings)211 void MainContextImpl::PopulateOsrSettings(OsrRendererSettings* settings) {
212   settings->show_update_rect =
213       command_line_->HasSwitch(switches::kShowUpdateRect);
214 
215 #if defined(OS_WIN)
216   settings->shared_texture_enabled = shared_texture_enabled_;
217 #endif
218   settings->external_begin_frame_enabled = external_begin_frame_enabled_;
219   settings->begin_frame_rate = windowless_frame_rate_;
220 
221   if (browser_background_color_ != 0)
222     settings->background_color = browser_background_color_;
223 }
224 
GetRootWindowManager()225 RootWindowManager* MainContextImpl::GetRootWindowManager() {
226   DCHECK(InValidState());
227   return root_window_manager_.get();
228 }
229 
Initialize(const CefMainArgs & args,const CefSettings & settings,CefRefPtr<CefApp> application,void * windows_sandbox_info)230 bool MainContextImpl::Initialize(const CefMainArgs& args,
231                                  const CefSettings& settings,
232                                  CefRefPtr<CefApp> application,
233                                  void* windows_sandbox_info) {
234   DCHECK(thread_checker_.CalledOnValidThread());
235   DCHECK(!initialized_);
236   DCHECK(!shutdown_);
237 
238   if (!CefInitialize(args, settings, application, windows_sandbox_info))
239     return false;
240 
241   // Need to create the RootWindowManager after calling CefInitialize because
242   // TempWindowX11 uses cef_get_xdisplay().
243   root_window_manager_.reset(
244       new RootWindowManager(terminate_when_all_windows_closed_));
245 
246   initialized_ = true;
247 
248   return true;
249 }
250 
Shutdown()251 void MainContextImpl::Shutdown() {
252   DCHECK(thread_checker_.CalledOnValidThread());
253   DCHECK(initialized_);
254   DCHECK(!shutdown_);
255 
256   root_window_manager_.reset();
257 
258   CefShutdown();
259 
260   shutdown_ = true;
261 }
262 
263 }  // namespace client
264