1111#include " javascript_callback.h"
1212#include " python_callback.h"
1313
14+ CefRefPtr<CefBrowser> g_mainBrowser;
15+
1416// -----------------------------------------------------------------------------
1517// CefApp
1618// -----------------------------------------------------------------------------
@@ -83,10 +85,45 @@ void CefPythonApp::OnWebKitInitialized() {
8385}
8486
8587void CefPythonApp::OnBrowserCreated (CefRefPtr<CefBrowser> browser) {
88+ // TODO: keep a list of all browsers, for sending the message
89+ // about browser being destroyed use the first browser
90+ // from that list. See this topic:
91+ // http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10893
92+ // --
93+ // The g_mainBrowser is not guaranteed to be the "main" browser,
94+ // it is just the first browser being created, that in most cases
95+ // should be the main browser. We will need it later to send
96+ // a process message about other browsers being destroyed.
97+ if (!g_mainBrowser.get ()) {
98+ g_mainBrowser = browser;
99+ }
86100}
87101
88102void CefPythonApp::OnBrowserDestroyed (CefRefPtr<CefBrowser> browser) {
103+ DebugLog (" Renderer: OnBrowserDestroyed()" );
89104 RemoveJavascriptBindings (browser);
105+ // TODO: keep a list of all browsers, for sending the message
106+ // about browser being destroyed use the first browser
107+ // from that list. See this topic:
108+ // http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10893
109+ if (g_mainBrowser.get () && g_mainBrowser->IsSame (browser)) {
110+ g_mainBrowser = NULL ;
111+ } else {
112+ // OnBrowserDestroyed process message is not sent for
113+ // the main browser.
114+ if (g_mainBrowser.get ()) {
115+ CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create (
116+ " OnBrowserDestroyed" );
117+ CefRefPtr<CefListValue> arguments = message->GetArgumentList ();
118+ arguments->SetInt (0 , browser->GetIdentifier ());
119+ g_mainBrowser->SendProcessMessage (PID_BROWSER, message);
120+ } else {
121+ DebugLog (" Renderer: OnBrowserDestroyed(): ERROR: main browser not " \
122+ " found, cannot send process message to the browser process " \
123+ " about browser being destroyed. This will cause memory leaks" \
124+ " and possibly other errors that could crash application." );
125+ }
126+ }
90127}
91128
92129bool CefPythonApp::OnBeforeNavigation (CefRefPtr<CefBrowser> browser,
@@ -103,7 +140,7 @@ void CefPythonApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
103140 DebugLog (" Renderer: OnContextCreated()" );
104141 CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create (
105142 " OnContextCreated" );
106- CefRefPtr<CefListValue> args = message->GetArgumentList ();
143+ CefRefPtr<CefListValue> arguments = message->GetArgumentList ();
107144 /*
108145 Sending int64 type using process messaging would require
109146 converting it to a string or a binary, or you could send
@@ -125,7 +162,7 @@ void CefPythonApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
125162 // from string to int64. But it is rather unlikely
126163 // that number of frames will exceed int range, so
127164 // casting it to int for now.
128- args ->SetInt (0 , (int )(frame->GetIdentifier ()));
165+ arguments ->SetInt (0 , (int )(frame->GetIdentifier ()));
129166 browser->SendProcessMessage (PID_BROWSER, message);
130167 CefRefPtr<CefDictionaryValue> jsBindings = GetJavascriptBindings (browser);
131168 if (jsBindings.get ()) {
@@ -149,16 +186,27 @@ void CefPythonApp::OnContextReleased(CefRefPtr<CefBrowser> browser,
149186 CefRefPtr<CefFrame> frame,
150187 CefRefPtr<CefV8Context> context) {
151188 DebugLog (" Renderer: OnContextReleased()" );
152- CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create (
153- " OnContextReleased" );
154- CefRefPtr<CefListValue> args = message->GetArgumentList ();
155- // TODO: losing int64 precision, the solution is to convert
156- // it to string and then in the Browser process back
157- // from string to int64. But it is rather unlikely
158- // that number of frames will exceed int range, so
159- // casting it to int for now.
160- args->SetInt (0 , (int )(frame->GetIdentifier ()));
161- browser->SendProcessMessage (PID_BROWSER, message);
189+ if (g_mainBrowser.get ()) {
190+ CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create (
191+ " OnContextReleased" );
192+ CefRefPtr<CefListValue> arguments = message->GetArgumentList ();
193+ arguments->SetInt (0 , browser->GetIdentifier ());
194+ // TODO: losing int64 precision, the solution is to convert
195+ // it to string and then in the Browser process back
196+ // from string to int64. But it is rather unlikely
197+ // that number of frames will exceed int range, so
198+ // casting it to int for now.
199+ arguments->SetInt (1 , (int )(frame->GetIdentifier ()));
200+ // Should we send the message using current "browser"
201+ // when this is not the main frame? It could fail, so
202+ // it is more reliable to always use the main browser.
203+ g_mainBrowser->SendProcessMessage (PID_BROWSER, message);
204+ } else {
205+ DebugLog (" Renderer: OnContextReleased(): ERROR: main browser not " \
206+ " found, cannot send process message to the browser process " \
207+ " about frame being destroyed. This will cause memory leaks" \
208+ " and possibly other errors that could crash application." );
209+ }
162210 // Clear javascript callbacks.
163211 RemoveJavascriptCallbacksForFrame (frame);
164212 RemovePythonCallbacksForFrame (frame);
0 commit comments