2013-04-03 20:20:59 +02:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
2012-04-03 03:34:16 +02:00
|
|
|
// 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 <stdio.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include "include/cef_app.h"
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_command_line.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "include/cef_runnable.h"
|
2012-04-27 21:12:01 +02:00
|
|
|
#include "include/cef_web_plugin.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "cefclient/client_handler.h"
|
2012-04-12 22:21:50 +02:00
|
|
|
#include "cefclient/client_switches.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "cefclient/string_util.h"
|
|
|
|
#include "cefclient/util.h"
|
|
|
|
|
|
|
|
CefRefPtr<ClientHandler> g_handler;
|
|
|
|
CefRefPtr<CefCommandLine> g_command_line;
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> AppGetBrowser() {
|
|
|
|
if (!g_handler.get())
|
|
|
|
return NULL;
|
|
|
|
return g_handler->GetBrowser();
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:01:22 +02:00
|
|
|
ClientWindowHandle AppGetMainHwnd() {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!g_handler.get())
|
2014-05-22 23:01:22 +02:00
|
|
|
return kNullWindowHandle;
|
2012-04-03 03:34:16 +02:00
|
|
|
return g_handler->GetMainHwnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
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<CefCommandLine> AppGetCommandLine() {
|
|
|
|
return g_command_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the application settings based on command line arguments.
|
2013-02-07 20:59:40 +01:00
|
|
|
void AppGetSettings(CefSettings& settings) {
|
2012-04-03 03:34:16 +02:00
|
|
|
ASSERT(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);
|
2012-11-21 01:40:15 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void RunGetSourceTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
class Visitor : public CefStringVisitor {
|
|
|
|
public:
|
2012-04-12 22:21:50 +02:00
|
|
|
explicit Visitor(CefRefPtr<CefBrowser> browser) : browser_(browser) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual void Visit(const CefString& string) OVERRIDE {
|
|
|
|
std::string source = StringReplace(string, "<", "<");
|
|
|
|
source = StringReplace(source, ">", ">");
|
|
|
|
std::stringstream ss;
|
2013-09-03 23:58:02 +02:00
|
|
|
ss << "<html><body bgcolor=\"white\">Source:<pre>" << source <<
|
|
|
|
"</pre></body></html>";
|
2012-04-03 03:34:16 +02:00
|
|
|
browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/getsource");
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefBrowser> browser_;
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
browser->GetMainFrame()->GetSource(new Visitor(browser));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunGetTextTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
class Visitor : public CefStringVisitor {
|
|
|
|
public:
|
2012-04-12 22:21:50 +02:00
|
|
|
explicit Visitor(CefRefPtr<CefBrowser> browser) : browser_(browser) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual void Visit(const CefString& string) OVERRIDE {
|
|
|
|
std::string text = StringReplace(string, "<", "<");
|
|
|
|
text = StringReplace(text, ">", ">");
|
|
|
|
std::stringstream ss;
|
2013-09-03 23:58:02 +02:00
|
|
|
ss << "<html><body bgcolor=\"white\">Text:<pre>" << text <<
|
|
|
|
"</pre></body></html>";
|
2012-04-03 03:34:16 +02:00
|
|
|
browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/gettext");
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefBrowser> browser_;
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
browser->GetMainFrame()->GetText(new Visitor(browser));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunRequestTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
// Create a new request
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefRequest> request(CefRequest::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// 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.
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostDataElement> postDataElement(CefPostDataElement::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string data = "arg1=val1&arg2=val2";
|
|
|
|
postDataElement->SetToBytes(data.length(), data.c_str());
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData(CefPostData::Create());
|
2012-04-03 03:34:16 +02:00
|
|
|
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<CefBrowser> browser) {
|
|
|
|
browser->GetMainFrame()->ExecuteJavaScript(
|
|
|
|
"window.open('http://www.google.com');", "about:blank", 0);
|
|
|
|
}
|
|
|
|
|
2012-04-27 21:12:01 +02:00
|
|
|
void RunPluginInfoTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
class Visitor : public CefWebPluginInfoVisitor {
|
|
|
|
public:
|
|
|
|
explicit Visitor(CefRefPtr<CefBrowser> browser)
|
|
|
|
: browser_(browser) {
|
2013-09-03 23:58:02 +02:00
|
|
|
html_ = "<html><head><title>Plugin Info Test</title></head>"
|
|
|
|
"<body bgcolor=\"white\">"
|
2012-04-27 21:12:01 +02:00
|
|
|
"\n<b>Installed plugins:</b>";
|
|
|
|
}
|
|
|
|
~Visitor() {
|
|
|
|
html_ += "\n</body></html>";
|
|
|
|
|
|
|
|
// Load the html in the browser.
|
|
|
|
browser_->GetMainFrame()->LoadString(html_, "http://tests/plugin_info");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Visit(CefRefPtr<CefWebPluginInfo> info, int count, int total)
|
|
|
|
OVERRIDE {
|
|
|
|
html_ += "\n<br/><br/>Name: " + info->GetName().ToString() +
|
|
|
|
"\n<br/>Description: " + info->GetDescription().ToString() +
|
|
|
|
"\n<br/>Version: " + info->GetVersion().ToString() +
|
|
|
|
"\n<br/>Path: " + info->GetPath().ToString();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string html_;
|
|
|
|
CefRefPtr<CefBrowser> browser_;
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
CefVisitWebPluginInfo(new Visitor(browser));
|
|
|
|
}
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
void RunOtherTests(CefRefPtr<CefBrowser> browser) {
|
|
|
|
browser->GetMainFrame()->LoadURL("http://tests/other_tests");
|
2013-01-12 00:00:39 +01:00
|
|
|
}
|