///'// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights//n// reserved. Use of this source code is governed by a BSD-style license that//n// can be found in the LICENSE file.//n//n#include ///'tests/cefsimple/simple_handler.h///'//n//n#include //n#include //n//n#include ///'include/base/cef_callback.h///'//n#include ///'include/cef_app.h///'//n#include ///'include/cef_parser.h///'//n#include ///'include/views/cef_browser_view.h///'//n#include ///'include/views/cef_window.h///'//n#include ///'include/wrapper/cef_closure_task.h///'//n#include ///'include/wrapper/cef_helpers.h///'//n//nnamespace {//n//nSimpleHandler* g_instance = nullptr;//n//n// Returns a data: URI with the specified contents.//nstd::string GetDataURI(const std::string& data, const std::string& mime_type) {//n return ///'data:///' + mime_type + ///;base64,///' +//n CefURIEncode(CefBase64Encode(data.data(), data.size()), false)//n .ToString();//n}//n//n} // namespace//n//nSimpleHandler::SimpleHandler(bool use_views)//n : use_views_(use_views), is_closing_(false) {//n DCHECK(!g_instance);//n g_instance = this;//n}//n//nSimpleHandler::~SimpleHandler() {//n g_instance = nullptr;//n}//n//n// static//nSimpleHandler* SimpleHandler::GetInstance() {//n return g_instance;//n}//n//nvoid SimpleHandler::OnTitleChange(CefRefPtr browser,//n const CefString& title) {//n CEF_REQUIRE_UI_THREAD();//n//n if (use_views_) {//n // Set the title of the window using the Views framework.//n CefRefPtr browser_view =//n CefBrowserView::GetForBrowser(browser);//n if (browser_view) {//n CefRefPtr window = browser_view->GetWindow();//n if (window) {//n window->SetTitle(title);//n }//n }//n } else if (!IsChromeRuntimeEnabled()) {//n // Set the title of the window using platform APIs.//n PlatformTitleChange(browser, title);//n }//n}//n//nvoid SimpleHandler::OnAfterCreated(CefRefPtr browser) {//n CEF_REQUIRE_UI_THREAD();//n//n // Add to the list of existing browsers.//n browser_list_.push_back(browser);//n}//n//nbool SimpleHandler::DoClose(CefRefPtr browser) {//n CEF_REQUIRE_UI_THREAD();//n//n // Closing the main window requires special handling. See the DoClose()//n // documentation in the CEF header for a detailed destription of this//n // process.//n if (browser_list_.size() == 1) {//n // Set a flag to indicate that the window close should be allowed.//n is_closing_ = true;//n }//n//n // Allow the close. For windowed browsers this will result in the OS close//n // event being sent.//n return false;//n}//n//nvoid SimpleHandler::OnBeforeClose(CefRefPtr browser) {//n CEF_REQUIRE_UI_THREAD();//n//n // Remove from the list of existing browsers.//n BrowserList::iterator bit = browser_list_.begin();//n for (; bit != browser_list_.end(); ++bit) {//n if ((*bit)->IsSame(browser)) {//n browser_list_.erase(bit);//n break;//n }//n }//n//n if (browser_list_.empty()) {//n // All browser windows have closed. Quit the application message loop.//n CefQuitMessageLoop();//n }//n}//n//nvoid SimpleHandler::OnLoadError(CefRefPtr browser,//n CefRefPtr frame,//n ErrorCode errorCode,//n const CefString& errorText,//n const CefString& failedUrl) {//n CEF_REQUIRE_UI_THREAD();//n//n // Allow Chrome to show the error page.//n if (IsChromeRuntimeEnabled()) {//n return;//n }//n//n // Don't display an error for downloaded files.//n if (errorCode == ERR_ABORTED) {//n return;//n }//n//n // Display a load error message using a data: URI.//n std::stringstream ss;//n ss << ///'<body bgcolor=///'white///'>///'//n ///'Failed to load URL ///'//n << std::string(failedUrl) << ///' with error ///' << std::string(errorText)//n << ///' (///' << errorCode << ///').
///'