void OverloadApp::registerFunctions(CefRefPtr<CefListValue> functionNames) { if (_browser != nullptr) { CefRefPtr<CefV8Context> currentContext = _browser->GetMainFrame()->GetV8Context(); currentContext->Enter(); CefRefPtr<CefV8Value> object = currentContext->GetGlobal(); for (std::size_t i = 0; i < functionNames->GetSize(); i++) { std::string functionName = functionNames->GetString(i).ToString(); object->SetValue(functionName, CefV8Value::CreateFunction(functionName, _v8Handler.get()), V8_PROPERTY_ATTRIBUTE_NONE); _functionNames.push_back(functionName); } currentContext->Exit(); } else { for (std::size_t i = 0; i < functionNames->GetSize(); i++) { _functionNames.push_back(functionNames->GetString(i).ToString()); } } }
// synchronously send a string from Node to browser, then return string result from browser to Node Handle<Value> Window::SendSync(const Arguments& args) { HandleScope scope; NativeWindow *window = ObjectWrap::Unwrap<NativeWindow> (args.This()); if (window->GetBrowser()) { // find browser's v8 context CefRefPtr<CefV8Context> context = window->GetBrowser()->GetMainFrame()->GetV8Context(); // ensure it's usable and enter if (context.get() && context->Enter()) { // try to get "appjs.onmessage" function CefRefPtr<CefV8Value> appjsObject = context->GetGlobal()->GetValue("appjs"); CefRefPtr<CefV8Value> callback = appjsObject->GetValue("onmessage"); if (callback.get()) { // convert Node V8 string to Cef V8 string CefV8ValueList argsOut; argsOut.push_back(CefV8Value::CreateString(V8StringToChar(args[0]->ToString()))); // execute window.appjs fuction, passing in the string, // then convert the return value from a CefValue to a Node V8 string Handle<String> ret = CefStringToV8(callback->ExecuteFunction(appjsObject, argsOut)->GetStringValue()); // exit browser v8 context, return string result to Node caller context->Exit(); return scope.Close(ret); } } } // likely error condition return scope.Close(Undefined()); }
void IPC_Container::ReadContainerObjectsAndWriteToIPCMsg( CefRefPtr<CefV8Context> context, std::string container_array_variable, std::vector<int> amounts, int64 frameID, CefRefPtr<CefProcessMessage> msg ) { CefRefPtr<CefListValue> args = msg->GetArgumentList(); int index = 0; // Write frameID at position #0 args->SetDouble(index++, (double) frameID); // Write different amount of different node types at position #1 args->SetInt(index++, amounts.size()); // Write amount of each node type at first positions for (int i = 0; i < (int)amounts.size(); i++) { args->SetInt(index++, amounts[i]); } if (context->Enter()) { int all_objects_done = 0; for (int i = 0; i < (int)amounts.size(); i++) { // Read each container object for (int j = 0; j < amounts[i]; j++) { // Read every attribute and get their value for (int k = 0; k < (int)_scheme.size(); k++) { // DLOG(INFO) << "Reading object #" << j << ", Attribute #" << k << "), writing to IPC args index #" << index; // Get each attribute as V8Value CefRefPtr<CefV8Value> attribute = GetAttributesV8Value( // TODO: There HAS TO BE a bug concerning the object's position in the array context, container_array_variable, j+all_objects_done, _scheme[k] ); // Write each attribute to IPC message SetIPCArgumentsValue( args, index, attribute, _scheme[k] ); } } all_objects_done += amounts[i]; } context->Exit(); } }
bool ClientApp::OnProcessMessageReceived( CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) { ASSERT(source_process == PID_BROWSER); bool handled = false; RenderDelegateSet::iterator it = render_delegates_.begin(); for (; it != render_delegates_.end() && !handled; ++it) { handled = (*it)->OnProcessMessageReceived(this, browser, source_process, message); } if (handled) return true; // Execute the registered JavaScript callback if any. if (!callback_map_.empty()) { CefString message_name = message->GetName(); CallbackMap::const_iterator it = callback_map_.find( std::make_pair(message_name.ToString(), browser->GetIdentifier())); if (it != callback_map_.end()) { // Keep a local reference to the objects. The callback may remove itself // from the callback map. CefRefPtr<CefV8Context> context = it->second.first; CefRefPtr<CefV8Value> callback = it->second.second; // Enter the context. context->Enter(); CefV8ValueList arguments; // First argument is the message name. arguments.push_back(CefV8Value::CreateString(message_name)); // Second argument is the list of message arguments. CefRefPtr<CefListValue> list = message->GetArgumentList(); CefRefPtr<CefV8Value> args = CefV8Value::CreateArray(static_cast<int>(list->GetSize())); SetList(list, args); arguments.push_back(args); // Execute the callback. CefRefPtr<CefV8Value> retval = callback->ExecuteFunction(NULL, arguments); if (retval.get()) { if (retval->IsBool()) handled = retval->GetBoolValue(); } // Exit the context. context->Exit(); } } return handled; }
void ClientHandler::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) { REQUIRE_UI_THREAD(); if (!browser->IsPopup()) { context->Enter(); CefRefPtr<CefV8Value> appjsObj = CefV8Value::CreateObject(NULL); CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("send", new AppjsSyncHandler(browser)); context->GetGlobal()->SetValue("appjs", appjsObj, V8_PROPERTY_ATTRIBUTE_NONE); appjsObj->SetValue("send", func, V8_PROPERTY_ATTRIBUTE_NONE); context->Exit(); } }
void V8_Container::AddContainerArray(CefRefPtr<CefV8Context> context, std::string js_var_name, int arraySize) { if (context->Enter()) { CefRefPtr<CefV8Value> array = CefV8Value::CreateArray(arraySize); for (int i = 0; i < arraySize; i++) { array->SetValue(i, CreateContainerV8Object()); } // Add empty array of container objects to context's global object context->GetGlobal()->SetValue(js_var_name, array, V8_PROPERTY_ATTRIBUTE_NONE); context->Exit(); } }
bool ClientApp::OnProcessMessageReceived( CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) { ASSERT(source_process == PID_BROWSER); bool handled = false; // Execute delegate callbacks. RenderDelegateSet::iterator it = render_delegates_.begin(); for (; it != render_delegates_.end() && !handled; ++it) { handled = (*it)->OnProcessMessageReceived(this, browser, source_process, message); } if (!handled) { if (message->GetName() == "invokeCallback") { // This is called by the appshell extension handler to invoke the asynchronous // callback function CefRefPtr<CefListValue> messageArgs = message->GetArgumentList(); int32 callbackId = messageArgs->GetInt(0); CefRefPtr<CefV8Context> context = callback_map_[callbackId].first; CefRefPtr<CefV8Value> callbackFunction = callback_map_[callbackId].second; CefV8ValueList arguments; context->Enter(); // Sanity check to make sure the context is still attched to a browser. // Async callbacks could be initiated after a browser instance has been deleted, // which can lead to bad things. If the browser instance has been deleted, don't // invoke this callback. if (context->GetBrowser()) { for (size_t i = 1; i < messageArgs->GetSize(); i++) { arguments.push_back(ListValueToV8Value(messageArgs, i)); } callbackFunction->ExecuteFunction(NULL, arguments); } context->Exit(); callback_map_.erase(callbackId); } else if (message->GetName() == "executeCommand") { // This is called by the browser process to execute a command via JavaScript // // The first argument is the command name. This is required. // The second argument is a message id. This is optional. If set, a response // message will be sent back to the browser process. CefRefPtr<CefListValue> messageArgs = message->GetArgumentList(); CefString commandName = messageArgs->GetString(0); int messageId = messageArgs->GetSize() > 1 ? messageArgs->GetInt(1) : -1; bool handled = false; StContextScope ctx(browser->GetMainFrame()->GetV8Context()); CefRefPtr<CefV8Value> global = ctx.GetContext()->GetGlobal(); if (global->HasValue("brackets")) { CefRefPtr<CefV8Value> brackets = global->GetValue("brackets"); if (brackets->HasValue("shellAPI")) { CefRefPtr<CefV8Value> shellAPI = brackets->GetValue("shellAPI"); if (shellAPI->HasValue("executeCommand")) { CefRefPtr<CefV8Value> executeCommand = shellAPI->GetValue("executeCommand"); if (executeCommand->IsFunction()) { CefRefPtr<CefV8Value> retval; CefV8ValueList args; args.push_back(CefV8Value::CreateString(commandName)); retval = executeCommand->ExecuteFunction(global, args); if (retval) { handled = retval->GetBoolValue(); } } } } } // Return a message saying whether or not the command was handled if (messageId != -1) { CefRefPtr<CefProcessMessage> result = CefProcessMessage::Create("executeCommandCallback"); result->GetArgumentList()->SetInt(0, messageId); result->GetArgumentList()->SetBool(1, handled); browser->SendProcessMessage(PID_BROWSER, result); } } } return handled; }
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { m_cefApp = new ClientApp(); if (!(m_cefApp->Init(hInstance) < 0)) return FALSE; CWndShadow::Initialize(hInstance); wstring strFileName = ZYM::CPath::GetAppPath() + _T("ImageOleCtrl.dll"); BOOL bRet = DllRegisterServer(strFileName.c_str()); // 注册COM组件 if (!bRet) { ::MessageBox(NULL, _T("COM组件注册失败,应用程序无法完成初始化操作!"), _T("提示"), MB_OK); return 0; } HRESULT hr = ::OleInitialize(NULL); if (FAILED(hr)) return 0; GdiplusStartup(&g_gdiplusToken, &g_gdiplusStartupInput, NULL); // 初始化GDI+ HMODULE hRichEditDll = ::LoadLibrary(_T("Riched20.dll")); // 加载RichEdit控件DLL CPaintManagerUI::SetInstance(hInstance); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("SkinRes")); HRESULT Hr = ::CoInitialize(NULL); if (FAILED(Hr)) return 0; CLoginWnd* pLoginFrame = new CLoginWnd(); CMainFrame *pWndFrame = new CMainFrame(pLoginFrame->m_manager); pLoginFrame->Create(NULL, _T(""), UI_WNDSTYLE_DIALOG, 0, 0, 0, 0, 0, NULL); pLoginFrame->CenterWindow(); pWndFrame->SetHandler(); int result = pLoginFrame->ShowModal(); if (result == 1) { pWndFrame->Create(NULL, _T(""), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE | WS_EX_ACCEPTFILES); pWndFrame->CenterWindow(); pWndFrame->ShowModal(); } else { //登录失败 } CPaintManagerUI::MessageLoop(); ::CoUninitialize(); if (hRichEditDll != NULL) // 卸载RichEdit控件DLL ::FreeLibrary(hRichEditDll); Gdiplus::GdiplusShutdown(g_gdiplusToken); // 反初始化GDI+ ::OleUninitialize(); m_cefApp->Exit(); m_cefApp = NULL; if (_globalSetting.m_logoutState == 1) { CDuiString path = GetCurrentPathW(); path += L"\\YunKa.exe"; ShellExecute(NULL, L"open", path.GetData(), NULL, NULL, SW_SHOWNOACTIVATE); } return 0; }