1 // Copyright (c) 2013 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/client_handler.h"
6
7 #include <stdio.h>
8 #include <algorithm>
9 #include <iomanip>
10 #include <sstream>
11 #include <string>
12
13 #include "include/base/cef_callback.h"
14 #include "include/cef_browser.h"
15 #include "include/cef_frame.h"
16 #include "include/cef_parser.h"
17 #include "include/cef_ssl_status.h"
18 #include "include/cef_x509_certificate.h"
19 #include "include/wrapper/cef_closure_task.h"
20 #include "tests/cefclient/browser/main_context.h"
21 #include "tests/cefclient/browser/root_window_manager.h"
22 #include "tests/cefclient/browser/test_runner.h"
23 #include "tests/shared/browser/extension_util.h"
24 #include "tests/shared/browser/resource_util.h"
25 #include "tests/shared/common/client_switches.h"
26
27 namespace client {
28
29 #if defined(OS_WIN)
30 #define NEWLINE "\r\n"
31 #else
32 #define NEWLINE "\n"
33 #endif
34
35 namespace {
36
37 // Custom menu command Ids.
38 enum client_menu_ids {
39 CLIENT_ID_SHOW_DEVTOOLS = MENU_ID_USER_FIRST,
40 CLIENT_ID_CLOSE_DEVTOOLS,
41 CLIENT_ID_INSPECT_ELEMENT,
42 CLIENT_ID_SHOW_SSL_INFO,
43 CLIENT_ID_CURSOR_CHANGE_DISABLED,
44 CLIENT_ID_OFFLINE,
45 CLIENT_ID_TESTMENU_SUBMENU,
46 CLIENT_ID_TESTMENU_CHECKITEM,
47 CLIENT_ID_TESTMENU_RADIOITEM1,
48 CLIENT_ID_TESTMENU_RADIOITEM2,
49 CLIENT_ID_TESTMENU_RADIOITEM3,
50 };
51
52 // Musr match the value in client_renderer.cc.
53 const char kFocusedNodeChangedMessage[] = "ClientRenderer.FocusedNodeChanged";
54
GetTimeString(const CefTime & value)55 std::string GetTimeString(const CefTime& value) {
56 if (value.GetTimeT() == 0)
57 return "Unspecified";
58
59 static const char* kMonths[] = {
60 "January", "February", "March", "April", "May", "June",
61 "July", "August", "September", "October", "November", "December"};
62 std::string month;
63 if (value.month >= 1 && value.month <= 12)
64 month = kMonths[value.month - 1];
65 else
66 month = "Invalid";
67
68 std::stringstream ss;
69 ss << month << " " << value.day_of_month << ", " << value.year << " "
70 << std::setfill('0') << std::setw(2) << value.hour << ":"
71 << std::setfill('0') << std::setw(2) << value.minute << ":"
72 << std::setfill('0') << std::setw(2) << value.second;
73 return ss.str();
74 }
75
GetBinaryString(CefRefPtr<CefBinaryValue> value)76 std::string GetBinaryString(CefRefPtr<CefBinaryValue> value) {
77 if (!value.get())
78 return " ";
79
80 // Retrieve the value.
81 const size_t size = value->GetSize();
82 std::string src;
83 src.resize(size);
84 value->GetData(const_cast<char*>(src.data()), size, 0);
85
86 // Encode the value.
87 return CefBase64Encode(src.data(), src.size());
88 }
89
90 #define FLAG(flag) \
91 if (status & flag) { \
92 result += std::string(#flag) + "<br/>"; \
93 }
94
95 #define VALUE(val, def) \
96 if (val == def) { \
97 return std::string(#def); \
98 }
99
GetCertStatusString(cef_cert_status_t status)100 std::string GetCertStatusString(cef_cert_status_t status) {
101 std::string result;
102
103 FLAG(CERT_STATUS_COMMON_NAME_INVALID);
104 FLAG(CERT_STATUS_DATE_INVALID);
105 FLAG(CERT_STATUS_AUTHORITY_INVALID);
106 FLAG(CERT_STATUS_NO_REVOCATION_MECHANISM);
107 FLAG(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION);
108 FLAG(CERT_STATUS_REVOKED);
109 FLAG(CERT_STATUS_INVALID);
110 FLAG(CERT_STATUS_WEAK_SIGNATURE_ALGORITHM);
111 FLAG(CERT_STATUS_NON_UNIQUE_NAME);
112 FLAG(CERT_STATUS_WEAK_KEY);
113 FLAG(CERT_STATUS_PINNED_KEY_MISSING);
114 FLAG(CERT_STATUS_NAME_CONSTRAINT_VIOLATION);
115 FLAG(CERT_STATUS_VALIDITY_TOO_LONG);
116 FLAG(CERT_STATUS_IS_EV);
117 FLAG(CERT_STATUS_REV_CHECKING_ENABLED);
118 FLAG(CERT_STATUS_SHA1_SIGNATURE_PRESENT);
119 FLAG(CERT_STATUS_CT_COMPLIANCE_FAILED);
120
121 if (result.empty())
122 return " ";
123 return result;
124 }
125
GetSSLVersionString(cef_ssl_version_t version)126 std::string GetSSLVersionString(cef_ssl_version_t version) {
127 VALUE(version, SSL_CONNECTION_VERSION_UNKNOWN);
128 VALUE(version, SSL_CONNECTION_VERSION_SSL2);
129 VALUE(version, SSL_CONNECTION_VERSION_SSL3);
130 VALUE(version, SSL_CONNECTION_VERSION_TLS1);
131 VALUE(version, SSL_CONNECTION_VERSION_TLS1_1);
132 VALUE(version, SSL_CONNECTION_VERSION_TLS1_2);
133 VALUE(version, SSL_CONNECTION_VERSION_TLS1_3);
134 VALUE(version, SSL_CONNECTION_VERSION_QUIC);
135 return std::string();
136 }
137
GetContentStatusString(cef_ssl_content_status_t status)138 std::string GetContentStatusString(cef_ssl_content_status_t status) {
139 std::string result;
140
141 VALUE(status, SSL_CONTENT_NORMAL_CONTENT);
142 FLAG(SSL_CONTENT_DISPLAYED_INSECURE_CONTENT);
143 FLAG(SSL_CONTENT_RAN_INSECURE_CONTENT);
144
145 if (result.empty())
146 return " ";
147 return result;
148 }
149
150 // Load a data: URI containing the error message.
LoadErrorPage(CefRefPtr<CefFrame> frame,const std::string & failed_url,cef_errorcode_t error_code,const std::string & other_info)151 void LoadErrorPage(CefRefPtr<CefFrame> frame,
152 const std::string& failed_url,
153 cef_errorcode_t error_code,
154 const std::string& other_info) {
155 std::stringstream ss;
156 ss << "<html><head><title>Page failed to load</title></head>"
157 "<body bgcolor=\"white\">"
158 "<h3>Page failed to load.</h3>"
159 "URL: <a href=\""
160 << failed_url << "\">" << failed_url
161 << "</a><br/>Error: " << test_runner::GetErrorString(error_code) << " ("
162 << error_code << ")";
163
164 if (!other_info.empty())
165 ss << "<br/>" << other_info;
166
167 ss << "</body></html>";
168 frame->LoadURL(test_runner::GetDataURI(ss.str(), "text/html"));
169 }
170
171 // Return HTML string with information about a certificate.
GetCertificateInformation(CefRefPtr<CefX509Certificate> cert,cef_cert_status_t certstatus)172 std::string GetCertificateInformation(CefRefPtr<CefX509Certificate> cert,
173 cef_cert_status_t certstatus) {
174 CefRefPtr<CefX509CertPrincipal> subject = cert->GetSubject();
175 CefRefPtr<CefX509CertPrincipal> issuer = cert->GetIssuer();
176
177 // Build a table showing certificate information. Various types of invalid
178 // certificates can be tested using https://badssl.com/.
179 std::stringstream ss;
180 ss << "<h3>X.509 Certificate Information:</h3>"
181 "<table border=1><tr><th>Field</th><th>Value</th></tr>";
182
183 if (certstatus != CERT_STATUS_NONE) {
184 ss << "<tr><td>Status</td><td>" << GetCertStatusString(certstatus)
185 << "</td></tr>";
186 }
187
188 ss << "<tr><td>Subject</td><td>"
189 << (subject.get() ? subject->GetDisplayName().ToString() : " ")
190 << "</td></tr>"
191 "<tr><td>Issuer</td><td>"
192 << (issuer.get() ? issuer->GetDisplayName().ToString() : " ")
193 << "</td></tr>"
194 "<tr><td>Serial #*</td><td>"
195 << GetBinaryString(cert->GetSerialNumber()) << "</td></tr>"
196 << "<tr><td>Valid Start</td><td>" << GetTimeString(cert->GetValidStart())
197 << "</td></tr>"
198 "<tr><td>Valid Expiry</td><td>"
199 << GetTimeString(cert->GetValidExpiry()) << "</td></tr>";
200
201 CefX509Certificate::IssuerChainBinaryList der_chain_list;
202 CefX509Certificate::IssuerChainBinaryList pem_chain_list;
203 cert->GetDEREncodedIssuerChain(der_chain_list);
204 cert->GetPEMEncodedIssuerChain(pem_chain_list);
205 DCHECK_EQ(der_chain_list.size(), pem_chain_list.size());
206
207 der_chain_list.insert(der_chain_list.begin(), cert->GetDEREncoded());
208 pem_chain_list.insert(pem_chain_list.begin(), cert->GetPEMEncoded());
209
210 for (size_t i = 0U; i < der_chain_list.size(); ++i) {
211 ss << "<tr><td>DER Encoded*</td>"
212 "<td style=\"max-width:800px;overflow:scroll;\">"
213 << GetBinaryString(der_chain_list[i])
214 << "</td></tr>"
215 "<tr><td>PEM Encoded*</td>"
216 "<td style=\"max-width:800px;overflow:scroll;\">"
217 << GetBinaryString(pem_chain_list[i]) << "</td></tr>";
218 }
219
220 ss << "</table> * Displayed value is base64 encoded.";
221 return ss.str();
222 }
223
224 } // namespace
225
226 class ClientDownloadImageCallback : public CefDownloadImageCallback {
227 public:
ClientDownloadImageCallback(CefRefPtr<ClientHandler> client_handler)228 explicit ClientDownloadImageCallback(CefRefPtr<ClientHandler> client_handler)
229 : client_handler_(client_handler) {}
230
OnDownloadImageFinished(const CefString & image_url,int http_status_code,CefRefPtr<CefImage> image)231 void OnDownloadImageFinished(const CefString& image_url,
232 int http_status_code,
233 CefRefPtr<CefImage> image) override {
234 if (image)
235 client_handler_->NotifyFavicon(image);
236 }
237
238 private:
239 CefRefPtr<ClientHandler> client_handler_;
240
241 IMPLEMENT_REFCOUNTING(ClientDownloadImageCallback);
242 DISALLOW_COPY_AND_ASSIGN(ClientDownloadImageCallback);
243 };
244
ClientHandler(Delegate * delegate,bool is_osr,const std::string & startup_url)245 ClientHandler::ClientHandler(Delegate* delegate,
246 bool is_osr,
247 const std::string& startup_url)
248 : is_osr_(is_osr),
249 startup_url_(startup_url),
250 download_favicon_images_(false),
251 delegate_(delegate),
252 browser_count_(0),
253 console_log_file_(MainContext::Get()->GetConsoleLogPath()),
254 first_console_message_(true),
255 focus_on_editable_field_(false),
256 initial_navigation_(true) {
257 DCHECK(!console_log_file_.empty());
258
259 #if defined(OS_LINUX)
260 // Provide the GTK-based dialog implementation on Linux.
261 dialog_handler_ = new ClientDialogHandlerGtk();
262 print_handler_ = new ClientPrintHandlerGtk();
263 #endif
264
265 resource_manager_ = new CefResourceManager();
266 test_runner::SetupResourceManager(resource_manager_, &string_resource_map_);
267
268 // Read command line settings.
269 CefRefPtr<CefCommandLine> command_line =
270 CefCommandLine::GetGlobalCommandLine();
271 mouse_cursor_change_disabled_ =
272 command_line->HasSwitch(switches::kMouseCursorChangeDisabled);
273 offline_ = command_line->HasSwitch(switches::kOffline);
274 }
275
DetachDelegate()276 void ClientHandler::DetachDelegate() {
277 if (!CURRENTLY_ON_MAIN_THREAD()) {
278 // Execute this method on the main thread.
279 MAIN_POST_CLOSURE(base::BindOnce(&ClientHandler::DetachDelegate, this));
280 return;
281 }
282
283 DCHECK(delegate_);
284 delegate_ = nullptr;
285 }
286
OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message)287 bool ClientHandler::OnProcessMessageReceived(
288 CefRefPtr<CefBrowser> browser,
289 CefRefPtr<CefFrame> frame,
290 CefProcessId source_process,
291 CefRefPtr<CefProcessMessage> message) {
292 CEF_REQUIRE_UI_THREAD();
293
294 if (message_router_->OnProcessMessageReceived(browser, frame, source_process,
295 message)) {
296 return true;
297 }
298
299 // Check for messages from the client renderer.
300 std::string message_name = message->GetName();
301 if (message_name == kFocusedNodeChangedMessage) {
302 // A message is sent from ClientRenderDelegate to tell us whether the
303 // currently focused DOM node is editable. Use of |focus_on_editable_field_|
304 // is redundant with CefKeyEvent.focus_on_editable_field in OnPreKeyEvent
305 // but is useful for demonstration purposes.
306 focus_on_editable_field_ = message->GetArgumentList()->GetBool(0);
307 return true;
308 }
309
310 return false;
311 }
312
OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefContextMenuParams> params,CefRefPtr<CefMenuModel> model)313 void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
314 CefRefPtr<CefFrame> frame,
315 CefRefPtr<CefContextMenuParams> params,
316 CefRefPtr<CefMenuModel> model) {
317 CEF_REQUIRE_UI_THREAD();
318
319 if ((params->GetTypeFlags() & (CM_TYPEFLAG_PAGE | CM_TYPEFLAG_FRAME)) != 0) {
320 // Add a separator if the menu already has items.
321 if (model->GetCount() > 0)
322 model->AddSeparator();
323
324 const bool use_chrome_runtime = MainContext::Get()->UseChromeRuntime();
325 if (!use_chrome_runtime) {
326 // TODO(chrome-runtime): Add support for this.
327 // Add DevTools items to all context menus.
328 model->AddItem(CLIENT_ID_SHOW_DEVTOOLS, "&Show DevTools");
329 model->AddItem(CLIENT_ID_CLOSE_DEVTOOLS, "Close DevTools");
330 model->AddSeparator();
331 model->AddItem(CLIENT_ID_INSPECT_ELEMENT, "Inspect Element");
332 }
333
334 if (HasSSLInformation(browser)) {
335 model->AddSeparator();
336 model->AddItem(CLIENT_ID_SHOW_SSL_INFO, "Show SSL information");
337 }
338
339 if (!use_chrome_runtime) {
340 // TODO(chrome-runtime): Add support for this.
341 model->AddSeparator();
342 model->AddCheckItem(CLIENT_ID_CURSOR_CHANGE_DISABLED,
343 "Cursor change disabled");
344 if (mouse_cursor_change_disabled_)
345 model->SetChecked(CLIENT_ID_CURSOR_CHANGE_DISABLED, true);
346 }
347
348 model->AddSeparator();
349 model->AddCheckItem(CLIENT_ID_OFFLINE, "Offline mode");
350 if (offline_)
351 model->SetChecked(CLIENT_ID_OFFLINE, true);
352
353 // Test context menu features.
354 BuildTestMenu(model);
355 }
356
357 if (delegate_)
358 delegate_->OnBeforeContextMenu(model);
359 }
360
OnContextMenuCommand(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefContextMenuParams> params,int command_id,EventFlags event_flags)361 bool ClientHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
362 CefRefPtr<CefFrame> frame,
363 CefRefPtr<CefContextMenuParams> params,
364 int command_id,
365 EventFlags event_flags) {
366 CEF_REQUIRE_UI_THREAD();
367
368 switch (command_id) {
369 case CLIENT_ID_SHOW_DEVTOOLS:
370 ShowDevTools(browser, CefPoint());
371 return true;
372 case CLIENT_ID_CLOSE_DEVTOOLS:
373 CloseDevTools(browser);
374 return true;
375 case CLIENT_ID_INSPECT_ELEMENT:
376 ShowDevTools(browser, CefPoint(params->GetXCoord(), params->GetYCoord()));
377 return true;
378 case CLIENT_ID_SHOW_SSL_INFO:
379 ShowSSLInformation(browser);
380 return true;
381 case CLIENT_ID_CURSOR_CHANGE_DISABLED:
382 mouse_cursor_change_disabled_ = !mouse_cursor_change_disabled_;
383 return true;
384 case CLIENT_ID_OFFLINE:
385 offline_ = !offline_;
386 SetOfflineState(browser, offline_);
387 return true;
388 default: // Allow default handling, if any.
389 return ExecuteTestMenu(command_id);
390 }
391 }
392
OnAddressChange(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const CefString & url)393 void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser,
394 CefRefPtr<CefFrame> frame,
395 const CefString& url) {
396 CEF_REQUIRE_UI_THREAD();
397
398 // Only update the address for the main (top-level) frame.
399 if (frame->IsMain())
400 NotifyAddress(url);
401 }
402
OnTitleChange(CefRefPtr<CefBrowser> browser,const CefString & title)403 void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
404 const CefString& title) {
405 CEF_REQUIRE_UI_THREAD();
406
407 NotifyTitle(title);
408 }
409
OnFaviconURLChange(CefRefPtr<CefBrowser> browser,const std::vector<CefString> & icon_urls)410 void ClientHandler::OnFaviconURLChange(
411 CefRefPtr<CefBrowser> browser,
412 const std::vector<CefString>& icon_urls) {
413 CEF_REQUIRE_UI_THREAD();
414
415 if (!icon_urls.empty() && download_favicon_images_) {
416 browser->GetHost()->DownloadImage(icon_urls[0], true, 16, false,
417 new ClientDownloadImageCallback(this));
418 }
419 }
420
OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,bool fullscreen)421 void ClientHandler::OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
422 bool fullscreen) {
423 CEF_REQUIRE_UI_THREAD();
424
425 NotifyFullscreen(fullscreen);
426 }
427
OnConsoleMessage(CefRefPtr<CefBrowser> browser,cef_log_severity_t level,const CefString & message,const CefString & source,int line)428 bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
429 cef_log_severity_t level,
430 const CefString& message,
431 const CefString& source,
432 int line) {
433 CEF_REQUIRE_UI_THREAD();
434
435 FILE* file = fopen(console_log_file_.c_str(), "a");
436 if (file) {
437 std::stringstream ss;
438 ss << "Level: ";
439 switch (level) {
440 case LOGSEVERITY_DEBUG:
441 ss << "Debug" << NEWLINE;
442 break;
443 case LOGSEVERITY_INFO:
444 ss << "Info" << NEWLINE;
445 break;
446 case LOGSEVERITY_WARNING:
447 ss << "Warn" << NEWLINE;
448 break;
449 case LOGSEVERITY_ERROR:
450 ss << "Error" << NEWLINE;
451 break;
452 default:
453 NOTREACHED();
454 break;
455 }
456 ss << "Message: " << message.ToString() << NEWLINE
457 << "Source: " << source.ToString() << NEWLINE << "Line: " << line
458 << NEWLINE << "-----------------------" << NEWLINE;
459 fputs(ss.str().c_str(), file);
460 fclose(file);
461
462 if (first_console_message_) {
463 test_runner::Alert(
464 browser, "Console messages written to \"" + console_log_file_ + "\"");
465 first_console_message_ = false;
466 }
467 }
468
469 return false;
470 }
471
OnAutoResize(CefRefPtr<CefBrowser> browser,const CefSize & new_size)472 bool ClientHandler::OnAutoResize(CefRefPtr<CefBrowser> browser,
473 const CefSize& new_size) {
474 CEF_REQUIRE_UI_THREAD();
475
476 NotifyAutoResize(new_size);
477 return true;
478 }
479
OnCursorChange(CefRefPtr<CefBrowser> browser,CefCursorHandle cursor,cef_cursor_type_t type,const CefCursorInfo & custom_cursor_info)480 bool ClientHandler::OnCursorChange(CefRefPtr<CefBrowser> browser,
481 CefCursorHandle cursor,
482 cef_cursor_type_t type,
483 const CefCursorInfo& custom_cursor_info) {
484 CEF_REQUIRE_UI_THREAD();
485
486 // Return true to disable default handling of cursor changes.
487 return mouse_cursor_change_disabled_;
488 }
489
OnBeforeDownload(CefRefPtr<CefBrowser> browser,CefRefPtr<CefDownloadItem> download_item,const CefString & suggested_name,CefRefPtr<CefBeforeDownloadCallback> callback)490 void ClientHandler::OnBeforeDownload(
491 CefRefPtr<CefBrowser> browser,
492 CefRefPtr<CefDownloadItem> download_item,
493 const CefString& suggested_name,
494 CefRefPtr<CefBeforeDownloadCallback> callback) {
495 CEF_REQUIRE_UI_THREAD();
496
497 // Continue the download and show the "Save As" dialog.
498 callback->Continue(MainContext::Get()->GetDownloadPath(suggested_name), true);
499 }
500
OnDownloadUpdated(CefRefPtr<CefBrowser> browser,CefRefPtr<CefDownloadItem> download_item,CefRefPtr<CefDownloadItemCallback> callback)501 void ClientHandler::OnDownloadUpdated(
502 CefRefPtr<CefBrowser> browser,
503 CefRefPtr<CefDownloadItem> download_item,
504 CefRefPtr<CefDownloadItemCallback> callback) {
505 CEF_REQUIRE_UI_THREAD();
506
507 if (download_item->IsComplete()) {
508 test_runner::Alert(browser, "File \"" +
509 download_item->GetFullPath().ToString() +
510 "\" downloaded successfully.");
511 }
512 }
513
OnDragEnter(CefRefPtr<CefBrowser> browser,CefRefPtr<CefDragData> dragData,CefDragHandler::DragOperationsMask mask)514 bool ClientHandler::OnDragEnter(CefRefPtr<CefBrowser> browser,
515 CefRefPtr<CefDragData> dragData,
516 CefDragHandler::DragOperationsMask mask) {
517 CEF_REQUIRE_UI_THREAD();
518
519 // Forbid dragging of URLs and files.
520 if ((mask & DRAG_OPERATION_LINK) && !dragData->IsFragment()) {
521 test_runner::Alert(browser, "cefclient blocks dragging of URLs and files");
522 return true;
523 }
524
525 return false;
526 }
527
OnDraggableRegionsChanged(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const std::vector<CefDraggableRegion> & regions)528 void ClientHandler::OnDraggableRegionsChanged(
529 CefRefPtr<CefBrowser> browser,
530 CefRefPtr<CefFrame> frame,
531 const std::vector<CefDraggableRegion>& regions) {
532 CEF_REQUIRE_UI_THREAD();
533
534 NotifyDraggableRegions(regions);
535 }
536
OnTakeFocus(CefRefPtr<CefBrowser> browser,bool next)537 void ClientHandler::OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) {
538 CEF_REQUIRE_UI_THREAD();
539
540 NotifyTakeFocus(next);
541 }
542
OnSetFocus(CefRefPtr<CefBrowser> browser,FocusSource source)543 bool ClientHandler::OnSetFocus(CefRefPtr<CefBrowser> browser,
544 FocusSource source) {
545 CEF_REQUIRE_UI_THREAD();
546
547 if (initial_navigation_) {
548 CefRefPtr<CefCommandLine> command_line =
549 CefCommandLine::GetGlobalCommandLine();
550 if (command_line->HasSwitch(switches::kNoActivate)) {
551 // Don't give focus to the browser on creation.
552 return true;
553 }
554 }
555
556 return false;
557 }
558
OnPreKeyEvent(CefRefPtr<CefBrowser> browser,const CefKeyEvent & event,CefEventHandle os_event,bool * is_keyboard_shortcut)559 bool ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
560 const CefKeyEvent& event,
561 CefEventHandle os_event,
562 bool* is_keyboard_shortcut) {
563 CEF_REQUIRE_UI_THREAD();
564
565 /*
566 if (!event.focus_on_editable_field && event.windows_key_code == 0x20) {
567 // Special handling for the space character when an input element does not
568 // have focus. Handling the event in OnPreKeyEvent() keeps the event from
569 // being processed in the renderer. If we instead handled the event in the
570 // OnKeyEvent() method the space key would cause the window to scroll in
571 // addition to showing the alert box.
572 if (event.type == KEYEVENT_RAWKEYDOWN)
573 test_runner::Alert(browser, "You pressed the space bar!");
574 return true;
575 }
576 */
577
578 return false;
579 }
580
OnBeforePopup(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const CefString & target_url,const CefString & target_frame_name,CefLifeSpanHandler::WindowOpenDisposition target_disposition,bool user_gesture,const CefPopupFeatures & popupFeatures,CefWindowInfo & windowInfo,CefRefPtr<CefClient> & client,CefBrowserSettings & settings,CefRefPtr<CefDictionaryValue> & extra_info,bool * no_javascript_access)581 bool ClientHandler::OnBeforePopup(
582 CefRefPtr<CefBrowser> browser,
583 CefRefPtr<CefFrame> frame,
584 const CefString& target_url,
585 const CefString& target_frame_name,
586 CefLifeSpanHandler::WindowOpenDisposition target_disposition,
587 bool user_gesture,
588 const CefPopupFeatures& popupFeatures,
589 CefWindowInfo& windowInfo,
590 CefRefPtr<CefClient>& client,
591 CefBrowserSettings& settings,
592 CefRefPtr<CefDictionaryValue>& extra_info,
593 bool* no_javascript_access) {
594 CEF_REQUIRE_UI_THREAD();
595
596 // Return true to cancel the popup window.
597 return !CreatePopupWindow(browser, false, popupFeatures, windowInfo, client,
598 settings);
599 }
600
OnAfterCreated(CefRefPtr<CefBrowser> browser)601 void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
602 CEF_REQUIRE_UI_THREAD();
603
604 browser_count_++;
605
606 if (!message_router_) {
607 // Create the browser-side router for query handling.
608 CefMessageRouterConfig config;
609 message_router_ = CefMessageRouterBrowserSide::Create(config);
610
611 // Register handlers with the router.
612 test_runner::CreateMessageHandlers(message_handler_set_);
613 MessageHandlerSet::const_iterator it = message_handler_set_.begin();
614 for (; it != message_handler_set_.end(); ++it)
615 message_router_->AddHandler(*(it), false);
616 }
617
618 // Set offline mode if requested via the command-line flag.
619 if (offline_)
620 SetOfflineState(browser, true);
621
622 if (browser->GetHost()->GetExtension()) {
623 // Browsers hosting extension apps should auto-resize.
624 browser->GetHost()->SetAutoResizeEnabled(true, CefSize(20, 20),
625 CefSize(1000, 1000));
626
627 CefRefPtr<CefExtension> extension = browser->GetHost()->GetExtension();
628 if (extension_util::IsInternalExtension(extension->GetPath())) {
629 // Register the internal handler for extension resources.
630 extension_util::AddInternalExtensionToResourceManager(extension,
631 resource_manager_);
632 }
633 }
634
635 NotifyBrowserCreated(browser);
636 }
637
DoClose(CefRefPtr<CefBrowser> browser)638 bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
639 CEF_REQUIRE_UI_THREAD();
640
641 NotifyBrowserClosing(browser);
642
643 // Allow the close. For windowed browsers this will result in the OS close
644 // event being sent.
645 return false;
646 }
647
OnBeforeClose(CefRefPtr<CefBrowser> browser)648 void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
649 CEF_REQUIRE_UI_THREAD();
650
651 if (--browser_count_ == 0) {
652 // Remove and delete message router handlers.
653 MessageHandlerSet::const_iterator it = message_handler_set_.begin();
654 for (; it != message_handler_set_.end(); ++it) {
655 message_router_->RemoveHandler(*(it));
656 delete *(it);
657 }
658 message_handler_set_.clear();
659 message_router_ = nullptr;
660 }
661
662 NotifyBrowserClosed(browser);
663 }
664
OnLoadingStateChange(CefRefPtr<CefBrowser> browser,bool isLoading,bool canGoBack,bool canGoForward)665 void ClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
666 bool isLoading,
667 bool canGoBack,
668 bool canGoForward) {
669 CEF_REQUIRE_UI_THREAD();
670
671 if (!isLoading && initial_navigation_) {
672 initial_navigation_ = false;
673 }
674
675 NotifyLoadingState(isLoading, canGoBack, canGoForward);
676 }
677
OnLoadError(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,ErrorCode errorCode,const CefString & errorText,const CefString & failedUrl)678 void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
679 CefRefPtr<CefFrame> frame,
680 ErrorCode errorCode,
681 const CefString& errorText,
682 const CefString& failedUrl) {
683 CEF_REQUIRE_UI_THREAD();
684
685 // Don't display an error for downloaded files.
686 if (errorCode == ERR_ABORTED)
687 return;
688
689 // Don't display an error for external protocols that we allow the OS to
690 // handle. See OnProtocolExecution().
691 if (errorCode == ERR_UNKNOWN_URL_SCHEME) {
692 std::string urlStr = frame->GetURL();
693 if (urlStr.find("spotify:") == 0)
694 return;
695 }
696
697 // Load the error page.
698 LoadErrorPage(frame, failedUrl, errorCode, errorText);
699 }
700
OnBeforeBrowse(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,bool user_gesture,bool is_redirect)701 bool ClientHandler::OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
702 CefRefPtr<CefFrame> frame,
703 CefRefPtr<CefRequest> request,
704 bool user_gesture,
705 bool is_redirect) {
706 CEF_REQUIRE_UI_THREAD();
707
708 message_router_->OnBeforeBrowse(browser, frame);
709 return false;
710 }
711
OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const CefString & target_url,CefRequestHandler::WindowOpenDisposition target_disposition,bool user_gesture)712 bool ClientHandler::OnOpenURLFromTab(
713 CefRefPtr<CefBrowser> browser,
714 CefRefPtr<CefFrame> frame,
715 const CefString& target_url,
716 CefRequestHandler::WindowOpenDisposition target_disposition,
717 bool user_gesture) {
718 if (target_disposition == WOD_NEW_BACKGROUND_TAB ||
719 target_disposition == WOD_NEW_FOREGROUND_TAB) {
720 // Handle middle-click and ctrl + left-click by opening the URL in a new
721 // browser window.
722 auto config = std::make_unique<RootWindowConfig>();
723 config->with_controls = true;
724 config->with_osr = is_osr();
725 config->url = target_url;
726 MainContext::Get()->GetRootWindowManager()->CreateRootWindow(
727 std::move(config));
728 return true;
729 }
730
731 // Open the URL in the current browser window.
732 return false;
733 }
734
GetResourceRequestHandler(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,bool is_navigation,bool is_download,const CefString & request_initiator,bool & disable_default_handling)735 CefRefPtr<CefResourceRequestHandler> ClientHandler::GetResourceRequestHandler(
736 CefRefPtr<CefBrowser> browser,
737 CefRefPtr<CefFrame> frame,
738 CefRefPtr<CefRequest> request,
739 bool is_navigation,
740 bool is_download,
741 const CefString& request_initiator,
742 bool& disable_default_handling) {
743 CEF_REQUIRE_IO_THREAD();
744 return this;
745 }
746
GetAuthCredentials(CefRefPtr<CefBrowser> browser,const CefString & origin_url,bool isProxy,const CefString & host,int port,const CefString & realm,const CefString & scheme,CefRefPtr<CefAuthCallback> callback)747 bool ClientHandler::GetAuthCredentials(CefRefPtr<CefBrowser> browser,
748 const CefString& origin_url,
749 bool isProxy,
750 const CefString& host,
751 int port,
752 const CefString& realm,
753 const CefString& scheme,
754 CefRefPtr<CefAuthCallback> callback) {
755 CEF_REQUIRE_IO_THREAD();
756
757 // Used for testing authentication with a proxy server.
758 // For example, CCProxy on Windows.
759 if (isProxy) {
760 callback->Continue("guest", "guest");
761 return true;
762 }
763
764 // Used for testing authentication with https://jigsaw.w3.org/HTTP/.
765 if (host == "jigsaw.w3.org") {
766 callback->Continue("guest", "guest");
767 return true;
768 }
769
770 return false;
771 }
772
OnQuotaRequest(CefRefPtr<CefBrowser> browser,const CefString & origin_url,int64 new_size,CefRefPtr<CefCallback> callback)773 bool ClientHandler::OnQuotaRequest(CefRefPtr<CefBrowser> browser,
774 const CefString& origin_url,
775 int64 new_size,
776 CefRefPtr<CefCallback> callback) {
777 CEF_REQUIRE_IO_THREAD();
778
779 static const int64 max_size = 1024 * 1024 * 20; // 20mb.
780
781 // Grant the quota request if the size is reasonable.
782 if (new_size <= max_size)
783 callback->Continue();
784 else
785 callback->Cancel();
786 return true;
787 }
788
OnCertificateError(CefRefPtr<CefBrowser> browser,ErrorCode cert_error,const CefString & request_url,CefRefPtr<CefSSLInfo> ssl_info,CefRefPtr<CefCallback> callback)789 bool ClientHandler::OnCertificateError(CefRefPtr<CefBrowser> browser,
790 ErrorCode cert_error,
791 const CefString& request_url,
792 CefRefPtr<CefSSLInfo> ssl_info,
793 CefRefPtr<CefCallback> callback) {
794 CEF_REQUIRE_UI_THREAD();
795
796 if (cert_error == ERR_CERT_AUTHORITY_INVALID &&
797 request_url.ToString().find("https://www.magpcss.org/") == 0U) {
798 // Allow the CEF Forum to load. It has a self-signed certificate.
799 callback->Continue();
800 return true;
801 }
802
803 CefRefPtr<CefX509Certificate> cert = ssl_info->GetX509Certificate();
804 if (cert.get()) {
805 // Load the error page.
806 LoadErrorPage(browser->GetMainFrame(), request_url, cert_error,
807 GetCertificateInformation(cert, ssl_info->GetCertStatus()));
808 }
809
810 return false; // Cancel the request.
811 }
812
OnSelectClientCertificate(CefRefPtr<CefBrowser> browser,bool isProxy,const CefString & host,int port,const X509CertificateList & certificates,CefRefPtr<CefSelectClientCertificateCallback> callback)813 bool ClientHandler::OnSelectClientCertificate(
814 CefRefPtr<CefBrowser> browser,
815 bool isProxy,
816 const CefString& host,
817 int port,
818 const X509CertificateList& certificates,
819 CefRefPtr<CefSelectClientCertificateCallback> callback) {
820 CEF_REQUIRE_UI_THREAD();
821
822 CefRefPtr<CefCommandLine> command_line =
823 CefCommandLine::GetGlobalCommandLine();
824 if (!command_line->HasSwitch(switches::kSslClientCertificate)) {
825 return false;
826 }
827
828 const std::string& cert_name =
829 command_line->GetSwitchValue(switches::kSslClientCertificate);
830
831 if (cert_name.empty()) {
832 callback->Select(nullptr);
833 return true;
834 }
835
836 std::vector<CefRefPtr<CefX509Certificate>>::const_iterator it =
837 certificates.begin();
838 for (; it != certificates.end(); ++it) {
839 CefString subject((*it)->GetSubject()->GetDisplayName());
840 if (subject == cert_name) {
841 callback->Select(*it);
842 return true;
843 }
844 }
845
846 return true;
847 }
848
OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,TerminationStatus status)849 void ClientHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
850 TerminationStatus status) {
851 CEF_REQUIRE_UI_THREAD();
852
853 message_router_->OnRenderProcessTerminated(browser);
854
855 // Don't reload if there's no start URL, or if the crash URL was specified.
856 if (startup_url_.empty() || startup_url_ == "chrome://crash")
857 return;
858
859 CefRefPtr<CefFrame> frame = browser->GetMainFrame();
860 std::string url = frame->GetURL();
861
862 // Don't reload if the termination occurred before any URL had successfully
863 // loaded.
864 if (url.empty())
865 return;
866
867 std::string start_url = startup_url_;
868
869 // Convert URLs to lowercase for easier comparison.
870 std::transform(url.begin(), url.end(), url.begin(), tolower);
871 std::transform(start_url.begin(), start_url.end(), start_url.begin(),
872 tolower);
873
874 // Don't reload the URL that just resulted in termination.
875 if (url.find(start_url) == 0)
876 return;
877
878 frame->LoadURL(startup_url_);
879 }
880
OnDocumentAvailableInMainFrame(CefRefPtr<CefBrowser> browser)881 void ClientHandler::OnDocumentAvailableInMainFrame(
882 CefRefPtr<CefBrowser> browser) {
883 CEF_REQUIRE_UI_THREAD();
884
885 // Restore offline mode after main frame navigation. Otherwise, offline state
886 // (e.g. `navigator.onLine`) might be wrong in the renderer process.
887 if (offline_)
888 SetOfflineState(browser, true);
889 }
890
OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefCallback> callback)891 cef_return_value_t ClientHandler::OnBeforeResourceLoad(
892 CefRefPtr<CefBrowser> browser,
893 CefRefPtr<CefFrame> frame,
894 CefRefPtr<CefRequest> request,
895 CefRefPtr<CefCallback> callback) {
896 CEF_REQUIRE_IO_THREAD();
897
898 return resource_manager_->OnBeforeResourceLoad(browser, frame, request,
899 callback);
900 }
901
GetResourceHandler(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request)902 CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
903 CefRefPtr<CefBrowser> browser,
904 CefRefPtr<CefFrame> frame,
905 CefRefPtr<CefRequest> request) {
906 CEF_REQUIRE_IO_THREAD();
907
908 return resource_manager_->GetResourceHandler(browser, frame, request);
909 }
910
GetResourceResponseFilter(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response)911 CefRefPtr<CefResponseFilter> ClientHandler::GetResourceResponseFilter(
912 CefRefPtr<CefBrowser> browser,
913 CefRefPtr<CefFrame> frame,
914 CefRefPtr<CefRequest> request,
915 CefRefPtr<CefResponse> response) {
916 CEF_REQUIRE_IO_THREAD();
917
918 return test_runner::GetResourceResponseFilter(browser, frame, request,
919 response);
920 }
921
OnProtocolExecution(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,bool & allow_os_execution)922 void ClientHandler::OnProtocolExecution(CefRefPtr<CefBrowser> browser,
923 CefRefPtr<CefFrame> frame,
924 CefRefPtr<CefRequest> request,
925 bool& allow_os_execution) {
926 CEF_REQUIRE_IO_THREAD();
927
928 std::string urlStr = request->GetURL();
929
930 // Allow OS execution of Spotify URIs.
931 if (urlStr.find("spotify:") == 0)
932 allow_os_execution = true;
933 }
934
GetBrowserCount() const935 int ClientHandler::GetBrowserCount() const {
936 CEF_REQUIRE_UI_THREAD();
937 return browser_count_;
938 }
939
ShowDevTools(CefRefPtr<CefBrowser> browser,const CefPoint & inspect_element_at)940 void ClientHandler::ShowDevTools(CefRefPtr<CefBrowser> browser,
941 const CefPoint& inspect_element_at) {
942 if (!CefCurrentlyOn(TID_UI)) {
943 // Execute this method on the UI thread.
944 CefPostTask(TID_UI, base::BindOnce(&ClientHandler::ShowDevTools, this,
945 browser, inspect_element_at));
946 return;
947 }
948
949 CefWindowInfo windowInfo;
950 CefRefPtr<CefClient> client;
951 CefBrowserSettings settings;
952
953 MainContext::Get()->PopulateBrowserSettings(&settings);
954
955 CefRefPtr<CefBrowserHost> host = browser->GetHost();
956
957 // Test if the DevTools browser already exists.
958 bool has_devtools = host->HasDevTools();
959 if (!has_devtools) {
960 // Create a new RootWindow for the DevTools browser that will be created
961 // by ShowDevTools().
962 has_devtools = CreatePopupWindow(browser, true, CefPopupFeatures(),
963 windowInfo, client, settings);
964 }
965
966 if (has_devtools) {
967 // Create the DevTools browser if it doesn't already exist.
968 // Otherwise, focus the existing DevTools browser and inspect the element
969 // at |inspect_element_at| if non-empty.
970 host->ShowDevTools(windowInfo, client, settings, inspect_element_at);
971 }
972 }
973
CloseDevTools(CefRefPtr<CefBrowser> browser)974 void ClientHandler::CloseDevTools(CefRefPtr<CefBrowser> browser) {
975 browser->GetHost()->CloseDevTools();
976 }
977
HasSSLInformation(CefRefPtr<CefBrowser> browser)978 bool ClientHandler::HasSSLInformation(CefRefPtr<CefBrowser> browser) {
979 CefRefPtr<CefNavigationEntry> nav =
980 browser->GetHost()->GetVisibleNavigationEntry();
981
982 return (nav && nav->GetSSLStatus() &&
983 nav->GetSSLStatus()->IsSecureConnection());
984 }
985
ShowSSLInformation(CefRefPtr<CefBrowser> browser)986 void ClientHandler::ShowSSLInformation(CefRefPtr<CefBrowser> browser) {
987 std::stringstream ss;
988 CefRefPtr<CefNavigationEntry> nav =
989 browser->GetHost()->GetVisibleNavigationEntry();
990 if (!nav)
991 return;
992
993 CefRefPtr<CefSSLStatus> ssl = nav->GetSSLStatus();
994 if (!ssl)
995 return;
996
997 ss << "<html><head><title>SSL Information</title></head>"
998 "<body bgcolor=\"white\">"
999 "<h3>SSL Connection</h3>"
1000 << "<table border=1><tr><th>Field</th><th>Value</th></tr>";
1001
1002 CefURLParts urlparts;
1003 if (CefParseURL(nav->GetURL(), urlparts)) {
1004 CefString port(&urlparts.port);
1005 ss << "<tr><td>Server</td><td>" << CefString(&urlparts.host).ToString();
1006 if (!port.empty())
1007 ss << ":" << port.ToString();
1008 ss << "</td></tr>";
1009 }
1010
1011 ss << "<tr><td>SSL Version</td><td>"
1012 << GetSSLVersionString(ssl->GetSSLVersion()) << "</td></tr>";
1013 ss << "<tr><td>Content Status</td><td>"
1014 << GetContentStatusString(ssl->GetContentStatus()) << "</td></tr>";
1015
1016 ss << "</table>";
1017
1018 CefRefPtr<CefX509Certificate> cert = ssl->GetX509Certificate();
1019 if (cert.get())
1020 ss << GetCertificateInformation(cert, ssl->GetCertStatus());
1021
1022 ss << "</body></html>";
1023
1024 auto config = std::make_unique<RootWindowConfig>();
1025 config->with_controls = false;
1026 config->with_osr = is_osr();
1027 config->url = test_runner::GetDataURI(ss.str(), "text/html");
1028 MainContext::Get()->GetRootWindowManager()->CreateRootWindow(
1029 std::move(config));
1030 }
1031
SetStringResource(const std::string & page,const std::string & data)1032 void ClientHandler::SetStringResource(const std::string& page,
1033 const std::string& data) {
1034 if (!CefCurrentlyOn(TID_IO)) {
1035 CefPostTask(TID_IO, base::BindOnce(&ClientHandler::SetStringResource, this,
1036 page, data));
1037 return;
1038 }
1039
1040 string_resource_map_[page] = data;
1041 }
1042
CreatePopupWindow(CefRefPtr<CefBrowser> browser,bool is_devtools,const CefPopupFeatures & popupFeatures,CefWindowInfo & windowInfo,CefRefPtr<CefClient> & client,CefBrowserSettings & settings)1043 bool ClientHandler::CreatePopupWindow(CefRefPtr<CefBrowser> browser,
1044 bool is_devtools,
1045 const CefPopupFeatures& popupFeatures,
1046 CefWindowInfo& windowInfo,
1047 CefRefPtr<CefClient>& client,
1048 CefBrowserSettings& settings) {
1049 CEF_REQUIRE_UI_THREAD();
1050
1051 // The popup browser will be parented to a new native window.
1052 // Don't show URL bar and navigation buttons on DevTools windows.
1053 MainContext::Get()->GetRootWindowManager()->CreateRootWindowAsPopup(
1054 !is_devtools, is_osr(), popupFeatures, windowInfo, client, settings);
1055
1056 return true;
1057 }
1058
NotifyBrowserCreated(CefRefPtr<CefBrowser> browser)1059 void ClientHandler::NotifyBrowserCreated(CefRefPtr<CefBrowser> browser) {
1060 if (!CURRENTLY_ON_MAIN_THREAD()) {
1061 // Execute this method on the main thread.
1062 MAIN_POST_CLOSURE(
1063 base::BindOnce(&ClientHandler::NotifyBrowserCreated, this, browser));
1064 return;
1065 }
1066
1067 if (delegate_)
1068 delegate_->OnBrowserCreated(browser);
1069 }
1070
NotifyBrowserClosing(CefRefPtr<CefBrowser> browser)1071 void ClientHandler::NotifyBrowserClosing(CefRefPtr<CefBrowser> browser) {
1072 if (!CURRENTLY_ON_MAIN_THREAD()) {
1073 // Execute this method on the main thread.
1074 MAIN_POST_CLOSURE(
1075 base::BindOnce(&ClientHandler::NotifyBrowserClosing, this, browser));
1076 return;
1077 }
1078
1079 if (delegate_)
1080 delegate_->OnBrowserClosing(browser);
1081 }
1082
NotifyBrowserClosed(CefRefPtr<CefBrowser> browser)1083 void ClientHandler::NotifyBrowserClosed(CefRefPtr<CefBrowser> browser) {
1084 if (!CURRENTLY_ON_MAIN_THREAD()) {
1085 // Execute this method on the main thread.
1086 MAIN_POST_CLOSURE(
1087 base::BindOnce(&ClientHandler::NotifyBrowserClosed, this, browser));
1088 return;
1089 }
1090
1091 if (delegate_)
1092 delegate_->OnBrowserClosed(browser);
1093 }
1094
NotifyAddress(const CefString & url)1095 void ClientHandler::NotifyAddress(const CefString& url) {
1096 if (!CURRENTLY_ON_MAIN_THREAD()) {
1097 // Execute this method on the main thread.
1098 MAIN_POST_CLOSURE(base::BindOnce(&ClientHandler::NotifyAddress, this, url));
1099 return;
1100 }
1101
1102 if (delegate_)
1103 delegate_->OnSetAddress(url);
1104 }
1105
NotifyTitle(const CefString & title)1106 void ClientHandler::NotifyTitle(const CefString& title) {
1107 if (!CURRENTLY_ON_MAIN_THREAD()) {
1108 // Execute this method on the main thread.
1109 MAIN_POST_CLOSURE(base::BindOnce(&ClientHandler::NotifyTitle, this, title));
1110 return;
1111 }
1112
1113 if (delegate_)
1114 delegate_->OnSetTitle(title);
1115 }
1116
NotifyFavicon(CefRefPtr<CefImage> image)1117 void ClientHandler::NotifyFavicon(CefRefPtr<CefImage> image) {
1118 if (!CURRENTLY_ON_MAIN_THREAD()) {
1119 // Execute this method on the main thread.
1120 MAIN_POST_CLOSURE(
1121 base::BindOnce(&ClientHandler::NotifyFavicon, this, image));
1122 return;
1123 }
1124
1125 if (delegate_)
1126 delegate_->OnSetFavicon(image);
1127 }
1128
NotifyFullscreen(bool fullscreen)1129 void ClientHandler::NotifyFullscreen(bool fullscreen) {
1130 if (!CURRENTLY_ON_MAIN_THREAD()) {
1131 // Execute this method on the main thread.
1132 MAIN_POST_CLOSURE(
1133 base::BindOnce(&ClientHandler::NotifyFullscreen, this, fullscreen));
1134 return;
1135 }
1136
1137 if (delegate_)
1138 delegate_->OnSetFullscreen(fullscreen);
1139 }
1140
NotifyAutoResize(const CefSize & new_size)1141 void ClientHandler::NotifyAutoResize(const CefSize& new_size) {
1142 if (!CURRENTLY_ON_MAIN_THREAD()) {
1143 // Execute this method on the main thread.
1144 MAIN_POST_CLOSURE(
1145 base::BindOnce(&ClientHandler::NotifyAutoResize, this, new_size));
1146 return;
1147 }
1148
1149 if (delegate_)
1150 delegate_->OnAutoResize(new_size);
1151 }
1152
NotifyLoadingState(bool isLoading,bool canGoBack,bool canGoForward)1153 void ClientHandler::NotifyLoadingState(bool isLoading,
1154 bool canGoBack,
1155 bool canGoForward) {
1156 if (!CURRENTLY_ON_MAIN_THREAD()) {
1157 // Execute this method on the main thread.
1158 MAIN_POST_CLOSURE(base::BindOnce(&ClientHandler::NotifyLoadingState, this,
1159 isLoading, canGoBack, canGoForward));
1160 return;
1161 }
1162
1163 if (delegate_)
1164 delegate_->OnSetLoadingState(isLoading, canGoBack, canGoForward);
1165 }
1166
NotifyDraggableRegions(const std::vector<CefDraggableRegion> & regions)1167 void ClientHandler::NotifyDraggableRegions(
1168 const std::vector<CefDraggableRegion>& regions) {
1169 if (!CURRENTLY_ON_MAIN_THREAD()) {
1170 // Execute this method on the main thread.
1171 MAIN_POST_CLOSURE(
1172 base::BindOnce(&ClientHandler::NotifyDraggableRegions, this, regions));
1173 return;
1174 }
1175
1176 if (delegate_)
1177 delegate_->OnSetDraggableRegions(regions);
1178 }
1179
NotifyTakeFocus(bool next)1180 void ClientHandler::NotifyTakeFocus(bool next) {
1181 if (!CURRENTLY_ON_MAIN_THREAD()) {
1182 // Execute this method on the main thread.
1183 MAIN_POST_CLOSURE(
1184 base::BindOnce(&ClientHandler::NotifyTakeFocus, this, next));
1185 return;
1186 }
1187
1188 if (delegate_)
1189 delegate_->OnTakeFocus(next);
1190 }
1191
BuildTestMenu(CefRefPtr<CefMenuModel> model)1192 void ClientHandler::BuildTestMenu(CefRefPtr<CefMenuModel> model) {
1193 if (model->GetCount() > 0)
1194 model->AddSeparator();
1195
1196 // Build the sub menu.
1197 CefRefPtr<CefMenuModel> submenu =
1198 model->AddSubMenu(CLIENT_ID_TESTMENU_SUBMENU, "Context Menu Test");
1199 submenu->AddCheckItem(CLIENT_ID_TESTMENU_CHECKITEM, "Check Item");
1200 submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM1, "Radio Item 1", 0);
1201 submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM2, "Radio Item 2", 0);
1202 submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM3, "Radio Item 3", 0);
1203
1204 // Check the check item.
1205 if (test_menu_state_.check_item)
1206 submenu->SetChecked(CLIENT_ID_TESTMENU_CHECKITEM, true);
1207
1208 // Check the selected radio item.
1209 submenu->SetChecked(
1210 CLIENT_ID_TESTMENU_RADIOITEM1 + test_menu_state_.radio_item, true);
1211 }
1212
ExecuteTestMenu(int command_id)1213 bool ClientHandler::ExecuteTestMenu(int command_id) {
1214 if (command_id == CLIENT_ID_TESTMENU_CHECKITEM) {
1215 // Toggle the check item.
1216 test_menu_state_.check_item ^= 1;
1217 return true;
1218 } else if (command_id >= CLIENT_ID_TESTMENU_RADIOITEM1 &&
1219 command_id <= CLIENT_ID_TESTMENU_RADIOITEM3) {
1220 // Store the selected radio item.
1221 test_menu_state_.radio_item = (command_id - CLIENT_ID_TESTMENU_RADIOITEM1);
1222 return true;
1223 }
1224
1225 // Allow default handling to proceed.
1226 return false;
1227 }
1228
SetOfflineState(CefRefPtr<CefBrowser> browser,bool offline)1229 void ClientHandler::SetOfflineState(CefRefPtr<CefBrowser> browser,
1230 bool offline) {
1231 // See DevTools protocol docs for message format specification.
1232 CefRefPtr<CefDictionaryValue> params = CefDictionaryValue::Create();
1233 params->SetBool("offline", offline);
1234 params->SetDouble("latency", 0);
1235 params->SetDouble("downloadThroughput", 0);
1236 params->SetDouble("uploadThroughput", 0);
1237 browser->GetHost()->ExecuteDevToolsMethod(
1238 /*message_id=*/0, "Network.emulateNetworkConditions", params);
1239 }
1240
1241 } // namespace client
1242