Repository navigation
Expand file tree
/
Copy pathclient_handler.cpp
More file actions
216 lines (207 loc) · 10.1 KB
/
Copy pathclient_handler.cpp
File metadata and controls
216 lines (207 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2012-2013 The CEF Python authors. All rights reserved.
// License: New BSD License.
// Website: http://code.google.com/p/cefpython/
#include "client_handler.h"
#include "cefpython_public_api.h"
#include "DebugLog.h"
// ----------------------------------------------------------------------------
// CefClient
// ----------------------------------------------------------------------------
bool ClientHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
if (source_process != PID_RENDERER) {
return false;
}
std::string messageName = message->GetName().ToString();
std::string logMessage = "Browser: OnProcessMessageReceived(): ";
logMessage.append(messageName.c_str());
DebugLog(logMessage.c_str());
if (messageName == "OnContextCreated") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) {
int64 frameId = arguments->GetInt(0);
CefRefPtr<CefFrame> frame = browser->GetFrame(frameId);
V8ContextHandler_OnContextCreated(browser, frame);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = OnContextCreated");
return false;
}
} else if (messageName == "OnContextReleased") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 2 \
&& arguments->GetType(0) == VTYPE_INT \
&& arguments->GetType(1) == VTYPE_INT) {
int browserId = arguments->GetInt(0);
int64 frameId = arguments->GetInt(1);
V8ContextHandler_OnContextReleased(browserId, frameId);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = OnContextReleased");
return false;
}
} else if (messageName == "V8FunctionHandler::Execute") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 3
&& arguments->GetType(0) == VTYPE_INT // frameId
&& arguments->GetType(1) == VTYPE_STRING // functionName
&& arguments->GetType(2) == VTYPE_LIST) { // functionArguments
int64 frameId = arguments->GetInt(0);
CefString functionName = arguments->GetString(1);
CefRefPtr<CefListValue> functionArguments = arguments->GetList(2);
CefRefPtr<CefFrame> frame = browser->GetFrame(frameId);
V8FunctionHandler_Execute(browser, frame, functionName, functionArguments);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = V8FunctionHandler::Execute");
return false;
}
} else if (messageName == "ExecutePythonCallback") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 2
&& arguments->GetType(0) == VTYPE_INT // callbackId
&& arguments->GetType(1) == VTYPE_LIST) { // functionArguments
int callbackId = arguments->GetInt(0);
CefRefPtr<CefListValue> functionArguments = arguments->GetList(1);
ExecutePythonCallback(browser, callbackId, functionArguments);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = ExecutePythonCallback");
return false;
}
} else if (messageName == "RemovePythonCallbacksForFrame") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) {
int frameId = arguments->GetInt(0);
RemovePythonCallbacksForFrame(frameId);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = ExecutePythonCallback");
return false;
}
}
return false;
}
// ----------------------------------------------------------------------------
// CefLifeSpanHandler
// ----------------------------------------------------------------------------
///
// Called on the IO thread before a new popup window is created. The |browser|
// and |frame| parameters represent the source of the popup request. The
// |target_url| and |target_frame_name| values may be empty if none were
// specified with the request. The |popupFeatures| structure contains
// information about the requested popup window. To allow creation of the
// popup window optionally modify |windowInfo|, |client|, |settings| and
// |no_javascript_access| and return false. To cancel creation of the popup
// window return true. The |client| and |settings| values will default to the
// source browser's values. The |no_javascript_access| value indicates whether
// the new browser window should be scriptable and in the same process as the
// source browser.
/*--cef(optional_param=target_url,optional_param=target_frame_name)--*/
bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& target_url,
const CefString& target_frame_name,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
bool* no_javascript_access) {
return false;
}
///
// Called after a new browser is created.
///
/*--cef()--*/
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
}
///
// Called when a modal window is about to display and the modal loop should
// begin running. Return false to use the default modal loop implementation or
// true to use a custom implementation.
///
/*--cef()--*/
bool ClientHandler::RunModal(CefRefPtr<CefBrowser> browser) {
return false;
}
///
// Called when a browser has recieved a request to close. This may result
// directly from a call to CefBrowserHost::CloseBrowser() or indirectly if the
// browser is a top-level OS window created by CEF and the user attempts to
// close the window. This method will be called after the JavaScript
// 'onunload' event has been fired. It will not be called for browsers after
// the associated OS window has been destroyed (for those browsers it is no
// longer possible to cancel the close).
//
// If CEF created an OS window for the browser returning false will send an OS
// close notification to the browser window's top-level owner (e.g. WM_CLOSE
// on Windows, performClose: on OS-X and "delete_event" on Linux). If no OS
// window exists (window rendering disabled) returning false will cause the
// browser object to be destroyed immediately. Return true if the browser is
// parented to another window and that other window needs to receive close
// notification via some non-standard technique.
//
// If an application provides its own top-level window it should handle OS
// close notifications by calling CefBrowserHost::CloseBrowser(false) instead
// of immediately closing (see the example below). This gives CEF an
// opportunity to process the 'onbeforeunload' event and optionally cancel the
// close before DoClose() is called.
//
// The CefLifeSpanHandler::OnBeforeClose() method will be called immediately
// before the browser object is destroyed. The application should only exit
// after OnBeforeClose() has been called for all existing browsers.
//
// If the browser represents a modal window and a custom modal loop
// implementation was provided in CefLifeSpanHandler::RunModal() this callback
// should be used to restore the opener window to a usable state.
//
// By way of example consider what should happen during window close when the
// browser is parented to an application-provided top-level OS window.
// 1. User clicks the window close button which sends an OS close
// notification (e.g. WM_CLOSE on Windows, performClose: on OS-X and
// "delete_event" on Linux).
// 2. Application's top-level window receives the close notification and:
// A. Calls CefBrowserHost::CloseBrowser(false).
// B. Cancels the window close.
// 3. JavaScript 'onbeforeunload' handler executes and shows the close
// confirmation dialog (which can be overridden via
// CefJSDialogHandler::OnBeforeUnloadDialog()).
// 4. User approves the close.
// 5. JavaScript 'onunload' handler executes.
// 6. Application's DoClose() handler is called. Application will:
// A. Call CefBrowserHost::ParentWindowWillClose() to notify CEF that the
// parent window will be closing.
// B. Set a flag to indicate that the next close attempt will be allowed.
// C. Return false.
// 7. CEF sends an OS close notification.
// 8. Application's top-level window receives the OS close notification and
// allows the window to close based on the flag from #6B.
// 9. Browser OS window is destroyed.
// 10. Application's CefLifeSpanHandler::OnBeforeClose() handler is called and
// the browser object is destroyed.
// 11. Application exits by calling CefQuitMessageLoop() if no other browsers
// exist.
///
/*--cef()--*/
bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
return false;
}
///
// Called just before a browser is destroyed. Release all references to the
// browser object and do not attempt to execute any methods on the browser
// object after this callback returns. If this is a modal window and a custom
// modal loop implementation was provided in RunModal() this callback should
// be used to exit the custom modal loop. See DoClose() documentation for
// additional usage information.
///
/*--cef()--*/
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
LifespanHandler_OnBeforeClose(browser);
}