|
| 1 | +// Copyright (c) 2012-2014 The PHP Desktop authors. All rights reserved. |
| 2 | +// License: New BSD License. |
| 3 | +// Website: http://code.google.com/p/phpdesktop/ |
| 4 | + |
| 5 | +#pragma comment(lib, "libcef.lib") |
| 6 | +#pragma comment(lib, "libcef_dll_wrapper.lib") |
| 7 | + |
| 8 | +#include "app.h" |
| 9 | +#include <string> |
| 10 | +#include "util.h" |
| 11 | + |
| 12 | +#include "include/cef_browser.h" |
| 13 | +#include "include/cef_command_line.h" |
| 14 | +#include "app.h" |
| 15 | +#include "../log.h" |
| 16 | +#include "client_handler.h" |
| 17 | +#include "../settings.h" |
| 18 | +#include "javascript_api.h" |
| 19 | + |
| 20 | +// ---------------------------------------------------------------------------- |
| 21 | +// CefRenderProcessHandler methods |
| 22 | +// ---------------------------------------------------------------------------- |
| 23 | + |
| 24 | +/// |
| 25 | +// Called when a new message is received from a different process. Return true |
| 26 | +// if the message was handled or false otherwise. Do not keep a reference to |
| 27 | +// or attempt to access the message outside of this callback. |
| 28 | +/// |
| 29 | +/*--cef()--*/ |
| 30 | +bool App::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, |
| 31 | + CefProcessId source_process, |
| 32 | + CefRefPtr<CefProcessMessage> message) { |
| 33 | + LOG_DEBUG << "renderer[" << browser->GetIdentifier() << "] " |
| 34 | + << "OnProcessMessageReceived: " << message->GetName().ToString(); |
| 35 | + if (message->GetName() == "SetIsFullscreen") { |
| 36 | + CefRefPtr<CefListValue> args = message->GetArgumentList(); |
| 37 | + bool isFullscreen = args->GetBool(0); |
| 38 | + CefRefPtr<JavascriptApi> javascriptApi = GetJavascriptApi(browser); |
| 39 | + if (javascriptApi.get()) { |
| 40 | + javascriptApi->SetIsFullscreen(isFullscreen); |
| 41 | + } |
| 42 | + return true; |
| 43 | + } |
| 44 | + LOG_ERROR << "Unhandled message in OnProcessMessageReceived"; |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +/// |
| 49 | +// Called immediately after the V8 context for a frame has been created. To |
| 50 | +// retrieve the JavaScript 'window' object use the CefV8Context::GetGlobal() |
| 51 | +// method. V8 handles can only be accessed from the thread on which they are |
| 52 | +// created. A task runner for posting tasks on the associated thread can be |
| 53 | +// retrieved via the CefV8Context::GetTaskRunner() method. |
| 54 | +/// |
| 55 | +/*--cef()--*/ |
| 56 | +void App::OnContextCreated(CefRefPtr<CefBrowser> browser, |
| 57 | + CefRefPtr<CefFrame> frame, |
| 58 | + CefRefPtr<CefV8Context> context) { |
| 59 | + // RENDERER PROCESS. |
| 60 | + LOG_DEBUG << "OnContextCreated()"; |
| 61 | + CefRefPtr<CefV8Value> window = context->GetGlobal(); |
| 62 | + CefRefPtr<CefV8Handler> handler = GetJavascriptApi(browser); |
| 63 | + if (!handler.get()) { |
| 64 | + LOG_ERROR << "GetJavascriptApi() failed in OnContextCreated()"; |
| 65 | + return; |
| 66 | + } |
| 67 | + // Javascipt bindings. |
| 68 | + // The phpdesktop object. |
| 69 | + CefRefPtr<CefV8Value> phpdesktop = CefV8Value::CreateObject(NULL, NULL); |
| 70 | + window->SetValue("phpdesktop", phpdesktop, V8_PROPERTY_ATTRIBUTE_READONLY); |
| 71 | + // Methods. |
| 72 | + const char* methods[] = { |
| 73 | + "GetVersion", |
| 74 | + "ToggleFullscreen", |
| 75 | + "IsFullscreen", |
| 76 | + NULL |
| 77 | + }; |
| 78 | + for (int i = 0; methods[i] != NULL; i++) { |
| 79 | + CefRefPtr<CefV8Value> method = CefV8Value::CreateFunction( |
| 80 | + methods[i], handler); |
| 81 | + phpdesktop->SetValue(method->GetFunctionName(), method, |
| 82 | + V8_PROPERTY_ATTRIBUTE_READONLY); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// |
| 87 | +// Called after a browser has been created. When browsing cross-origin a new |
| 88 | +// browser will be created before the old browser with the same identifier is |
| 89 | +// destroyed. |
| 90 | +/// |
| 91 | +/*--cef()--*/ |
| 92 | +void App::OnBrowserCreated(CefRefPtr<CefBrowser> browser) { |
| 93 | + LOG_DEBUG << "OnBrowserCreated()"; |
| 94 | + StoreJavascriptApi(browser, new JavascriptApi(browser)); |
| 95 | +} |
| 96 | + |
| 97 | +/// |
| 98 | +// Called before a browser is destroyed. |
| 99 | +/// |
| 100 | +/*--cef()--*/ |
| 101 | +void App::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) { |
| 102 | + LOG_DEBUG << "OnBrowserDestroyed()"; |
| 103 | + RemoveJavascriptApi(browser); |
| 104 | +} |
| 105 | + |
| 106 | +// ---------------------------------------------------------------------------- |
| 107 | +// CefApp methods |
| 108 | +// ---------------------------------------------------------------------------- |
| 109 | + |
| 110 | +/// |
| 111 | +// Provides an opportunity to view and/or modify command-line arguments before |
| 112 | +// processing by CEF and Chromium. The |process_type| value will be empty for |
| 113 | +// the browser process. Do not keep a reference to the CefCommandLine object |
| 114 | +// passed to this method. The CefSettings.command_line_args_disabled value |
| 115 | +// can be used to start with an empty command-line object. Any values |
| 116 | +// specified in CefSettings that equate to command-line arguments will be set |
| 117 | +// before this method is called. Be cautious when using this method to modify |
| 118 | +// command-line arguments for non-browser processes as this may result in |
| 119 | +// undefined behavior including crashes. |
| 120 | +/// |
| 121 | +/*--cef(optional_param=process_type)--*/ |
| 122 | +void App::OnBeforeCommandLineProcessing( |
| 123 | + const CefString& process_type, |
| 124 | + CefRefPtr<CefCommandLine> command_line) { |
| 125 | + // May be called on any thread? |
| 126 | + if (process_type.empty()) { |
| 127 | + // Browser process. |
| 128 | + json_value* appSettings = GetApplicationSettings(); |
| 129 | + json_value switches = (*appSettings)["chrome"]["command_line_switches"]; |
| 130 | + if (switches.type == json_object) { |
| 131 | + int length = switches.u.object.length; |
| 132 | + for (int i = 0; i < length; i++) { |
| 133 | + std::string name = switches.u.object.values[i].name; |
| 134 | + std::string value = static_cast<const char*>(*switches.u.object.values[i].value); |
| 135 | + if (name.find("-") == 0) { |
| 136 | + LOG_WARNING << "Invalid command line switch: " << name; |
| 137 | + continue; |
| 138 | + } |
| 139 | + if (command_line->HasSwitch(name)) { |
| 140 | + if (value.empty()) { |
| 141 | + // Switch already set, do nothing. |
| 142 | + } else { |
| 143 | + std::string oldValue = command_line->GetSwitchValue(name); |
| 144 | + if (oldValue != value) { |
| 145 | + // Overwrite the switch with a new value. |
| 146 | + command_line->AppendSwitchWithValue(name, value); |
| 147 | + } |
| 148 | + } |
| 149 | + } else { |
| 150 | + if (value.empty()) { |
| 151 | + command_line->AppendSwitch(name); |
| 152 | + } else { |
| 153 | + command_line->AppendSwitchWithValue(name, value); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + std::string process_name = "browser"; |
| 160 | + if (!process_type.empty()) { |
| 161 | + process_name = process_type; |
| 162 | + } |
| 163 | + LOG_DEBUG << "Command line string for the " << process_name << " process: " |
| 164 | + << command_line->GetCommandLineString().ToString(); |
| 165 | +} |
| 166 | + |
| 167 | +// ---------------------------------------------------------------------------- |
| 168 | +// CefBrowserProcessHandler methods |
| 169 | +// ---------------------------------------------------------------------------- |
| 170 | + |
| 171 | +/// |
| 172 | +// Called on the browser process UI thread immediately after the CEF context |
| 173 | +// has been initialized. |
| 174 | +/// |
| 175 | +void App::OnContextInitialized() { |
| 176 | + REQUIRE_UI_THREAD(); |
| 177 | + LOG_DEBUG << "App::OnContextInitialized()"; |
| 178 | +} |
| 179 | + |
0 commit comments