1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_
8 #include "chrome/browser/devtools/devtools_contents_resizing_strategy.h"
9 #include "chrome/browser/devtools/devtools_toggle_action.h"
10 #include "chrome/browser/devtools/devtools_ui_bindings.h"
11 #include "content/public/browser/web_contents_delegate.h"
12 #include "content/public/browser/web_contents_observer.h"
16 class DevToolsWindowTesting
;
17 class DevToolsEventForwarder
;
20 class DevToolsAgentHost
;
21 struct NativeWebKeyboardEvent
;
25 namespace user_prefs
{
26 class PrefRegistrySyncable
;
29 class DevToolsWindow
: public DevToolsUIBindings::Delegate
,
30 public content::WebContentsDelegate
{
32 class ObserverWithAccessor
: public content::WebContentsObserver
{
34 explicit ObserverWithAccessor(content::WebContents
* web_contents
);
35 ~ObserverWithAccessor() override
;
38 DISALLOW_COPY_AND_ASSIGN(ObserverWithAccessor
);
41 static const char kDevToolsApp
[];
43 ~DevToolsWindow() override
;
45 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
47 // Return the DevToolsWindow for the given WebContents if one exists,
49 static DevToolsWindow
* GetInstanceForInspectedWebContents(
50 content::WebContents
* inspected_web_contents
);
52 // Return the docked DevTools WebContents for the given inspected WebContents
53 // if one exists and should be shown in browser window, otherwise NULL.
54 // This method will return only fully initialized window ready to be
56 // If |out_strategy| is not NULL, it will contain resizing strategy.
57 // For immediately-ready-to-use but maybe not yet fully initialized DevTools
58 // use |GetInstanceForInspectedRenderViewHost| instead.
59 static content::WebContents
* GetInTabWebContents(
60 content::WebContents
* inspected_tab
,
61 DevToolsContentsResizingStrategy
* out_strategy
);
63 static bool IsDevToolsWindow(content::WebContents
* web_contents
);
64 static DevToolsWindow
* AsDevToolsWindow(content::WebContents
* web_contents
);
66 // Open or reveal DevTools window, and perform the specified action.
67 static DevToolsWindow
* OpenDevToolsWindow(
68 content::WebContents
* inspected_web_contents
,
69 const DevToolsToggleAction
& action
);
71 // Open or reveal DevTools window, with no special action.
72 static DevToolsWindow
* OpenDevToolsWindow(
73 content::WebContents
* inspected_web_contents
);
75 // Perform specified action for current WebContents inside a |browser|.
76 // This may close currently open DevTools window.
77 static DevToolsWindow
* ToggleDevToolsWindow(
79 const DevToolsToggleAction
& action
);
81 // External frontend is always undocked.
82 static void OpenExternalFrontend(
84 const std::string
& frontend_uri
,
85 const scoped_refptr
<content::DevToolsAgentHost
>& agent_host
,
88 // Worker frontend is always undocked.
89 static DevToolsWindow
* OpenDevToolsWindowForWorker(
91 const scoped_refptr
<content::DevToolsAgentHost
>& worker_agent
);
93 static void InspectElement(content::WebContents
* inspected_web_contents
,
97 // Sets closure to be called after load is done. If already loaded, calls
98 // closure immediately.
99 void SetLoadCompletedCallback(const base::Closure
& closure
);
101 // Forwards an unhandled keyboard event to the DevTools frontend.
102 bool ForwardKeyboardEvent(const content::NativeWebKeyboardEvent
& event
);
104 // content::WebContentsDelegate overrides.
105 content::WebContents
* OpenURLFromTab(
106 content::WebContents
* source
,
107 const content::OpenURLParams
& params
) override
;
109 // BeforeUnload interception ////////////////////////////////////////////////
111 // In order to preserve any edits the user may have made in devtools, the
112 // beforeunload event of the inspected page is hooked - devtools gets the
113 // first shot at handling beforeunload and presents a dialog to the user. If
114 // the user accepts the dialog then the script is given a chance to handle
115 // it. This way 2 dialogs may be displayed: one from the devtools asking the
116 // user to confirm that they're ok with their devtools edits going away and
117 // another from the webpage as the result of its beforeunload handler.
118 // The following set of methods handle beforeunload event flow through
119 // devtools window. When the |contents| with devtools opened on them are
120 // getting closed, the following sequence of calls takes place:
121 // 1. |DevToolsWindow::InterceptPageBeforeUnload| is called and indicates
122 // whether devtools intercept the beforeunload event.
123 // If InterceptPageBeforeUnload() returns true then the following steps
124 // will take place; otherwise only step 4 will be reached and none of the
125 // corresponding functions in steps 2 & 3 will get called.
126 // 2. |DevToolsWindow::InterceptPageBeforeUnload| fires beforeunload event
127 // for devtools frontend, which will asynchronously call
128 // |WebContentsDelegate::BeforeUnloadFired| method.
129 // In case of docked devtools window, devtools are set as a delegate for
130 // its frontend, so method |DevToolsWindow::BeforeUnloadFired| will be
132 // If devtools window is undocked it's not set as the delegate so the call
133 // to BeforeUnloadFired is proxied through HandleBeforeUnload() rather
134 // than getting called directly.
135 // 3a. If |DevToolsWindow::BeforeUnloadFired| is called with |proceed|=false
136 // it calls throught to the content's BeforeUnloadFired(), which from the
137 // WebContents perspective looks the same as the |content|'s own
138 // beforeunload dialog having had it's 'stay on this page' button clicked.
139 // 3b. If |proceed| = true, then it fires beforeunload event on |contents|
140 // and everything proceeds as it normally would without the Devtools
142 // 4. If the user cancels the dialog put up by either the WebContents or
143 // devtools frontend, then |contents|'s |BeforeUnloadFired| callback is
144 // called with the proceed argument set to false, this causes
145 // |DevToolsWindow::OnPageCloseCancelled| to be called.
147 // Devtools window in undocked state is not set as a delegate of
148 // its frontend. Instead, an instance of browser is set as the delegate, and
149 // thus beforeunload event callback from devtools frontend is not delivered
150 // to the instance of devtools window, which is solely responsible for
151 // managing custom beforeunload event flow.
152 // This is a helper method to route callback from
153 // |Browser::BeforeUnloadFired| back to |DevToolsWindow::BeforeUnloadFired|.
154 // * |proceed| - true if the user clicked 'ok' in the beforeunload dialog,
156 // * |proceed_to_fire_unload| - output parameter, whether we should continue
157 // to fire the unload event or stop things here.
158 // Returns true if devtools window is in a state of intercepting beforeunload
159 // event and if it will manage unload process on its own.
160 static bool HandleBeforeUnload(content::WebContents
* contents
,
162 bool* proceed_to_fire_unload
);
164 // Returns true if this contents beforeunload event was intercepted by
165 // devtools and false otherwise. If the event was intercepted, caller should
166 // not fire beforeunlaod event on |contents| itself as devtools window will
167 // take care of it, otherwise caller should continue handling the event as
169 static bool InterceptPageBeforeUnload(content::WebContents
* contents
);
171 // Returns true if devtools browser has already fired its beforeunload event
172 // as a result of beforeunload event interception.
173 static bool HasFiredBeforeUnloadEventForDevToolsBrowser(Browser
* browser
);
175 // Returns true if devtools window would like to hook beforeunload event
176 // of this |contents|.
177 static bool NeedsToInterceptBeforeUnload(content::WebContents
* contents
);
179 // Notify devtools window that closing of |contents| was cancelled
181 static void OnPageCloseCanceled(content::WebContents
* contents
);
184 friend class DevToolsWindowTesting
;
186 // DevTools lifecycle typically follows this way:
187 // - Toggle/Open: client call;
189 // - ScheduleShow: setup window to be functional, but not yet show;
190 // - DocumentOnLoadCompletedInMainFrame: frontend loaded;
191 // - SetIsDocked: frontend decided on docking state;
192 // - OnLoadCompleted: ready to present frontend;
193 // - Show: actually placing frontend WebContents to a Browser or docked place;
194 // - DoAction: perform action passed in Toggle/Open;
196 // - CloseWindow: initiates before unload handling;
197 // - CloseContents: destroys frontend;
198 // - DevToolsWindow is dead once it's main_web_contents dies.
201 kOnLoadFired
, // Implies SetIsDocked was not yet called.
202 kIsDockedSet
, // Implies DocumentOnLoadCompleted was not yet called.
207 DevToolsWindow(Profile
* profile
,
208 content::WebContents
* main_web_contents
,
209 DevToolsUIBindings
* bindings
,
210 content::WebContents
* inspected_web_contents
,
213 static DevToolsWindow
* Create(Profile
* profile
,
214 const GURL
& frontend_url
,
215 content::WebContents
* inspected_web_contents
,
216 bool shared_worker_frontend
,
217 const std::string
& remote_frontend
,
219 const std::string
& settings
);
220 static GURL
GetDevToolsURL(Profile
* profile
,
221 const GURL
& base_url
,
222 bool shared_worker_frontend
,
223 const std::string
& remote_frontend
,
225 const std::string
& settings
);
226 static DevToolsWindow
* FindDevToolsWindow(content::DevToolsAgentHost
*);
227 static DevToolsWindow
* CreateDevToolsWindowForWorker(Profile
* profile
);
228 static DevToolsWindow
* ToggleDevToolsWindow(
229 content::WebContents
* web_contents
,
231 const DevToolsToggleAction
& action
,
232 const std::string
& settings
);
234 // content::WebContentsDelegate:
235 void ActivateContents(content::WebContents
* contents
) override
;
236 void AddNewContents(content::WebContents
* source
,
237 content::WebContents
* new_contents
,
238 WindowOpenDisposition disposition
,
239 const gfx::Rect
& initial_rect
,
241 bool* was_blocked
) override
;
242 void WebContentsCreated(content::WebContents
* source_contents
,
243 int opener_render_frame_id
,
244 const base::string16
& frame_name
,
245 const GURL
& target_url
,
246 content::WebContents
* new_contents
) override
;
247 void CloseContents(content::WebContents
* source
) override
;
248 void ContentsZoomChange(bool zoom_in
) override
;
249 void BeforeUnloadFired(content::WebContents
* tab
,
251 bool* proceed_to_fire_unload
) override
;
252 bool PreHandleKeyboardEvent(content::WebContents
* source
,
253 const content::NativeWebKeyboardEvent
& event
,
254 bool* is_keyboard_shortcut
) override
;
255 void HandleKeyboardEvent(
256 content::WebContents
* source
,
257 const content::NativeWebKeyboardEvent
& event
) override
;
258 content::JavaScriptDialogManager
* GetJavaScriptDialogManager(
259 content::WebContents
* source
) override
;
260 content::ColorChooser
* OpenColorChooser(
261 content::WebContents
* web_contents
,
263 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
264 void RunFileChooser(content::WebContents
* web_contents
,
265 const content::FileChooserParams
& params
) override
;
266 void WebContentsFocused(content::WebContents
* contents
) override
;
267 bool PreHandleGestureEvent(content::WebContents
* source
,
268 const blink::WebGestureEvent
& event
) override
;
270 // content::DevToolsUIBindings::Delegate overrides
271 void ActivateWindow() override
;
272 void CloseWindow() override
;
273 void SetInspectedPageBounds(const gfx::Rect
& rect
) override
;
274 void InspectElementCompleted() override
;
275 void SetIsDocked(bool is_docked
) override
;
276 void OpenInNewTab(const std::string
& url
) override
;
277 void SetWhitelistedShortcuts(const std::string
& message
) override
;
278 void InspectedContentsClosing() override
;
279 void OnLoadCompleted() override
;
280 InfoBarService
* GetInfoBarService() override
;
281 void RenderProcessGone(bool crashed
) override
;
283 void CreateDevToolsBrowser();
284 BrowserWindow
* GetInspectedBrowserWindow();
285 void ScheduleShow(const DevToolsToggleAction
& action
);
286 void Show(const DevToolsToggleAction
& action
);
287 void DoAction(const DevToolsToggleAction
& action
);
288 void LoadCompleted();
289 void UpdateBrowserToolbar();
290 void UpdateBrowserWindow();
291 content::WebContents
* GetInspectedWebContents();
293 scoped_ptr
<ObserverWithAccessor
> inspected_contents_observer_
;
296 content::WebContents
* main_web_contents_
;
297 content::WebContents
* toolbox_web_contents_
;
298 DevToolsUIBindings
* bindings_
;
301 const bool can_dock_
;
302 LifeStage life_stage_
;
303 DevToolsToggleAction action_on_load_
;
304 DevToolsContentsResizingStrategy contents_resizing_strategy_
;
305 // True if we're in the process of handling a beforeunload event originating
306 // from the inspected webcontents, see InterceptPageBeforeUnload for details.
307 bool intercepted_page_beforeunload_
;
308 base::Closure load_completed_callback_
;
309 base::Closure close_callback_
;
311 base::TimeTicks inspect_element_start_time_
;
312 scoped_ptr
<DevToolsEventForwarder
> event_forwarder_
;
314 friend class DevToolsEventForwarder
;
315 DISALLOW_COPY_AND_ASSIGN(DevToolsWindow
);
318 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_