// 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 #include #include CefRefPtr g_handler; CefRefPtr 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 frame) { // Retrieve the current page source and display. std::string source = frame->GetSource(); source = StringReplace(source, "<", "<"); source = StringReplace(source, ">", ">"); std::stringstream ss; ss << "Source:" << "
" << source
      << "
"; frame->LoadString(ss.str(), "http://tests/getsource"); } void RunGetSourceTest(CefRefPtr browser) { // Execute the GetSource() call on the UI thread. CefPostTask(TID_UI, NewCefRunnableFunction(&ExecuteGetSource, browser->GetMainFrame())); } static void ExecuteGetText(CefRefPtr frame) { std::string text = frame->GetText(); text = StringReplace(text, "<", "<"); text = StringReplace(text, ">", ">"); std::stringstream ss; ss << "Text:" << "
" << text
      << "
"; frame->LoadString(ss.str(), "http://tests/gettext"); } void RunGetTextTest(CefRefPtr browser) { // Execute the GetText() call on the UI thread. CefPostTask(TID_UI, NewCefRunnableFunction(&ExecuteGetText, browser->GetMainFrame())); } void RunRequestTest(CefRefPtr browser) { // Create a new request CefRefPtr 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 postDataElement( CefPostDataElement::CreatePostDataElement()); std::string data = "arg1=val1&arg2=val2"; postDataElement->SetToBytes(data.length(), data.c_str()); CefRefPtr 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 browser) { browser->GetMainFrame()->ExecuteJavaScript( "alert('JavaScript execute works!');", "about:blank", 0); } void RunPopupTest(CefRefPtr browser) { browser->GetMainFrame()->ExecuteJavaScript( "window.open('http://www.google.com');", "about:blank", 0); } void RunLocalStorageTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL("http://tests/localstorage"); } void RunAccelerated2DCanvasTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL( "http://mudcu.be/labs/JS1k/BreathingGalaxies.html"); } void RunAcceleratedLayersTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL( "http://webkit.org/blog-files/3d-transforms/poster-circle.html"); } void RunWebGLTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL( "http://webglsamples.googlecode.com/hg/field/field.html"); } void RunHTML5VideoTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL( "http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True"); } void RunXMLHTTPRequestTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL("http://tests/xmlhttprequest"); } void RunWebURLRequestTest(CefRefPtr browser) { class RequestClient : public CefWebURLRequestClient { public: RequestClient(CefRefPtr browser) : browser_(browser) {} virtual void OnStateChange(CefRefPtr requester, RequestState state) { REQUIRE_UI_THREAD(); if (state == WUR_STATE_DONE) { buffer_ = StringReplace(buffer_, "<", "<"); buffer_ = StringReplace(buffer_, ">", ">"); std::stringstream ss; ss << "Source:
" << buffer_ << "
"; browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/weburlrequest"); } } virtual void OnRedirect(CefRefPtr requester, CefRefPtr request, CefRefPtr response) { REQUIRE_UI_THREAD(); } virtual void OnHeadersReceived(CefRefPtr requester, CefRefPtr response) { REQUIRE_UI_THREAD(); } virtual void OnProgress(CefRefPtr requester, uint64 bytesSent, uint64 totalBytesToBeSent) { REQUIRE_UI_THREAD(); } virtual void OnData(CefRefPtr requester, const void* data, int dataLength) { REQUIRE_UI_THREAD(); buffer_.append(static_cast(data), dataLength); } virtual void OnError(CefRefPtr 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 browser_; std::string buffer_; IMPLEMENT_REFCOUNTING(CefWebURLRequestClient); }; CefRefPtr request(CefRequest::CreateRequest()); request->SetURL("http://www.google.com"); CefRefPtr client(new RequestClient(browser)); CefRefPtr requester( CefWebURLRequest::CreateWebURLRequest(request, client)); } void RunDOMAccessTest(CefRefPtr browser) { class Listener : public CefDOMEventListener { public: Listener() {} virtual void HandleEvent(CefRefPtr event) { CefRefPtr document = event->GetDocument(); ASSERT(document.get()); std::stringstream ss; CefRefPtr 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 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 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 desc = document->GetElementById("description"); ASSERT(desc.get()); CefRefPtr 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 document) { // Register an click listener for the button. CefRefPtr 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 client = browser->GetClient(); static_cast(client.get())->AddDOMVisitor( "http://tests/domaccess", new Visitor()); browser->GetMainFrame()->LoadURL("http://tests/domaccess"); } void RunDragDropTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL("http://html5demos.com/drag"); } void RunModalDialogTest(CefRefPtr browser) { browser->GetMainFrame()->LoadURL("http://tests/modalmain"); }