// Copyright (c) 2010 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 "include/cef.h"
#include "include/cef_runnable.h"
#include "include/cef_wrapper.h"
#include "cefclient.h"
#include "client_handler.h"
#include "binding_test.h"
#include "string_util.h"
#include "util.h"
#include <sstream>
#include <stdio.h>
#include <string> CefRefPtr<ClientHandler> g_handler; CefRefPtr<CefBrowser> AppGetBrowser()
{
if(!g_handler.get())
return NULL;
return g_handler->GetBrowser();
} CefWindowHandle AppGetMainHwnd()
{
if(!g_handler.get())
return NULL;
return g_handler->GetMainHwnd();
} static void ExecuteGetSource(CefRefPtr<CefFrame> frame)
{
// Retrieve the current page source and display.
std::string source = frame->GetSource();
source = StringReplace(source, "<", "&lt;");
source = StringReplace(source, ">", "&gt;");
std::stringstream ss;
ss << "<html><body>Source:" << "<pre>" << source
<< "</pre></body></html>";
frame->LoadString(ss.str(), "http://tests/getsource");
} void RunGetSourceTest(CefRefPtr<CefBrowser> browser)
{
// Execute the GetSource() call on the UI thread.
CefPostTask(TID_UI,
NewCefRunnableFunction(&ExecuteGetSource, browser->GetMainFrame()));
} static void ExecuteGetText(CefRefPtr<CefFrame> frame)
{
std::string text = frame->GetText();
text = StringReplace(text, "<", "&lt;");
text = StringReplace(text, ">", "&gt;");
std::stringstream ss;
ss << "<html><body>Text:" << "<pre>" << text
<< "</pre></body></html>";
frame->LoadString(ss.str(), "http://tests/gettext");
} void RunGetTextTest(CefRefPtr<CefBrowser> browser)
{
// Execute the GetText() call on the UI thread.
CefPostTask(TID_UI,
NewCefRunnableFunction(&ExecuteGetText, browser->GetMainFrame()));
} void RunRequestTest(CefRefPtr<CefBrowser> browser)
{
// Create a new request
CefRefPtr<CefRequest> request(CefRequest::CreateRequest()); // Set the request URL
request->SetURL("http://tests/request"); // Add post data to the request. The correct method and content-
// type headers will be set by CEF.
CefRefPtr<CefPostDataElement> postDataElement(
CefPostDataElement::CreatePostDataElement());
std::string data = "arg1=val1&arg2=val2";
postDataElement->SetToBytes(data.length(), data.c_str());
CefRefPtr<CefPostData> postData(CefPostData::CreatePostData());
postData->AddElement(postDataElement);
request->SetPostData(postData); // Add a custom header
CefRequest::HeaderMap headerMap;
headerMap.insert(
std::make_pair("X-My-Header", "My Header Value"));
request->SetHeaderMap(headerMap); // Load the request
browser->GetMainFrame()->LoadRequest(request);
} void RunJavaScriptExecuteTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->ExecuteJavaScript(
"alert('JavaScript execute works!');", "about:blank", 0);
} void RunPopupTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->ExecuteJavaScript(
"window.open('http://www.google.com');", "about:blank", 0);
} void RunLocalStorageTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL("http://tests/localstorage");
} void RunAccelerated2DCanvasTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL(
"http://mudcu.be/labs/JS1k/BreathingGalaxies.html");
} void RunAcceleratedLayersTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL(
"http://webkit.org/blog-files/3d-transforms/poster-circle.html");
} void RunWebGLTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL(
"http://webglsamples.googlecode.com/hg/field/field.html");
} void RunHTML5VideoTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL(
"http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True");
} void RunXMLHTTPRequestTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL("http://tests/xmlhttprequest");
} void RunWebURLRequestTest(CefRefPtr<CefBrowser> browser)
{
class RequestClient : public CefWebURLRequestClient
{
public:
RequestClient(CefRefPtr<CefBrowser> browser) : browser_(browser) {} virtual void OnStateChange(CefRefPtr<CefWebURLRequest> requester,
RequestState state)
{
REQUIRE_UI_THREAD();
if (state == WUR_STATE_DONE) {
buffer_ = StringReplace(buffer_, "<", "&lt;");
buffer_ = StringReplace(buffer_, ">", "&gt;");
std::stringstream ss;
ss << "<html><body>Source:<pre>" << buffer_ << "</pre></body></html>"; browser_->GetMainFrame()->LoadString(ss.str(),
"http://tests/weburlrequest");
}
} virtual void OnRedirect(CefRefPtr<CefWebURLRequest> requester,
CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response)
{
REQUIRE_UI_THREAD();
} virtual void OnHeadersReceived(CefRefPtr<CefWebURLRequest> requester,
CefRefPtr<CefResponse> response)
{
REQUIRE_UI_THREAD();
} virtual void OnProgress(CefRefPtr<CefWebURLRequest> requester,
uint64 bytesSent, uint64 totalBytesToBeSent)
{
REQUIRE_UI_THREAD();
} virtual void OnData(CefRefPtr<CefWebURLRequest> requester,
const void* data, int dataLength)
{
REQUIRE_UI_THREAD();
buffer_.append(static_cast<const char*>(data), dataLength);
} virtual void OnError(CefRefPtr<CefWebURLRequest> requester,
ErrorCode errorCode)
{
REQUIRE_UI_THREAD();
std::stringstream ss;
ss << "Load failed with error code " << errorCode;
browser_->GetMainFrame()->LoadString(ss.str(),
"http://tests/weburlrequest");
} protected:
CefRefPtr<CefBrowser> browser_;
std::string buffer_; IMPLEMENT_REFCOUNTING(CefWebURLRequestClient);
}; CefRefPtr<CefRequest> request(CefRequest::CreateRequest());
request->SetURL("http://www.google.com"); CefRefPtr<CefWebURLRequestClient> client(new RequestClient(browser));
CefRefPtr<CefWebURLRequest> requester(
CefWebURLRequest::CreateWebURLRequest(request, client));
} void RunDOMAccessTest(CefRefPtr<CefBrowser> browser)
{
class Listener : public CefDOMEventListener
{
public:
Listener() {}
virtual void HandleEvent(CefRefPtr<CefDOMEvent> event)
{
CefRefPtr<CefDOMDocument> document = event->GetDocument();
ASSERT(document.get()); std::stringstream ss; CefRefPtr<CefDOMNode> button = event->GetTarget();
ASSERT(button.get());
std::string buttonValue = button->GetElementAttribute("value");
ss << "You clicked the " << buttonValue.c_str() << " button. "; if (document->HasSelection()) {
std::string startName, endName; // Determine the start name by first trying to locate the "id" attribute
// and then defaulting to the tag name.
{
CefRefPtr<CefDOMNode> node = document->GetSelectionStartNode();
if (!node->IsElement())
node = node->GetParent();
if (node->IsElement() && node->HasElementAttribute("id"))
startName = node->GetElementAttribute("id");
else
startName = node->GetName();
} // Determine the end name by first trying to locate the "id" attribute
// and then defaulting to the tag name.
{
CefRefPtr<CefDOMNode> node = document->GetSelectionEndNode();
if (!node->IsElement())
node = node->GetParent();
if (node->IsElement() && node->HasElementAttribute("id"))
endName = node->GetElementAttribute("id");
else
endName = node->GetName();
} ss << "The selection is from " <<
startName.c_str() << ":" << document->GetSelectionStartOffset() <<
" to " <<
endName.c_str() << ":" << document->GetSelectionEndOffset();
} else {
ss << "Nothing is selected.";
} // Update the description.
CefRefPtr<CefDOMNode> desc = document->GetElementById("description");
ASSERT(desc.get());
CefRefPtr<CefDOMNode> text = desc->GetFirstChild();
ASSERT(text.get());
ASSERT(text->IsText());
text->SetValue(ss.str());
} IMPLEMENT_REFCOUNTING(Listener);
}; class Visitor : public CefDOMVisitor
{
public:
Visitor() {}
virtual void Visit(CefRefPtr<CefDOMDocument> document)
{
// Register an click listener for the button.
CefRefPtr<CefDOMNode> button = document->GetElementById("button");
ASSERT(button.get());
button->AddEventListener("click", new Listener(), false);
} IMPLEMENT_REFCOUNTING(Visitor);
}; // The DOM visitor will be called after the path is loaded.
CefRefPtr<CefClient> client = browser->GetClient();
static_cast<ClientHandler*>(client.get())->AddDOMVisitor(
"http://tests/domaccess", new Visitor()); browser->GetMainFrame()->LoadURL("http://tests/domaccess");
} void RunDragDropTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL("http://html5demos.com/drag");
} void RunModalDialogTest(CefRefPtr<CefBrowser> browser)
{
browser->GetMainFrame()->LoadURL("http://tests/modalmain");
}

cef研究的更多相关文章

  1. qt添加cef库嵌入web [转]

    qt cef嵌入web 原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium ...

  2. 使用C#在CEF中拦截并响应请求

    一.前言 忙里偷闲,研究了一下如何在CEF中拦截请求,并作出响应.这个功能对某些需要修改服务器响应的需求来说必不可少,可以直接读取本地文件作为响应内容. C#的CEF封装项目有很多,我使用的是Chro ...

  3. CEF使用的几个注意点

    CEF为chrome浏览器的切入其他浏览器中的轻量级框架. 开发的客户端的时候,这是作为界面显示的首先,可以增强客户的易变性,可塑性. 在开发的过程中(侧重于C,C++解决),遇到的几个问题,以及自己 ...

  4. 从今天起,记录CEF使用开发心得经验

    已经使用CEF来呈现桌面程序界面大半年了,从来没有写过相关博文.发现网上的中文资料甚至英文已经无法满足我的开发需求,不少问题只得自己探索.在此先谢过网络上各位CEF使用开发博文的贡献者,没有你们我也难 ...

  5. Chromium与CEF的多进程模型及相关參数

    CEF基于Chromium,也是多进程模型.关于进程模型.參考这里:https://www.chromium.org/developers/design-documents/process-model ...

  6. Duilib嵌入CEF出现窗口显示不正常

    参考资料:https://www.aliyun.com/zixun/wenji/1247250.html 转载:https://www.cnblogs.com/gongxijun/p/4857977. ...

  7. CEF 中的那些坑

    CEF (Chromium Embedded Framework) 的大名也听说很久了,最近因为客户的需求,简单地研究了一下.结果遇到了一个接一个的坑,且慢慢道来.比之前用QtWebkit的坑还要多和 ...

  8. CEF加载FLASH插件时弹出CMD命令行窗口的问题

    这个是flash插件的一个bug,CEF(chromium系列浏览器)关闭sandbox第一次加载flash插件就会跳出这样的一个提示,在Google官方也看到了chromium的issue: 解决方 ...

  9. Duilib嵌入CEF禁止浏览器响应拖拽事件

    转载:http://blog.csdn.net/liuyan20092009/article/details/53819473 转载:https://blog.csdn.net/u012778714( ...

随机推荐

  1. <转载> 22种代码味道(Martin Fowler与Kent Beck) http://blog.csdn.net/lovelion/article/details/9301691

    Martin Fowler在Refactoring: Improving the Design of Existing Code(中译名:<重构——改善既有代码的设计>)一书中与Kent ...

  2. 2. instr用法

    跟oracle中的instr用法一样: SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5 ...

  3. java reflect反射获取方法变量参数

    类的成员包含变量(Field),方法(Method),构造器(Constructor) 类定义 package Reflect; public class MyTest { public int a; ...

  4. ubuntu编译安装nginx

    下载nginx源码, ./configure --prefix=/usr/local/nginx/1.8 --with-http_ssl_module --with-http_dav_module - ...

  5. 机器学习进阶-案例实战-图像全景拼接-书籍SIFT特征点连接 1.cv2.drawMatches(对两个图像的关键点进行连线操作)

    1.cv2.drawMatches(imageA, kpsA, imageB, kpsB, matches[:10], None, flags=2)  # 对两个图像关键点进行连线操作 参数说明:im ...

  6. SpringBoot配置定时任务的两种方式

    一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  7. Servlet基本_初期化パラメータ、外部ファイル

    1.サーブレットの初期化パラメータサーブレットの初期化パラメータを利用するには.必ずweb.xmlにおいてサーブレットマッピングを指定する必要がある.(Tomactのinvokerサーブレットは利用で ...

  8. 尚硅谷springboot学习2-微服务

    2014年,martin flowler发表关于微服务的博客 微服务是一种架构风格:一个应用应该是一组小型服务:可以通过HTTP的方式进行互通: 单体应用:ALL IN ONE 微服务:每一个功能元素 ...

  9. shell编程:基本语法

    要掌握一门语言,就要先掌握它的语法.如同C语言一般,shell也有自己的语法. 变量 按照惯例,Shell变量通常由字母加下划线开头,由任意长度的字母.数字.下划线组成.有两种类型的Shell变量: ...

  10. 【转】UGUI研究院之缓存策略让UI打开更快(三十)

    UGUI研究院之缓存策略让UI打开更快(三十) [投稿]Unity3D游戏优化之头顶UI 注意里面提到了:SuperTextMesh:能渲染动态文字,富文本支持图文混排,缺点是支持atlas但很弱,资 ...