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
);
65 static DevToolsWindow
* FindDevToolsWindow(content::DevToolsAgentHost
*);
67 // Open or reveal DevTools window, and perform the specified action.
68 // How to get pointer to the created window see comments for
69 // ToggleDevToolsWindow().
70 static void OpenDevToolsWindow(content::WebContents
* inspected_web_contents
,
71 const DevToolsToggleAction
& action
);
73 // Open or reveal DevTools window, with no special action.
74 // How to get pointer to the created window see comments for
75 // ToggleDevToolsWindow().
76 static void OpenDevToolsWindow(content::WebContents
* inspected_web_contents
);
78 // Open or reveal DevTools window. This window will be undocked.
79 static void OpenDevToolsWindow(
81 const scoped_refptr
<content::DevToolsAgentHost
>& agent_host
);
83 // Perform specified action for current WebContents inside a |browser|.
84 // This may close currently open DevTools window.
85 // If DeveloperToolsDisabled policy is set, no DevTools window created.
86 // In case if needed pointer to the created window one should use
87 // DevToolsAgentHost and DevToolsWindow::FindDevToolsWindow(). E.g.:
89 // scoped_refptr<content::DevToolsAgentHost> agent(
90 // content::DevToolsAgentHost::GetOrCreateFor(inspected_web_contents));
91 // DevToolsWindow::ToggleDevToolsWindow(
92 // inspected_web_contents, DevToolsToggleAction::Show());
93 // DevToolsWindow* window = DevToolsWindow::FindDevToolsWindow(agent.get());
95 static void ToggleDevToolsWindow(
97 const DevToolsToggleAction
& action
);
99 // External frontend is always undocked.
100 static void OpenExternalFrontend(
102 const std::string
& frontend_uri
,
103 const scoped_refptr
<content::DevToolsAgentHost
>& agent_host
,
106 // Worker frontend is always undocked.
107 static void OpenDevToolsWindowForWorker(
109 const scoped_refptr
<content::DevToolsAgentHost
>& worker_agent
);
111 static void InspectElement(content::WebContents
* inspected_web_contents
,
115 // Sets closure to be called after load is done. If already loaded, calls
116 // closure immediately.
117 void SetLoadCompletedCallback(const base::Closure
& closure
);
119 // Forwards an unhandled keyboard event to the DevTools frontend.
120 bool ForwardKeyboardEvent(const content::NativeWebKeyboardEvent
& event
);
122 // content::WebContentsDelegate overrides.
123 content::WebContents
* OpenURLFromTab(
124 content::WebContents
* source
,
125 const content::OpenURLParams
& params
) override
;
127 // BeforeUnload interception ////////////////////////////////////////////////
129 // In order to preserve any edits the user may have made in devtools, the
130 // beforeunload event of the inspected page is hooked - devtools gets the
131 // first shot at handling beforeunload and presents a dialog to the user. If
132 // the user accepts the dialog then the script is given a chance to handle
133 // it. This way 2 dialogs may be displayed: one from the devtools asking the
134 // user to confirm that they're ok with their devtools edits going away and
135 // another from the webpage as the result of its beforeunload handler.
136 // The following set of methods handle beforeunload event flow through
137 // devtools window. When the |contents| with devtools opened on them are
138 // getting closed, the following sequence of calls takes place:
139 // 1. |DevToolsWindow::InterceptPageBeforeUnload| is called and indicates
140 // whether devtools intercept the beforeunload event.
141 // If InterceptPageBeforeUnload() returns true then the following steps
142 // will take place; otherwise only step 4 will be reached and none of the
143 // corresponding functions in steps 2 & 3 will get called.
144 // 2. |DevToolsWindow::InterceptPageBeforeUnload| fires beforeunload event
145 // for devtools frontend, which will asynchronously call
146 // |WebContentsDelegate::BeforeUnloadFired| method.
147 // In case of docked devtools window, devtools are set as a delegate for
148 // its frontend, so method |DevToolsWindow::BeforeUnloadFired| will be
150 // If devtools window is undocked it's not set as the delegate so the call
151 // to BeforeUnloadFired is proxied through HandleBeforeUnload() rather
152 // than getting called directly.
153 // 3a. If |DevToolsWindow::BeforeUnloadFired| is called with |proceed|=false
154 // it calls throught to the content's BeforeUnloadFired(), which from the
155 // WebContents perspective looks the same as the |content|'s own
156 // beforeunload dialog having had it's 'stay on this page' button clicked.
157 // 3b. If |proceed| = true, then it fires beforeunload event on |contents|
158 // and everything proceeds as it normally would without the Devtools
160 // 4. If the user cancels the dialog put up by either the WebContents or
161 // devtools frontend, then |contents|'s |BeforeUnloadFired| callback is
162 // called with the proceed argument set to false, this causes
163 // |DevToolsWindow::OnPageCloseCancelled| to be called.
165 // Devtools window in undocked state is not set as a delegate of
166 // its frontend. Instead, an instance of browser is set as the delegate, and
167 // thus beforeunload event callback from devtools frontend is not delivered
168 // to the instance of devtools window, which is solely responsible for
169 // managing custom beforeunload event flow.
170 // This is a helper method to route callback from
171 // |Browser::BeforeUnloadFired| back to |DevToolsWindow::BeforeUnloadFired|.
172 // * |proceed| - true if the user clicked 'ok' in the beforeunload dialog,
174 // * |proceed_to_fire_unload| - output parameter, whether we should continue
175 // to fire the unload event or stop things here.
176 // Returns true if devtools window is in a state of intercepting beforeunload
177 // event and if it will manage unload process on its own.
178 static bool HandleBeforeUnload(content::WebContents
* contents
,
180 bool* proceed_to_fire_unload
);
182 // Returns true if this contents beforeunload event was intercepted by
183 // devtools and false otherwise. If the event was intercepted, caller should
184 // not fire beforeunlaod event on |contents| itself as devtools window will
185 // take care of it, otherwise caller should continue handling the event as
187 static bool InterceptPageBeforeUnload(content::WebContents
* contents
);
189 // Returns true if devtools browser has already fired its beforeunload event
190 // as a result of beforeunload event interception.
191 static bool HasFiredBeforeUnloadEventForDevToolsBrowser(Browser
* browser
);
193 // Returns true if devtools window would like to hook beforeunload event
194 // of this |contents|.
195 static bool NeedsToInterceptBeforeUnload(content::WebContents
* contents
);
197 // Notify devtools window that closing of |contents| was cancelled
199 static void OnPageCloseCanceled(content::WebContents
* contents
);
202 friend class DevToolsWindowTesting
;
204 // DevTools lifecycle typically follows this way:
205 // - Toggle/Open: client call;
207 // - ScheduleShow: setup window to be functional, but not yet show;
208 // - DocumentOnLoadCompletedInMainFrame: frontend loaded;
209 // - SetIsDocked: frontend decided on docking state;
210 // - OnLoadCompleted: ready to present frontend;
211 // - Show: actually placing frontend WebContents to a Browser or docked place;
212 // - DoAction: perform action passed in Toggle/Open;
214 // - CloseWindow: initiates before unload handling;
215 // - CloseContents: destroys frontend;
216 // - DevToolsWindow is dead once it's main_web_contents dies.
219 kOnLoadFired
, // Implies SetIsDocked was not yet called.
220 kIsDockedSet
, // Implies DocumentOnLoadCompleted was not yet called.
225 DevToolsWindow(Profile
* profile
,
226 content::WebContents
* main_web_contents
,
227 DevToolsUIBindings
* bindings
,
228 content::WebContents
* inspected_web_contents
,
231 static DevToolsWindow
* Create(Profile
* profile
,
232 const GURL
& frontend_url
,
233 content::WebContents
* inspected_web_contents
,
234 bool shared_worker_frontend
,
235 const std::string
& remote_frontend
,
237 const std::string
& settings
);
238 static GURL
GetDevToolsURL(Profile
* profile
,
239 const GURL
& base_url
,
240 bool shared_worker_frontend
,
241 const std::string
& remote_frontend
,
243 const std::string
& settings
);
245 static DevToolsWindow
* CreateDevToolsWindowForWorker(Profile
* profile
);
246 static void ToggleDevToolsWindow(
247 content::WebContents
* web_contents
,
249 const DevToolsToggleAction
& action
,
250 const std::string
& settings
);
252 // content::WebContentsDelegate:
253 void ActivateContents(content::WebContents
* contents
) override
;
254 void AddNewContents(content::WebContents
* source
,
255 content::WebContents
* new_contents
,
256 WindowOpenDisposition disposition
,
257 const gfx::Rect
& initial_rect
,
259 bool* was_blocked
) override
;
260 void WebContentsCreated(content::WebContents
* source_contents
,
261 int opener_render_frame_id
,
262 const base::string16
& frame_name
,
263 const GURL
& target_url
,
264 content::WebContents
* new_contents
) override
;
265 void CloseContents(content::WebContents
* source
) override
;
266 void ContentsZoomChange(bool zoom_in
) override
;
267 void BeforeUnloadFired(content::WebContents
* tab
,
269 bool* proceed_to_fire_unload
) override
;
270 bool PreHandleKeyboardEvent(content::WebContents
* source
,
271 const content::NativeWebKeyboardEvent
& event
,
272 bool* is_keyboard_shortcut
) override
;
273 void HandleKeyboardEvent(
274 content::WebContents
* source
,
275 const content::NativeWebKeyboardEvent
& event
) override
;
276 content::JavaScriptDialogManager
* GetJavaScriptDialogManager(
277 content::WebContents
* source
) override
;
278 content::ColorChooser
* OpenColorChooser(
279 content::WebContents
* web_contents
,
281 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
282 void RunFileChooser(content::WebContents
* web_contents
,
283 const content::FileChooserParams
& params
) override
;
284 void WebContentsFocused(content::WebContents
* contents
) override
;
285 bool PreHandleGestureEvent(content::WebContents
* source
,
286 const blink::WebGestureEvent
& event
) override
;
288 // content::DevToolsUIBindings::Delegate overrides
289 void ActivateWindow() override
;
290 void CloseWindow() override
;
291 void SetInspectedPageBounds(const gfx::Rect
& rect
) override
;
292 void InspectElementCompleted() override
;
293 void SetIsDocked(bool is_docked
) override
;
294 void OpenInNewTab(const std::string
& url
) override
;
295 void SetWhitelistedShortcuts(const std::string
& message
) override
;
296 void InspectedContentsClosing() override
;
297 void OnLoadCompleted() override
;
298 InfoBarService
* GetInfoBarService() override
;
299 void RenderProcessGone(bool crashed
) override
;
301 void CreateDevToolsBrowser();
302 BrowserWindow
* GetInspectedBrowserWindow();
303 void ScheduleShow(const DevToolsToggleAction
& action
);
304 void Show(const DevToolsToggleAction
& action
);
305 void DoAction(const DevToolsToggleAction
& action
);
306 void LoadCompleted();
307 void UpdateBrowserToolbar();
308 void UpdateBrowserWindow();
309 content::WebContents
* GetInspectedWebContents();
311 scoped_ptr
<ObserverWithAccessor
> inspected_contents_observer_
;
314 content::WebContents
* main_web_contents_
;
315 content::WebContents
* toolbox_web_contents_
;
316 DevToolsUIBindings
* bindings_
;
319 const bool can_dock_
;
320 LifeStage life_stage_
;
321 DevToolsToggleAction action_on_load_
;
322 DevToolsContentsResizingStrategy contents_resizing_strategy_
;
323 // True if we're in the process of handling a beforeunload event originating
324 // from the inspected webcontents, see InterceptPageBeforeUnload for details.
325 bool intercepted_page_beforeunload_
;
326 base::Closure load_completed_callback_
;
327 base::Closure close_callback_
;
329 base::TimeTicks inspect_element_start_time_
;
330 scoped_ptr
<DevToolsEventForwarder
> event_forwarder_
;
332 friend class DevToolsEventForwarder
;
333 DISALLOW_COPY_AND_ASSIGN(DevToolsWindow
);
336 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_