1 #include "ChromiumBrowser.h"
2 #include "ClientApp.h"
3 
4 
ClientApp()5 ClientApp::ClientApp()
6 {
7 }
8 
9 
OnBeforeCommandLineProcessing(const CefString & process_type,CefRefPtr<CefCommandLine> command_line)10 void ClientApp::OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line)
11 {
12 	//Extra command line switches
13 	command_line->AppendSwitch("enable-media-stream");
14 	command_line->AppendSwitch("no-sandbox");
15 	command_line->AppendSwitch("disable-pinch");
16 
17 #ifdef PLATFORM_LINUX
18 	const char flash_name[] = "libpepflashplayer.so";
19 #elif defined(PLATFORM_WIN32)
20 	const char flash_name[] = "pepflashplayer.dll";
21 #endif
22 
23 	Upp::String fp = Upp::GetExeDirFile(flash_name);
24 	if (Upp::FileExists(fp)){
25 		command_line->AppendSwitchWithValue("ppapi-flash-path", ~fp);
26 		Upp::String fv = ParseJSON(LoadFile(Upp::GetExeDirFile("manifest.json")))["version"];
27 		command_line->AppendSwitchWithValue("ppapi-flash-version", fv.IsEmpty() ? "30.0.0.154" : ~fv);
28 	}
29 }
30 
31 
OnContextCreated(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)32 void ClientApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
33 {
34 	//Register our JS functions
35 	CefRefPtr<CefV8Value> object = context->GetGlobal();
36 	for(int f = 0; Upp::ChromiumBrowser::JSFunctions[f] != nullptr; f++){
37 		CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction(Upp::ChromiumBrowser::JSFunctions[f], this);
38 		object->SetValue(Upp::ChromiumBrowser::JSFunctions[f], func, V8_PROPERTY_ATTRIBUTE_NONE);
39 	}
40 }
41 
42 
OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefDOMNode> node)43 void ClientApp::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefDOMNode> node)
44 {
45 	bool is_editable = (node.get() && node->IsEditable());
46 	browser->SendProcessMessage(PID_BROWSER, CefProcessMessage::Create(is_editable ? "show_keyboard" : "hide_keyboard"));
47 }
48 
49 
Execute(const CefString & name,CefRefPtr<CefV8Value> object,const CefV8ValueList & arguments,CefRefPtr<CefV8Value> & retval,CefString & exception)50 bool ClientApp::Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception)
51 {
52 	CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(name);
53 	CefRefPtr<CefListValue> par = message->GetArgumentList();
54 	V8ValueListToCefListValue(arguments, par);
55 	CefRefPtr<CefBrowser> browser = CefV8Context::GetCurrentContext()->GetBrowser();
56 	if (browser) browser->SendProcessMessage(PID_BROWSER, message);
57 	return true;
58 }
59 
60 
V8ValueListToCefListValue(const CefV8ValueList & src,CefRefPtr<CefListValue> & dst)61 void ClientApp::V8ValueListToCefListValue(const CefV8ValueList& src, CefRefPtr<CefListValue> & dst)
62 {
63 	for (const auto & s: src){
64 		if (!s->IsValid())						{ dst->SetString(dst->GetSize(),"Invalid V8Value");		continue;}
65 		if (s->IsUndefined() || s->IsNull())	{ dst->SetNull(dst->GetSize());							continue;}
66 		if (s->IsBool())						{ dst->SetBool(dst->GetSize(),	s->GetBoolValue());		continue;}
67 		if (s->IsInt())							{ dst->SetInt(dst->GetSize(),	s->GetIntValue());		continue;}
68 		if (s->IsDouble())						{ dst->SetDouble(dst->GetSize(),s->GetDoubleValue());	continue;}
69 		if (s->IsString())						{ dst->SetString(dst->GetSize(),s->GetStringValue());	continue;}
70 
71 		dst->SetString(dst->GetSize(), "Unimplemented V8Value conversion");
72     }
73 }
74