2013-11-26 23:02:14 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefsimple/simple_handler.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
#include "include/cef_app.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "include/views/cef_browser_view.h"
|
|
|
|
#include "include/views/cef_window.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
SimpleHandler* g_instance = NULL;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
SimpleHandler::SimpleHandler(bool use_views)
|
|
|
|
: use_views_(use_views),
|
|
|
|
is_closing_(false) {
|
2014-07-11 22:10:05 +02:00
|
|
|
DCHECK(!g_instance);
|
2013-11-26 23:02:14 +01:00
|
|
|
g_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleHandler::~SimpleHandler() {
|
|
|
|
g_instance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
SimpleHandler* SimpleHandler::GetInstance() {
|
|
|
|
return g_instance;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& title) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
if (use_views_) {
|
|
|
|
// Set the title of the window using the Views framework.
|
|
|
|
CefRefPtr<CefBrowserView> browser_view =
|
|
|
|
CefBrowserView::GetForBrowser(browser);
|
|
|
|
if (browser_view) {
|
|
|
|
CefRefPtr<CefWindow> window = browser_view->GetWindow();
|
|
|
|
if (window)
|
|
|
|
window->SetTitle(title);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Set the title of the window using platform APIs.
|
|
|
|
PlatformTitleChange(browser, title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 23:02:14 +01:00
|
|
|
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
2014-07-11 22:10:05 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
// Add to the list of existing browsers.
|
|
|
|
browser_list_.push_back(browser);
|
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser) {
|
2014-07-11 22:10:05 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2014-04-09 20:29:51 +02:00
|
|
|
|
|
|
|
// Closing the main window requires special handling. See the DoClose()
|
|
|
|
// documentation in the CEF header for a detailed destription of this
|
|
|
|
// process.
|
|
|
|
if (browser_list_.size() == 1) {
|
|
|
|
// Set a flag to indicate that the window close should be allowed.
|
|
|
|
is_closing_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the close. For windowed browsers this will result in the OS close
|
|
|
|
// event being sent.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-26 23:02:14 +01:00
|
|
|
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
2014-07-11 22:10:05 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
// Remove from the list of existing browsers.
|
|
|
|
BrowserList::iterator bit = browser_list_.begin();
|
|
|
|
for (; bit != browser_list_.end(); ++bit) {
|
|
|
|
if ((*bit)->IsSame(browser)) {
|
|
|
|
browser_list_.erase(bit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_list_.empty()) {
|
|
|
|
// All browser windows have closed. Quit the application message loop.
|
|
|
|
CefQuitMessageLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
ErrorCode errorCode,
|
|
|
|
const CefString& errorText,
|
|
|
|
const CefString& failedUrl) {
|
2014-07-11 22:10:05 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
// Don't display an error for downloaded files.
|
|
|
|
if (errorCode == ERR_ABORTED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Display a load error message.
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "<html><body bgcolor=\"white\">"
|
|
|
|
"<h2>Failed to load URL " << std::string(failedUrl) <<
|
|
|
|
" with error " << std::string(errorText) << " (" << errorCode <<
|
|
|
|
").</h2></body></html>";
|
|
|
|
frame->LoadString(ss.str(), failedUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleHandler::CloseAllBrowsers(bool force_close) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
// Execute on the UI thread.
|
|
|
|
CefPostTask(TID_UI,
|
2014-07-15 00:18:51 +02:00
|
|
|
base::Bind(&SimpleHandler::CloseAllBrowsers, this, force_close));
|
2013-11-26 23:02:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_list_.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
BrowserList::const_iterator it = browser_list_.begin();
|
|
|
|
for (; it != browser_list_.end(); ++it)
|
|
|
|
(*it)->GetHost()->CloseBrowser(force_close);
|
|
|
|
}
|