Tutorial Tutorial explaining how to create a simple application using CEF3. Updated Aug 13, 2014 by [email protected] IntroductionThis tutorial explains how to create a simple application using CEF3. It references the cefsimple Getting StartedBegin by downloading a binary distribution for your platform from http://cefbuilds.com. For the purposes of this tutorial Building the Included ProjectApplications based on CEF binary distributions can be built using standard platform build tools. This includes Visual Studio on Windows, Xcode on Mac OS X and gcc/make on Linux. Each platform has a slightly different WindowsFollow these steps to build the cefsimple application on Windows:
LinuxFollow these steps to build the cefsimple application on Linux:
Mac OS XFollow these steps to build the cefsimple application on OS X:
Loading a Custom URLThe cefsimple application loads google.com by default but you can change it to load a custom URL instead. The easiest way to load a different URL is via the command-line. # Load the local file “c:\example\example.html” cefsimple.exe --url=file://c:/example/example.html You can also edit the source code in cefsimple/simple_app.cpp and recompile the application to load your custom URL by default. # Load the local file “c:\example\example.html” … if (url.empty()) url = "file://c:/example/example.html"; … Application ComponentsAll CEF applications have the following primary components:
The CEF dynamic library, support libraries and resources will be the same for every CEF-based application. They are included in the Debug/Release or Resources directory of the binary distribution. See the README.txt Architecture in 60 SecondsThe below list summarizes the items of primary importance for this tutorial:
Read the GeneralUsage Wiki page for complete discussion of the above points. Source CodeThe cefsimple application initializes CEF and creates a single popup browser window. The application terminates when all browser windows have been closed. Program flow is as follows:
Your binary distribution may include newer versions of the below files. However, the general concepts remain unchanged. Entry Point FunctionExecution begins in the browser process entry point function. This function is responsible for initializing CEF and any OS-related objects. For example, it initializes GTK on Linux and allocates the necessary Cocoa Windows#include <windows.h> #include "cefsimple/simple_app.h" #include "include/cef_sandbox_win.h" // Set to 0 to disable sandbox support. #define CEF_ENABLE_SANDBOX 1 #if CEF_ENABLE_SANDBOX // The cef_sandbox.lib static library is currently built with VS2010. It may not // link successfully with other VS versions. #pragma comment(lib, "cef_sandbox.lib") #endif // Entry point function for all processes. int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); void* sandbox_info = NULL; #if CEF_ENABLE_SANDBOX // Manage the life span of the sandbox information object. This is necessary // for sandbox support on Windows. See cef_sandbox_win.h for complete details. CefScopedSandboxInfo scoped_sandbox; sandbox_info = scoped_sandbox.sandbox_info(); #endif // Provide CEF with command-line arguments. CefMainArgs main_args(hInstance); // SimpleApp implements application-level callbacks. It will create the first // browser instance in OnContextInitialized() after CEF has initialized. CefRefPtr<SimpleApp> app(new SimpleApp); // CEF applications have multiple sub-processes (render, plugin, GPU, etc) // that share the same executable. This function checks the command-line and, // if this is a sub-process, executes the appropriate logic. int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info); if (exit_code >= 0) { // The sub-process has completed so return here. return exit_code; } // Specify CEF global settings here. CefSettings settings; #if !CEF_ENABLE_SANDBOX settings.no_sandbox = true; #endif // Initialize CEF. CefInitialize(main_args, settings, app.get(), sandbox_info); // Run the CEF message loop. This will block until CefQuitMessageLoop() is // called. CefRunMessageLoop(); // Shut down CEF. CefShutdown(); return 0; } Linux// cefsimple_gtk.cpp #include <gtk/gtk.h> #include "cefsimple/simple_app.h" // Entry point function for all processes. int main(int argc, char* argv[]) { // Provide CEF with command-line arguments. CefMainArgs main_args(argc, argv); // SimpleApp implements application-level callbacks. It will create the first // browser instance in OnContextInitialized() after CEF has initialized. CefRefPtr<SimpleApp> app(new SimpleApp); // CEF applications have multiple sub-processes (render, plugin, GPU, etc) // that share the same executable. This function checks the command-line and, // if this is a sub-process, executes the appropriate logic. int exit_code = CefExecuteProcess(main_args, app.get(), NULL); if (exit_code >= 0) { // The sub-process has completed so return here. return exit_code; } // Initialize GTK. gtk_init(&argc, &argv); // Specify CEF global settings here. CefSettings settings; // Initialize CEF for the browser process. CefInitialize(main_args, settings, app.get(), NULL); // Run the CEF message loop. This will block until CefQuitMessageLoop() is // called. CefRunMessageLoop(); // Shut down CEF. CefShutdown(); return 0; } Mac OS XBrowser process entry point: // cefsimple_mac.mm #import <Cocoa/Cocoa.h> #include "cefsimple/simple_app.h" #include "cefsimple/simple_handler.h" #include "cefsimple/util.h" #include "include/cef_application_mac.h" // Provide the CefAppProtocol implementation required by CEF. @interface SimpleApplication : NSApplication<CefAppProtocol> { @private BOOL handlingSendEvent_; } @end @implementation SimpleApplication - (BOOL)isHandlingSendEvent { return handlingSendEvent_; } - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { handlingSendEvent_ = handlingSendEvent; } - (void)sendEvent:(NSEvent*)event { CefScopedSendingEvent sendingEventScoper; [super sendEvent:event]; } @end // Receives notifications from the application. @interface SimpleAppDelegate : NSObject - (void)createApp:(id)object; @end @implementation SimpleAppDelegate // Create the application on the UI thread. - (void)createApp:(id)object { [NSApplication sharedApplication]; [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; // Set the delegate for application events. [NSApp setDelegate:self]; } // Called when the application‘s Quit menu item is selected. - (NSApplicationTerminateReply)applicationShouldTerminate: (NSApplication *)sender { // Request that all browser windows close. if (SimpleHandler* handler = SimpleHandler::GetInstance()) handler->CloseAllBrowsers(false); // Cancel the termination. The application will exit after all windows have // closed. return NSTerminateCancel; } // Sent immediately before the application terminates. This signal should not // be called because we cancel the termination. - (void)applicationWillTerminate:(NSNotification *)aNotification { ASSERT(false); // Not reached. } @end // Entry point function for the browser process. int main(int argc, char* argv[]) { // Provide CEF with command-line arguments. CefMainArgs main_args(argc, argv); // SimpleApp implements application-level callbacks. It will create the first // browser instance in OnContextInitialized() after CEF has initialized. CefRefPtr<SimpleApp> app(new SimpleApp); // Initialize the AutoRelease pool. NSAutoreleasePool* autopool = [[NSAutoreleasePool alloc] init]; // Initialize the SimpleApplication instance. [SimpleApplication sharedApplication]; // Specify CEF global settings here. CefSettings settings; // Initialize CEF for the browser process. CefInitialize(main_args, settings, app.get(), NULL); // Create the application delegate. NSObject* delegate = [[SimpleAppDelegate alloc] init]; [delegate performSelectorOnMainThread:@selector(createApp:) withObject:nil waitUntilDone:NO]; // Run the CEF message loop. This will block until CefQuitMessageLoop() is // called. CefRunMessageLoop(); // Shut down CEF. CefShutdown(); // Release the delegate. [delegate release]; // Release the AutoRelease pool. [autopool release]; return 0; } Sub-process entry point: // process_helper_mac.cpp #include "include/cef_app.h" // Entry point function for sub-processes. int main(int argc, char* argv[]) { // Provide CEF with command-line arguments. CefMainArgs main_args(argc, argv); // Execute the sub-process. return CefExecuteProcess(main_args, NULL, NULL); } SimpleAppSimpleApp is responsible for handling process-level callbacks. It exposes some interfaces/methods that are shared by multiple processes and some that are only called in a particular process. The CefBrowserProcessHandler // simple_app.h #include "include/cef_app.h" class SimpleApp : public CefApp, public CefBrowserProcessHandler { public: SimpleApp(); // CefApp methods: virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE { return this; } // CefBrowserProcessHandler methods: virtual void OnContextInitialized() OVERRIDE; private: // Include the default reference counting implementation. IMPLEMENT_REFCOUNTING(SimpleApp); }; // simple_app.cpp #include "cefsimple/simple_app.h" #include <string> #include "cefsimple/simple_handler.h" #include "cefsimple/util.h" #include "include/cef_browser.h" #include "include/cef_command_line.h" SimpleApp::SimpleApp() { } void SimpleApp::OnContextInitialized() { REQUIRE_UI_THREAD(); // Information used when creating the native window. CefWindowInfo window_info; #if defined(OS_WIN) // On Windows we need to specify certain flags that will be passed to // CreateWindowEx(). window_info.SetAsPopup(NULL, "cefsimple"); #endif // SimpleHandler implements browser-level callbacks. CefRefPtr<SimpleHandler> handler(new SimpleHandler()); // Specify CEF browser settings here. CefBrowserSettings browser_settings; std::string url; // Check if a "--url=" value was provided via the command-line. If so, use // that instead of the default URL. CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine(); url = command_line->GetSwitchValue("url"); if (url.empty()) url = "http://www.google.com"; // Create the first browser window. CefBrowserHost::CreateBrowserSync(window_info, handler.get(), url, browser_settings, NULL); } SimpleHandlerSimpleHandler is responsible for handling browser-level callbacks. These callbacks are executed in the browser process. In this example we use the same CefClient instance for all browsers, but your application can // simple_handler.h #include "include/cef_client.h" #include <list> class SimpleHandler : public CefClient, public CefDisplayHandler, public CefLifeSpanHandler, public CefLoadHandler { public: SimpleHandler(); ~SimpleHandler(); // Provide access to the single global instance of this object. static SimpleHandler* GetInstance(); // CefClient methods: virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE { return this; } virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE { return this; } virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE { return this; } // CefDisplayHandler methods: virtual void OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) OVERRIDE; // CefLifeSpanHandler methods: virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE; virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE; // CefLoadHandler methods: virtual void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE; // Request that all existing browser windows close. void CloseAllBrowsers(bool force_close); private: // List of existing browser windows. Only accessed on the CEF UI thread. typedef std::list<CefRefPtr<CefBrowser> > BrowserList; BrowserList browser_list_; // Include the default reference counting implementation. IMPLEMENT_REFCOUNTING(SimpleHandler); }; // simple_handler.cpp #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); } // simple_handler_win.cpp -- Windows-specific code. #include "cefsimple/simple_handler.h" #include <string> #include <windows.h> #include "cefsimple/util.h" #include "include/cef_browser.h" void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) { REQUIRE_UI_THREAD(); CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle(); SetWindowText(hwnd, std::wstring(title).c_str()); } // simple_handler_gtk.cc -- Linux-specific code. #include "cefsimple/simple_handler.h" #include <gtk/gtk.h> #include <string> #include "cefsimple/util.h" #include "include/cef_browser.h" void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) { REQUIRE_UI_THREAD(); GtkWidget* window = gtk_widget_get_ancestor( GTK_WIDGET(browser->GetHost()->GetWindowHandle()), GTK_TYPE_WINDOW); std::string titleStr(title); gtk_window_set_title(GTK_WINDOW(window), titleStr.c_str()); } // simple_handler_mac.mm -- OS X-specific code. #include "cefsimple/simple_handler.h" #import <Cocoa/Cocoa.h> #include "cefsimple/util.h" #include "include/cef_browser.h" void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) { REQUIRE_UI_THREAD(); NSView* view = (NSView*)browser->GetHost()->GetWindowHandle(); NSWindow* window = [view window]; std::string titleStr(title); NSString* str = [NSString stringWithUTF8String:titleStr.c_str()]; [window setTitle:str]; } Build StepsBuild steps vary depending on the platform. Explore the project files included with the binary distribution for a complete understanding of all required steps. The build steps common to all platforms can generally
Windows
The resulting directory structure looks like this: Application/ cefsimple.exe <= cefsimple application executable libcef.dll <= main CEF library icudt.dll <= ICU unicode support library ffmpegsumo.dll <= HTML5 audio/video support library libEGL.dll, libGLESv2.dll, … <= accelerated compositing support libraries cef.pak, devtools_resources.pak <= non-localized resources and strings locales/ en-US.pak, … <= locale-specific resources and strings Linux
The resulting directory structure looks like this: Application/ cefsimple <= cefsimple application executable chrome-sandbox <= sandbox support binary libcef.so <= main CEF library ffmpegsumo.so <-- HTML5 audio/video support library cef.pak, devtools_resources.pak <= non-localized resources and strings locales/ en-US.pak, … <= locale-specific resources and strings Mac OS X
The resulting directory structure looks like this: cefsimple.app/ Contents/ Frameworks/ Chromium Embedded Framework.framework/ Chromium Embedded Framework <= main application library Libraries/ ffmpegsumo.so <= HTML5 audio/video support library Resources/ cef.pak, devtools_resources.pak <= non-localized resources and strings crash_inspector, crash_report_sender <= breakpad support en.lproj/, ... <= locale-specific resources and strings Info.plist libplugin_carbon_interpose.dylib <= plugin support library cefsimple Helper.app/ Contents/ Info.plist MacOS/ cefsimple Helper <= helper executable Pkginfo cefsimple Helper EH.app/ Contents/ Info.plist MacOS/ cefsimple Helper EH <= helper executable Pkginfo cefsimple Helper NP.app/ Contents/ Info.plist MacOS/ cefsimple Helper NP <= helper executable Pkginfo Info.plist MacOS/ cefsimple <= cefsimple application executable Pkginfo Resources/ cefsimple.icns, ... <= cefsimple application resources |
libcef Tutorial
时间: 2024-11-10 12:46:29
libcef Tutorial的相关文章
chromium嵌入式框架libcef简介
Introduction CEF简介 The Chromium Embedded Framework (CEF) is a simple framework for embedding Chromium-based browsers in other applications. It is a BSD-licensed open source project founded by Marshall Greenblatt in 2008 and based on the Google Chr
初译 Support Vector Machines:A Simple Tutorial(一)
从本次开始我将开始尝试着逐章翻译一下 Alexey Nefedov的<Support Vector Machines:A Simple Tutorial>这本教材,这可是我们导师极力推荐的SVM教材,看了好久一直感觉一脸懵逼,索性开坑翻译一下吧,也当是加深理解,毕竟我也是一知半解,如果翻译的有不对的地方还望大佬们斧正,欢迎提意见,欢迎讨论. 嗯,就是这样. (一)Introduction 在本章节中将会介绍一些用于定义支持向量机(SVM)的基础的概念,这些概念对于理解SVM至关重要,假定读者了
ZetCode PyQt4 tutorial Dialogs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ
简单的LESS Tutorial
1. 什么是LESS? LESS是一种动态的CSS语言,更专业的称呼是CSS preprocessor.作为CSS的扩展语言,LESS可以让CSS文件逻辑上更清晰,从而更容易维护和更新.LESS是开源的,诞生于2009年,采用javascript开发, LESS深受另外一种动态CSS语言SASS/SCSS的影响(SCSS是SASS的升级版) .相对于SASS/SCSS或者其他CSS preprocessor, LESS的典型特征有两个, 支持实时编译,例如网页或者应用可以直接应用less文件,通
(翻译)deeplearning.net/tutorial —— 栈式去噪自编码器(SdA)
前言 栈式去噪自编码器是栈式自动编码器的扩展[Bengio07],并且它在[Vincent08]里有介绍. 这次教程建立在之前的去噪自编码器Denoising Autoencoders.如果你对自编码器没什么了解,建议你先了解一下. 栈式自编码器 通过把上一层去噪自编码器找到的隐藏输入(output code)当作下一层的输入,我们可以把去噪自编码器以栈的形式构成一个深度网络.这种无监督预训练的结构在一层里同时实现.每一层当作一个去噪自编码器,通过重构输入(上一层的输出)最小化损失.一旦前面 层
A Complete ActiveX Web Control Tutorial
? Introduction ActiveX is a Microsoft technology developed in the mid 90's, that allows for the creation of applet-like applications that can be downloaded and run within Microsoft's Web browser. This article is intended for Visual C++ developers who
PIC32MZ tutorial -- OC Interrupt
In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Compare without interrupt to generate PWM signal. I also tried the Output Compare interrupt. I selected OC to be PWM mode without fault pin (OCM = "110&qu
akka---Getting Started Tutorial (Java): First Chapter
原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome to the first tutorial on how to get started with Akka and Java. We assume that you already know what Akka and Java are and will now focus on the steps
NS3之路---Tutorial解读---Beginning&;&;Concept
鉴于前面已经有写过了ns3安装的部分,因此也就不重新介绍beginning部分了. 第四章介绍了几个网络中非常重要的概念.对于网络比较熟悉的基本上一看就能懂,理解这几个概念对于理解ns3十分重要.下面就是tutorial第四章的相关翻译工作. 相关概念介绍 首先,在进行ns3程序开发之前,我们有必要对相关概念进行介绍.它们是网络中最基本的对象,因此必须理解. 节点-Node 在因特网术语中,连接到网络的计算设备被称为主机或者终端.但是由于ns3是网络模拟器,而非因特网模拟器,因此我们更习惯使用图