///'// 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 << ///').

///'///';//n//n frame->LoadURL(GetDataURI(ss.str(), ///'text/html///'));//n}//n//nvoid SimpleHandler::CloseAllBrowsers(bool force_close) {//n if (!CefCurrentlyOn(TID_UI)) {//n // Execute on the UI thread.//n CefPostTask(TID_UI, base::BindOnce(&SimpleHandler::CloseAllBrowsers, this,//n force_close));//n return;//n }//n//n if (browser_list_.empty()) {//n return;//n }//n//n BrowserList::const_iterator it = browser_list_.begin();//n for (; it != browser_list_.end(); ++it) {//n (*it)->GetHost()->CloseBrowser(force_close);//n }//n}//n//n// static//nbool SimpleHandler::IsChromeRuntimeEnabled() {//n static int value = -1;//n if (value == -1) {//n CefRefPtr command_line =//n CefCommandLine::GetGlobalCommandLine();//n value = command_line->HasSwitch(///'enable-chrome-runtime///') ? 1 : 0;//n }//n return value == 1;//n}//n

CEF SimpleHandler 类代码解析 - 浏览器事件处理

原文地址: https://www.cveoy.top/t/topic/pRGo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录