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. It is now safe to begin performing 99 // actions with |browser|. CefFrameHandler callbacks related to initial main 100 // frame creation will arrive before this callback. See CefFrameHandler 101 // documentation for additional usage information. 102 /// 103 /*--cef()--*/ OnAfterCreated(CefRefPtr<CefBrowser> browser)104 virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) {} 105 106 /// 107 // Called when a browser has recieved a request to close. This may result 108 // directly from a call to CefBrowserHost::*CloseBrowser() or indirectly if 109 // the browser is parented to a top-level window created by CEF and the user 110 // attempts to close that window (by clicking the 'X', for example). The 111 // DoClose() method will be called after the JavaScript 'onunload' event has 112 // been fired. 113 // 114 // An application should handle top-level owner window close notifications by 115 // calling CefBrowserHost::TryCloseBrowser() or 116 // CefBrowserHost::CloseBrowser(false) instead of allowing the window to close 117 // immediately (see the examples below). This gives CEF an opportunity to 118 // process the 'onbeforeunload' event and optionally cancel the close before 119 // DoClose() is called. 120 // 121 // When windowed rendering is enabled CEF will internally create a window or 122 // view to host the browser. In that case returning false from DoClose() will 123 // send the standard close notification to the browser's top-level owner 124 // window (e.g. WM_CLOSE on Windows, performClose: on OS X, "delete_event" on 125 // Linux or CefWindowDelegate::CanClose() callback from Views). If the 126 // browser's host window/view has already been destroyed (via view hierarchy 127 // tear-down, for example) then DoClose() will not be called for that browser 128 // since is no longer possible to cancel the close. 129 // 130 // When windowed rendering is disabled returning false from DoClose() will 131 // cause the browser object to be destroyed immediately. 132 // 133 // If the browser's top-level owner window requires a non-standard close 134 // notification then send that notification from DoClose() and return true. 135 // 136 // The CefLifeSpanHandler::OnBeforeClose() method will be called after 137 // DoClose() (if DoClose() is called) and immediately before the browser 138 // object is destroyed. The application should only exit after OnBeforeClose() 139 // has been called for all existing browsers. 140 // 141 // The below examples describe what should happen during window close when the 142 // browser is parented to an application-provided top-level window. 143 // 144 // Example 1: Using CefBrowserHost::TryCloseBrowser(). This is recommended for 145 // clients using standard close handling and windows created on the browser 146 // process UI thread. 147 // 1. User clicks the window close button which sends a close notification to 148 // the application's top-level window. 149 // 2. Application's top-level window receives the close notification and 150 // calls TryCloseBrowser() (which internally calls CloseBrowser(false)). 151 // TryCloseBrowser() returns false so the client cancels the window close. 152 // 3. JavaScript 'onbeforeunload' handler executes and shows the close 153 // confirmation dialog (which can be overridden via 154 // CefJSDialogHandler::OnBeforeUnloadDialog()). 155 // 4. User approves the close. 156 // 5. JavaScript 'onunload' handler executes. 157 // 6. CEF sends a close notification to the application's top-level window 158 // (because DoClose() returned false by default). 159 // 7. Application's top-level window receives the close notification and 160 // calls TryCloseBrowser(). TryCloseBrowser() returns true so the client 161 // allows the window close. 162 // 8. Application's top-level window is destroyed. 163 // 9. Application's OnBeforeClose() handler is called and the browser object 164 // is destroyed. 165 // 10. Application exits by calling CefQuitMessageLoop() if no other browsers 166 // exist. 167 // 168 // Example 2: Using CefBrowserHost::CloseBrowser(false) and implementing the 169 // DoClose() callback. This is recommended for clients using non-standard 170 // close handling or windows that were not created on the browser process UI 171 // thread. 172 // 1. User clicks the window close button which sends a close notification to 173 // the application's top-level window. 174 // 2. Application's top-level window receives the close notification and: 175 // A. Calls CefBrowserHost::CloseBrowser(false). 176 // B. Cancels the window close. 177 // 3. JavaScript 'onbeforeunload' handler executes and shows the close 178 // confirmation dialog (which can be overridden via 179 // CefJSDialogHandler::OnBeforeUnloadDialog()). 180 // 4. User approves the close. 181 // 5. JavaScript 'onunload' handler executes. 182 // 6. Application's DoClose() handler is called. Application will: 183 // A. Set a flag to indicate that the next close attempt will be allowed. 184 // B. Return false. 185 // 7. CEF sends an close notification to the application's top-level window. 186 // 8. Application's top-level window receives the close notification and 187 // allows the window to close based on the flag from #6B. 188 // 9. Application's top-level window is destroyed. 189 // 10. Application's OnBeforeClose() handler is called and the browser object 190 // is destroyed. 191 // 11. Application exits by calling CefQuitMessageLoop() if no other browsers 192 // exist. 193 /// 194 /*--cef()--*/ DoClose(CefRefPtr<CefBrowser> browser)195 virtual bool DoClose(CefRefPtr<CefBrowser> browser) { return false; } 196 197 /// 198 // Called just before a browser is destroyed. Release all references to the 199 // browser object and do not attempt to execute any methods on the browser 200 // object (other than IsValid, GetIdentifier or IsSame) after this callback 201 // returns. CefFrameHandler callbacks related to final main frame destruction 202 // will arrive after this callback and CefBrowser::IsValid will return false 203 // at that time. Any in-progress network requests associated with |browser| 204 // will be aborted when the browser is destroyed, and 205 // CefResourceRequestHandler callbacks related to those requests may still 206 // arrive on the IO thread after this callback. See CefFrameHandler and 207 // DoClose() documentation for additional usage information. 208 /// 209 /*--cef()--*/ OnBeforeClose(CefRefPtr<CefBrowser> browser)210 virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) {} 211 }; 212 213 #endif // CEF_INCLUDE_CEF_LIFE_SPAN_HANDLER_H_ 214