1 // Copyright (c) 2012 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 // 32 // The contents of this file must follow a specific format in order to 33 // support the CEF translator tool. See the translator.README.txt file in the 34 // tools directory for more information. 35 // 36 37 #ifndef CEF_INCLUDE_CEF_LIFE_SPAN_HANDLER_H_ 38 #define CEF_INCLUDE_CEF_LIFE_SPAN_HANDLER_H_ 39 #pragma once 40 41 #include "include/cef_base.h" 42 #include "include/cef_browser.h" 43 44 class CefClient; 45 46 /// 47 // Implement this interface to handle events related to browser life span. The 48 // methods of this class will be called on the UI thread unless otherwise 49 // indicated. 50 /// 51 /*--cef(source=client)--*/ 52 class CefLifeSpanHandler : public virtual CefBaseRefCounted { 53 public: 54 typedef cef_window_open_disposition_t WindowOpenDisposition; 55 56 /// 57 // Called on the UI thread before a new popup browser is created. The 58 // |browser| and |frame| values represent the source of the popup request. The 59 // |target_url| and |target_frame_name| values indicate where the popup 60 // browser should navigate and may be empty if not specified with the request. 61 // The |target_disposition| value indicates where the user intended to open 62 // the popup (e.g. current tab, new tab, etc). The |user_gesture| value will 63 // be true if the popup was opened via explicit user gesture (e.g. clicking a 64 // link) or false if the popup opened automatically (e.g. via the 65 // DomContentLoaded event). The |popupFeatures| structure contains additional 66 // information about the requested popup window. To allow creation of the 67 // popup browser optionally modify |windowInfo|, |client|, |settings| and 68 // |no_javascript_access| and return false. To cancel creation of the popup 69 // browser return true. The |client| and |settings| values will default to the 70 // source browser's values. If the |no_javascript_access| value is set to 71 // false the new browser will not be scriptable and may not be hosted in the 72 // same renderer process as the source browser. Any modifications to 73 // |windowInfo| will be ignored if the parent browser is wrapped in a 74 // CefBrowserView. Popup browser creation will be canceled if the parent 75 // browser is destroyed before the popup browser creation completes (indicated 76 // by a call to OnAfterCreated for the popup browser). The |extra_info| 77 // parameter provides an opportunity to specify extra information specific 78 // to the created popup browser that will be passed to 79 // CefRenderProcessHandler::OnBrowserCreated() in the render process. 80 /// 81 /*--cef(optional_param=target_url,optional_param=target_frame_name)--*/ OnBeforePopup(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const CefString & target_url,const CefString & target_frame_name,WindowOpenDisposition target_disposition,bool user_gesture,const CefPopupFeatures & popupFeatures,CefWindowInfo & windowInfo,CefRefPtr<CefClient> & client,CefBrowserSettings & settings,CefRefPtr<CefDictionaryValue> & extra_info,bool * no_javascript_access)82 virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser, 83 CefRefPtr<CefFrame> frame, 84 const CefString& target_url, 85 const CefString& target_frame_name, 86 WindowOpenDisposition target_disposition, 87 bool user_gesture, 88 const CefPopupFeatures& popupFeatures, 89 CefWindowInfo& windowInfo, 90 CefRefPtr<CefClient>& client, 91 CefBrowserSettings& settings, 92 CefRefPtr<CefDictionaryValue>& extra_info, 93 bool* no_javascript_access) { 94 return false; 95 } 96 97 /// 98 // Called after a new browser is created. This callback will be the first 99 // notification that references |browser|. 100 /// 101 /*--cef()--*/ OnAfterCreated(CefRefPtr<CefBrowser> browser)102 virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) {} 103 104 /// 105 // Called when a browser has recieved a request to close. This may result 106 // directly from a call to CefBrowserHost::*CloseBrowser() or indirectly if 107 // the browser is parented to a top-level window created by CEF and the user 108 // attempts to close that window (by clicking the 'X', for example). The 109 // DoClose() method will be called after the JavaScript 'onunload' event has 110 // been fired. 111 // 112 // An application should handle top-level owner window close notifications by 113 // calling CefBrowserHost::TryCloseBrowser() or 114 // CefBrowserHost::CloseBrowser(false) instead of allowing the window to close 115 // immediately (see the examples below). This gives CEF an opportunity to 116 // process the 'onbeforeunload' event and optionally cancel the close before 117 // DoClose() is called. 118 // 119 // When windowed rendering is enabled CEF will internally create a window or 120 // view to host the browser. In that case returning false from DoClose() will 121 // send the standard close notification to the browser's top-level owner 122 // window (e.g. WM_CLOSE on Windows, performClose: on OS X, "delete_event" on 123 // Linux or CefWindowDelegate::CanClose() callback from Views). If the 124 // browser's host window/view has already been destroyed (via view hierarchy 125 // tear-down, for example) then DoClose() will not be called for that browser 126 // since is no longer possible to cancel the close. 127 // 128 // When windowed rendering is disabled returning false from DoClose() will 129 // cause the browser object to be destroyed immediately. 130 // 131 // If the browser's top-level owner window requires a non-standard close 132 // notification then send that notification from DoClose() and return true. 133 // 134 // The CefLifeSpanHandler::OnBeforeClose() method will be called after 135 // DoClose() (if DoClose() is called) and immediately before the browser 136 // object is destroyed. The application should only exit after OnBeforeClose() 137 // has been called for all existing browsers. 138 // 139 // The below examples describe what should happen during window close when the 140 // browser is parented to an application-provided top-level window. 141 // 142 // Example 1: Using CefBrowserHost::TryCloseBrowser(). This is recommended for 143 // clients using standard close handling and windows created on the browser 144 // process UI thread. 145 // 1. User clicks the window close button which sends a close notification to 146 // the application's top-level window. 147 // 2. Application's top-level window receives the close notification and 148 // calls TryCloseBrowser() (which internally calls CloseBrowser(false)). 149 // TryCloseBrowser() returns false so the client cancels the window close. 150 // 3. JavaScript 'onbeforeunload' handler executes and shows the close 151 // confirmation dialog (which can be overridden via 152 // CefJSDialogHandler::OnBeforeUnloadDialog()). 153 // 4. User approves the close. 154 // 5. JavaScript 'onunload' handler executes. 155 // 6. CEF sends a close notification to the application's top-level window 156 // (because DoClose() returned false by default). 157 // 7. Application's top-level window receives the close notification and 158 // calls TryCloseBrowser(). TryCloseBrowser() returns true so the client 159 // allows the window close. 160 // 8. Application's top-level window is destroyed. 161 // 9. Application's OnBeforeClose() handler is called and the browser object 162 // is destroyed. 163 // 10. Application exits by calling CefQuitMessageLoop() if no other browsers 164 // exist. 165 // 166 // Example 2: Using CefBrowserHost::CloseBrowser(false) and implementing the 167 // DoClose() callback. This is recommended for clients using non-standard 168 // close handling or windows that were not created on the browser process UI 169 // thread. 170 // 1. User clicks the window close button which sends a close notification to 171 // the application's top-level window. 172 // 2. Application's top-level window receives the close notification and: 173 // A. Calls CefBrowserHost::CloseBrowser(false). 174 // B. Cancels the window close. 175 // 3. JavaScript 'onbeforeunload' handler executes and shows the close 176 // confirmation dialog (which can be overridden via 177 // CefJSDialogHandler::OnBeforeUnloadDialog()). 178 // 4. User approves the close. 179 // 5. JavaScript 'onunload' handler executes. 180 // 6. Application's DoClose() handler is called. Application will: 181 // A. Set a flag to indicate that the next close attempt will be allowed. 182 // B. Return false. 183 // 7. CEF sends an close notification to the application's top-level window. 184 // 8. Application's top-level window receives the close notification and 185 // allows the window to close based on the flag from #6B. 186 // 9. Application's top-level window is destroyed. 187 // 10. Application's OnBeforeClose() handler is called and the browser object 188 // is destroyed. 189 // 11. Application exits by calling CefQuitMessageLoop() if no other browsers 190 // exist. 191 /// 192 /*--cef()--*/ DoClose(CefRefPtr<CefBrowser> browser)193 virtual bool DoClose(CefRefPtr<CefBrowser> browser) { return false; } 194 195 /// 196 // Called just before a browser is destroyed. Release all references to the 197 // browser object and do not attempt to execute any methods on the browser 198 // object (other than GetIdentifier or IsSame) after this callback returns. 199 // This callback will be the last notification that references |browser| on 200 // the UI thread. Any in-progress network requests associated with |browser| 201 // will be aborted when the browser is destroyed, and 202 // CefResourceRequestHandler callbacks related to those requests may still 203 // arrive on the IO thread after this method is called. See DoClose() 204 // documentation for additional usage information. 205 /// 206 /*--cef()--*/ OnBeforeClose(CefRefPtr<CefBrowser> browser)207 virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) {} 208 }; 209 210 #endif // CEF_INCLUDE_CEF_LIFE_SPAN_HANDLER_H_ 211