// Copyright (c) 2013 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 "cefclient/cefclient.h" #include #include #include #include #include "include/cef_app.h" #include "include/cef_browser.h" #include "include/cef_command_line.h" #include "include/cef_frame.h" #include "include/cef_web_plugin.h" #include "include/wrapper/cef_helpers.h" #include "cefclient/client_handler.h" #include "cefclient/client_switches.h" #include "cefclient/string_util.h" CefRefPtr g_handler; namespace { CefRefPtr g_command_line; int g_offscreen_state = 0; } // namespace CefRefPtr AppGetBrowser() { if (!g_handler.get()) return NULL; return g_handler->GetBrowser(); } ClientWindowHandle AppGetMainWindowHandle() { if (!g_handler.get()) return kNullWindowHandle; return g_handler->GetMainWindowHandle(); } void AppInitCommandLine(int argc, const char* const* argv) { g_command_line = CefCommandLine::CreateCommandLine(); #if defined(OS_WIN) g_command_line->InitFromString(::GetCommandLineW()); #else g_command_line->InitFromArgv(argc, argv); #endif } // Returns the application command line object. CefRefPtr AppGetCommandLine() { return g_command_line; } // Returns the application settings based on command line arguments. void AppGetSettings(CefSettings& settings) { DCHECK(g_command_line.get()); if (!g_command_line.get()) return; CefString str; #if defined(OS_WIN) settings.multi_threaded_message_loop = g_command_line->HasSwitch(cefclient::kMultiThreadedMessageLoop); #endif CefString(&settings.cache_path) = g_command_line->GetSwitchValue(cefclient::kCachePath); if (g_command_line->HasSwitch(cefclient::kOffScreenRenderingEnabled)) settings.windowless_rendering_enabled = true; } void AppGetBrowserSettings(CefBrowserSettings& settings) { DCHECK(g_command_line.get()); if (!g_command_line.get()) return; if (g_command_line->HasSwitch(cefclient::kOffScreenFrameRate)) { settings.windowless_frame_rate = atoi(g_command_line-> GetSwitchValue(cefclient::kOffScreenFrameRate).ToString().c_str()); } } bool AppIsOffScreenRenderingEnabled() { if (g_offscreen_state == 0) { // Store the value so it isn't queried multiple times. DCHECK(g_command_line.get()); g_offscreen_state = g_command_line->HasSwitch(cefclient::kOffScreenRenderingEnabled) ? 1 : 2; } return (g_offscreen_state == 1); } void RunGetSourceTest(CefRefPtr browser) { class Visitor : public CefStringVisitor { public: explicit Visitor(CefRefPtr browser) : browser_(browser) {} virtual void Visit(const CefString& string) OVERRIDE { std::string source = StringReplace(string, "<", "<"); source = StringReplace(source, ">", ">"); std::stringstream ss; ss << "Source:
" << source <<
            "
"; browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/getsource"); } private: CefRefPtr browser_; IMPLEMENT_REFCOUNTING(Visitor); }; browser->GetMainFrame()->GetSource(new Visitor(browser)); } void RunGetTextTest(CefRefPtr browser) { class Visitor : public CefStringVisitor { public: explicit Visitor(CefRefPtr browser) : browser_(browser) {} virtual void Visit(const CefString& string) OVERRIDE { std::string text = StringReplace(string, "<", "<"); text = StringReplace(text, ">", ">"); std::stringstream ss; ss << "Text:
" << text <<
            "
"; browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/gettext"); } private: CefRefPtr browser_; IMPLEMENT_REFCOUNTING(Visitor); }; browser->GetMainFrame()->GetText(new Visitor(browser)); } void RunRequestTest(CefRefPtr browser) { // Create a new request CefRefPtr request(CefRequest::Create()); // 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::Create()); std::string data = "arg1=val1&arg2=val2"; postDataElement->SetToBytes(data.length(), data.c_str()); CefRefPtr postData(CefPostData::Create()); 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 RunPopupTest(CefRefPtr browser) { browser->GetMainFrame()->ExecuteJavaScript( "window.open('http://www.google.com');", "about:blank", 0); } void RunPluginInfoTest(CefRefPtr browser) { class Visitor : public CefWebPluginInfoVisitor { public: explicit Visitor(CefRefPtr browser) : browser_(browser) { html_ = "Plugin Info Test" "" "\nInstalled plugins:"; } ~Visitor() { html_ += "\n"; // Load the html in the browser. browser_->GetMainFrame()->LoadString(html_, "http://tests/plugin_info"); } virtual bool Visit(CefRefPtr info, int count, int total) OVERRIDE { html_ += "\n

Name: " + info->GetName().ToString() + "\n
Description: " + info->GetDescription().ToString() + "\n
Version: " + info->GetVersion().ToString() + "\n
Path: " + info->GetPath().ToString(); return true; } private: std::string html_; CefRefPtr browser_; IMPLEMENT_REFCOUNTING(Visitor); }; CefVisitWebPluginInfo(new Visitor(browser)); } void RunOtherTests(CefRefPtr browser) { browser->GetMainFrame()->LoadURL("http://tests/other_tests"); }