95 lines
2.4 KiB
C++
95 lines
2.4 KiB
C++
|
// 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.
|
||
|
|
||
|
#include "cefsimple/simple_handler.h"
|
||
|
|
||
|
#include <sstream>
|
||
|
#include <string>
|
||
|
|
||
|
#include "cefsimple/util.h"
|
||
|
#include "include/cef_app.h"
|
||
|
#include "include/cef_runnable.h"
|
||
|
|
||
|
namespace {
|
||
|
|
||
|
SimpleHandler* g_instance = NULL;
|
||
|
|
||
|
} // namespace
|
||
|
|
||
|
SimpleHandler::SimpleHandler() {
|
||
|
ASSERT(!g_instance);
|
||
|
g_instance = this;
|
||
|
}
|
||
|
|
||
|
SimpleHandler::~SimpleHandler() {
|
||
|
g_instance = NULL;
|
||
|
}
|
||
|
|
||
|
// static
|
||
|
SimpleHandler* SimpleHandler::GetInstance() {
|
||
|
return g_instance;
|
||
|
}
|
||
|
|
||
|
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
||
|
REQUIRE_UI_THREAD();
|
||
|
|
||
|
// Add to the list of existing browsers.
|
||
|
browser_list_.push_back(browser);
|
||
|
}
|
||
|
|
||
|
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
||
|
REQUIRE_UI_THREAD();
|
||
|
|
||
|
// 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) {
|
||
|
REQUIRE_UI_THREAD();
|
||
|
|
||
|
// 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,
|
||
|
NewCefRunnableMethod(this, &SimpleHandler::CloseAllBrowsers,
|
||
|
force_close));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (browser_list_.empty())
|
||
|
return;
|
||
|
|
||
|
BrowserList::const_iterator it = browser_list_.begin();
|
||
|
for (; it != browser_list_.end(); ++it)
|
||
|
(*it)->GetHost()->CloseBrowser(force_close);
|
||
|
}
|