Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: chrome/browser/devtools/devtools_window.cc

Issue 253823005: DevTools: extract DevToolsWindowBase, leave docking, unload and factory logic in DevToolsWindow. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/devtools/devtools_window.h" 5 #include "chrome/browser/devtools/devtools_window.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/prefs/scoped_user_pref_update.h" 14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 17 #include "base/time/time.h"
19 #include "base/values.h" 18 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/chrome_page_zoom.h" 19 #include "chrome/browser/chrome_page_zoom.h"
23 #include "chrome/browser/extensions/api/debugger/debugger_api.h"
24 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
25 #include "chrome/browser/extensions/extension_service.h"
26 #include "chrome/browser/file_select_helper.h" 20 #include "chrome/browser/file_select_helper.h"
27 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
28 #include "chrome/browser/infobars/infobar_service.h"
29 #include "chrome/browser/prefs/pref_service_syncable.h" 21 #include "chrome/browser/prefs/pref_service_syncable.h"
30 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/sessions/session_tab_helper.h" 23 #include "chrome/browser/sessions/session_tab_helper.h"
32 #include "chrome/browser/themes/theme_properties.h"
33 #include "chrome/browser/themes/theme_service.h"
34 #include "chrome/browser/themes/theme_service_factory.h"
35 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
36 #include "chrome/browser/ui/browser_dialogs.h" 25 #include "chrome/browser/ui/browser_dialogs.h"
37 #include "chrome/browser/ui/browser_iterator.h" 26 #include "chrome/browser/ui/browser_iterator.h"
38 #include "chrome/browser/ui/browser_list.h" 27 #include "chrome/browser/ui/browser_list.h"
39 #include "chrome/browser/ui/browser_window.h" 28 #include "chrome/browser/ui/browser_window.h"
40 #include "chrome/browser/ui/host_desktop.h" 29 #include "chrome/browser/ui/host_desktop.h"
41 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" 30 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
42 #include "chrome/browser/ui/tabs/tab_strip_model.h" 31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
43 #include "chrome/browser/ui/webui/devtools_ui.h" 32 #include "chrome/browser/ui/webui/devtools_ui.h"
44 #include "chrome/common/chrome_switches.h" 33 #include "chrome/common/chrome_switches.h"
45 #include "chrome/common/extensions/manifest_url_handler.h"
46 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
47 #include "chrome/common/render_messages.h" 35 #include "chrome/common/render_messages.h"
48 #include "chrome/common/url_constants.h" 36 #include "chrome/common/url_constants.h"
49 #include "components/infobars/core/infobar.h"
50 #include "components/user_prefs/pref_registry_syncable.h" 37 #include "components/user_prefs/pref_registry_syncable.h"
51 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/child_process_security_policy.h"
53 #include "content/public/browser/devtools_agent_host.h" 39 #include "content/public/browser/devtools_agent_host.h"
54 #include "content/public/browser/devtools_client_host.h" 40 #include "content/public/browser/devtools_client_host.h"
55 #include "content/public/browser/devtools_manager.h" 41 #include "content/public/browser/devtools_manager.h"
56 #include "content/public/browser/favicon_status.h"
57 #include "content/public/browser/load_notification_details.h"
58 #include "content/public/browser/native_web_keyboard_event.h" 42 #include "content/public/browser/native_web_keyboard_event.h"
59 #include "content/public/browser/navigation_controller.h" 43 #include "content/public/browser/navigation_controller.h"
60 #include "content/public/browser/navigation_entry.h" 44 #include "content/public/browser/navigation_entry.h"
61 #include "content/public/browser/notification_source.h" 45 #include "content/public/browser/notification_source.h"
62 #include "content/public/browser/render_frame_host.h" 46 #include "content/public/browser/render_frame_host.h"
63 #include "content/public/browser/render_process_host.h" 47 #include "content/public/browser/render_process_host.h"
64 #include "content/public/browser/render_view_host.h" 48 #include "content/public/browser/render_view_host.h"
65 #include "content/public/browser/user_metrics.h" 49 #include "content/public/browser/user_metrics.h"
66 #include "content/public/browser/web_contents.h" 50 #include "content/public/browser/web_contents.h"
67 #include "content/public/browser/web_contents_observer.h" 51 #include "content/public/browser/web_contents_observer.h"
68 #include "content/public/browser/web_contents_view.h" 52 #include "content/public/browser/web_contents_view.h"
69 #include "content/public/common/bindings_policy.h"
70 #include "content/public/common/content_client.h" 53 #include "content/public/common/content_client.h"
71 #include "content/public/common/page_transition_types.h" 54 #include "content/public/common/page_transition_types.h"
72 #include "content/public/common/renderer_preferences.h"
73 #include "content/public/common/url_constants.h" 55 #include "content/public/common/url_constants.h"
74 #include "content/public/test/test_utils.h" 56 #include "content/public/test/test_utils.h"
75 #include "extensions/browser/extension_system.h"
76 #include "extensions/common/extension_set.h"
77 #include "grit/generated_resources.h"
78 #include "third_party/WebKit/public/web/WebInputEvent.h" 57 #include "third_party/WebKit/public/web/WebInputEvent.h"
79 #include "ui/base/l10n/l10n_util.h"
80 #include "ui/events/keycodes/keyboard_codes.h" 58 #include "ui/events/keycodes/keyboard_codes.h"
81 59
82 using base::DictionaryValue; 60 using base::DictionaryValue;
83 using blink::WebInputEvent; 61 using blink::WebInputEvent;
84 using content::BrowserThread; 62 using content::BrowserThread;
85 using content::DevToolsAgentHost; 63 using content::DevToolsAgentHost;
86 64 using content::WebContents;
87
88 // DevToolsConfirmInfoBarDelegate ---------------------------------------------
89
90 class DevToolsConfirmInfoBarDelegate : public ConfirmInfoBarDelegate {
91 public:
92 // If |infobar_service| is NULL, runs |callback| with a single argument with
93 // value "false". Otherwise, creates a dev tools confirm infobar and delegate
94 // and adds the infobar to |infobar_service|.
95 static void Create(InfoBarService* infobar_service,
96 const DevToolsWindow::InfoBarCallback& callback,
97 const base::string16& message);
98
99 private:
100 DevToolsConfirmInfoBarDelegate(
101 const DevToolsWindow::InfoBarCallback& callback,
102 const base::string16& message);
103 virtual ~DevToolsConfirmInfoBarDelegate();
104
105 virtual base::string16 GetMessageText() const OVERRIDE;
106 virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
107 virtual bool Accept() OVERRIDE;
108 virtual bool Cancel() OVERRIDE;
109
110 DevToolsWindow::InfoBarCallback callback_;
111 const base::string16 message_;
112
113 DISALLOW_COPY_AND_ASSIGN(DevToolsConfirmInfoBarDelegate);
114 };
115
116 void DevToolsConfirmInfoBarDelegate::Create(
117 InfoBarService* infobar_service,
118 const DevToolsWindow::InfoBarCallback& callback,
119 const base::string16& message) {
120 if (!infobar_service) {
121 callback.Run(false);
122 return;
123 }
124
125 infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
126 scoped_ptr<ConfirmInfoBarDelegate>(
127 new DevToolsConfirmInfoBarDelegate(callback, message))));
128 }
129
130 DevToolsConfirmInfoBarDelegate::DevToolsConfirmInfoBarDelegate(
131 const DevToolsWindow::InfoBarCallback& callback,
132 const base::string16& message)
133 : ConfirmInfoBarDelegate(),
134 callback_(callback),
135 message_(message) {
136 }
137
138 DevToolsConfirmInfoBarDelegate::~DevToolsConfirmInfoBarDelegate() {
139 if (!callback_.is_null())
140 callback_.Run(false);
141 }
142
143 base::string16 DevToolsConfirmInfoBarDelegate::GetMessageText() const {
144 return message_;
145 }
146
147 base::string16 DevToolsConfirmInfoBarDelegate::GetButtonLabel(
148 InfoBarButton button) const {
149 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
150 IDS_DEV_TOOLS_CONFIRM_ALLOW_BUTTON : IDS_DEV_TOOLS_CONFIRM_DENY_BUTTON);
151 }
152
153 bool DevToolsConfirmInfoBarDelegate::Accept() {
154 callback_.Run(true);
155 callback_.Reset();
156 return true;
157 }
158
159 bool DevToolsConfirmInfoBarDelegate::Cancel() {
160 callback_.Run(false);
161 callback_.Reset();
162 return true;
163 }
164
165 // DevToolsEventForwarder -----------------------------------------------------
166 65
167 namespace { 66 namespace {
168 67
68 typedef std::vector<DevToolsWindow*> DevToolsWindows;
69 base::LazyInstance<DevToolsWindows>::Leaky g_instances =
70 LAZY_INSTANCE_INITIALIZER;
71
169 static const char kKeyUpEventName[] = "keyup"; 72 static const char kKeyUpEventName[] = "keyup";
170 static const char kKeyDownEventName[] = "keydown"; 73 static const char kKeyDownEventName[] = "keydown";
171 74
172 } // namespace 75 } // namespace
173 76
77 // DevToolsEventForwarder -----------------------------------------------------
78
174 class DevToolsEventForwarder { 79 class DevToolsEventForwarder {
175 public: 80 public:
176 explicit DevToolsEventForwarder(DevToolsWindow* window) 81 explicit DevToolsEventForwarder(DevToolsWindow* window)
177 : devtools_window_(window) {} 82 : devtools_window_(window) {}
178 83
179 // Registers whitelisted shortcuts with the forwarder. 84 // Registers whitelisted shortcuts with the forwarder.
180 // Only registered keys will be forwarded to the DevTools frontend. 85 // Only registered keys will be forwarded to the DevTools frontend.
181 void SetWhitelistedShortcuts(const std::string& message); 86 void SetWhitelistedShortcuts(const std::string& message);
182 87
183 // Forwards a keyboard event to the DevTools frontend if it is whitelisted. 88 // Forwards a keyboard event to the DevTools frontend if it is whitelisted.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 default: 183 default:
279 return key_code; 184 return key_code;
280 } 185 }
281 } 186 }
282 187
283 // DevToolsWindow::InspectedWebContentsObserver ------------------------------- 188 // DevToolsWindow::InspectedWebContentsObserver -------------------------------
284 189
285 class DevToolsWindow::InspectedWebContentsObserver 190 class DevToolsWindow::InspectedWebContentsObserver
286 : public content::WebContentsObserver { 191 : public content::WebContentsObserver {
287 public: 192 public:
288 explicit InspectedWebContentsObserver(content::WebContents* web_contents); 193 explicit InspectedWebContentsObserver(WebContents* web_contents);
289 virtual ~InspectedWebContentsObserver(); 194 virtual ~InspectedWebContentsObserver();
290 195
291 content::WebContents* web_contents() { 196 WebContents* web_contents() {
292 return WebContentsObserver::web_contents(); 197 return WebContentsObserver::web_contents();
293 } 198 }
294 199
295 private: 200 private:
296 DISALLOW_COPY_AND_ASSIGN(InspectedWebContentsObserver); 201 DISALLOW_COPY_AND_ASSIGN(InspectedWebContentsObserver);
297 }; 202 };
298 203
299 DevToolsWindow::InspectedWebContentsObserver::InspectedWebContentsObserver( 204 DevToolsWindow::InspectedWebContentsObserver::InspectedWebContentsObserver(
300 content::WebContents* web_contents) 205 WebContents* web_contents)
301 : WebContentsObserver(web_contents) { 206 : WebContentsObserver(web_contents) {
302 } 207 }
303 208
304 DevToolsWindow::InspectedWebContentsObserver::~InspectedWebContentsObserver() { 209 DevToolsWindow::InspectedWebContentsObserver::~InspectedWebContentsObserver() {
305 } 210 }
306 211
307
308 // DevToolsWindow::FrontendWebContentsObserver --------------------------------
309
310 class DevToolsWindow::FrontendWebContentsObserver
311 : public content::WebContentsObserver {
312 public:
313 explicit FrontendWebContentsObserver(DevToolsWindow* window);
314 virtual ~FrontendWebContentsObserver();
315
316 private:
317 // contents::WebContentsObserver:
318 virtual void AboutToNavigateRenderView(
319 content::RenderViewHost* render_view_host) OVERRIDE;
320 virtual void DocumentOnLoadCompletedInMainFrame(int32 page_id) OVERRIDE;
321 virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
322
323 DevToolsWindow* devtools_window_;
324 DISALLOW_COPY_AND_ASSIGN(FrontendWebContentsObserver);
325 };
326
327 DevToolsWindow::FrontendWebContentsObserver::FrontendWebContentsObserver(
328 DevToolsWindow* devtools_window)
329 : WebContentsObserver(devtools_window->web_contents()),
330 devtools_window_(devtools_window) {
331 }
332
333 void DevToolsWindow::FrontendWebContentsObserver::WebContentsDestroyed(
334 content::WebContents* contents) {
335 delete devtools_window_;
336 }
337
338 DevToolsWindow::FrontendWebContentsObserver::~FrontendWebContentsObserver() {
339 }
340
341 void DevToolsWindow::FrontendWebContentsObserver::AboutToNavigateRenderView(
342 content::RenderViewHost* render_view_host) {
343 content::DevToolsClientHost::SetupDevToolsFrontendClient(render_view_host);
344 }
345
346 void DevToolsWindow::FrontendWebContentsObserver::
347 DocumentOnLoadCompletedInMainFrame(int32 page_id) {
348 devtools_window_->DocumentOnLoadCompletedInMainFrame();
349 }
350
351 // DevToolsWindow ------------------------------------------------------------- 212 // DevToolsWindow -------------------------------------------------------------
352 213
353 namespace {
354
355 typedef std::vector<DevToolsWindow*> DevToolsWindows;
356 base::LazyInstance<DevToolsWindows>::Leaky g_instances =
357 LAZY_INSTANCE_INITIALIZER;
358
359 static const char kFrontendHostId[] = "id";
360 static const char kFrontendHostMethod[] = "method";
361 static const char kFrontendHostParams[] = "params";
362
363 std::string SkColorToRGBAString(SkColor color) {
364 // We avoid StringPrintf because it will use locale specific formatters for
365 // the double (e.g. ',' instead of '.' in German).
366 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," +
367 base::IntToString(SkColorGetG(color)) + "," +
368 base::IntToString(SkColorGetB(color)) + "," +
369 base::DoubleToString(SkColorGetA(color) / 255.0) + ")";
370 }
371
372 base::DictionaryValue* CreateFileSystemValue(
373 DevToolsFileHelper::FileSystem file_system) {
374 base::DictionaryValue* file_system_value = new base::DictionaryValue();
375 file_system_value->SetString("fileSystemName", file_system.file_system_name);
376 file_system_value->SetString("rootURL", file_system.root_url);
377 file_system_value->SetString("fileSystemPath", file_system.file_system_path);
378 return file_system_value;
379 }
380
381 } // namespace
382
383 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp"; 214 const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp";
384 215
385 DevToolsWindow::~DevToolsWindow() { 216 DevToolsWindow::~DevToolsWindow() {
386 content::DevToolsManager::GetInstance()->ClientHostClosing(
387 frontend_host_.get());
388 UpdateBrowserToolbar(); 217 UpdateBrowserToolbar();
389 218
390 DevToolsWindows* instances = g_instances.Pointer(); 219 DevToolsWindows* instances = g_instances.Pointer();
391 DevToolsWindows::iterator it( 220 DevToolsWindows::iterator it(
392 std::find(instances->begin(), instances->end(), this)); 221 std::find(instances->begin(), instances->end(), this));
393 DCHECK(it != instances->end()); 222 DCHECK(it != instances->end());
394 instances->erase(it); 223 instances->erase(it);
395
396 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin());
397 jobs_it != indexing_jobs_.end(); ++jobs_it) {
398 jobs_it->second->Stop();
399 }
400 indexing_jobs_.clear();
401 if (device_listener_enabled_)
402 EnableRemoteDeviceCounter(false);
403 } 224 }
404 225
405 // static 226 // static
406 std::string DevToolsWindow::GetDevToolsWindowPlacementPrefKey() { 227 std::string DevToolsWindow::GetDevToolsWindowPlacementPrefKey() {
407 return std::string(prefs::kBrowserWindowPlacement) + "_" + 228 return std::string(prefs::kBrowserWindowPlacement) + "_" +
408 std::string(kDevToolsApp); 229 std::string(kDevToolsApp);
409 } 230 }
410 231
411 // static 232 // static
412 void DevToolsWindow::RegisterProfilePrefs( 233 void DevToolsWindow::RegisterProfilePrefs(
(...skipping 24 matching lines...) Expand all
437 prefs::kDevToolsPortForwardingDefaultSet, 258 prefs::kDevToolsPortForwardingDefaultSet,
438 false, 259 false,
439 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 260 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
440 registry->RegisterDictionaryPref( 261 registry->RegisterDictionaryPref(
441 prefs::kDevToolsPortForwardingConfig, 262 prefs::kDevToolsPortForwardingConfig,
442 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 263 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
443 } 264 }
444 265
445 // static 266 // static
446 DevToolsWindow* DevToolsWindow::GetDockedInstanceForInspectedTab( 267 DevToolsWindow* DevToolsWindow::GetDockedInstanceForInspectedTab(
447 content::WebContents* inspected_web_contents) { 268 WebContents* inspected_web_contents) {
448 DevToolsWindow* window = GetInstanceForInspectedRenderViewHost( 269 DevToolsWindow* window = GetInstanceForInspectedRenderViewHost(
449 inspected_web_contents->GetRenderViewHost()); 270 inspected_web_contents->GetRenderViewHost());
450 if (!window) 271 if (!window)
451 return NULL; 272 return NULL;
452 // Not yet loaded window is treated as docked, but we should not present it 273 // Not yet loaded window is treated as docked, but we should not present it
453 // until we decided on docking. 274 // until we decided on docking.
454 bool is_docked_set = window->load_state_ == kLoadCompleted || 275 bool is_docked_set = window->load_state_ == kLoadCompleted ||
455 window->load_state_ == kIsDockedSet; 276 window->load_state_ == kIsDockedSet;
456 return window->is_docked_ && is_docked_set ? window : NULL; 277 return window->is_docked_ && is_docked_set ? window : NULL;
457 } 278 }
458 279
459 // static 280 // static
460 DevToolsWindow* DevToolsWindow::GetInstanceForInspectedRenderViewHost( 281 DevToolsWindow* DevToolsWindow::GetInstanceForInspectedRenderViewHost(
461 content::RenderViewHost* inspected_rvh) { 282 content::RenderViewHost* inspected_rvh) {
462 if (!inspected_rvh || !DevToolsAgentHost::HasFor(inspected_rvh)) 283 if (!inspected_rvh || !DevToolsAgentHost::HasFor(inspected_rvh))
463 return NULL; 284 return NULL;
464 285
465 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetOrCreateFor( 286 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetOrCreateFor(
466 inspected_rvh)); 287 inspected_rvh));
467 return FindDevToolsWindow(agent.get()); 288 return FindDevToolsWindow(agent.get());
468 } 289 }
469 290
470 // static 291 // static
471 DevToolsWindow* DevToolsWindow::GetInstanceForInspectedWebContents( 292 DevToolsWindow* DevToolsWindow::GetInstanceForInspectedWebContents(
472 content::WebContents* inspected_web_contents) { 293 WebContents* inspected_web_contents) {
473 if (!inspected_web_contents) 294 if (!inspected_web_contents)
474 return NULL; 295 return NULL;
475 return GetInstanceForInspectedRenderViewHost( 296 return GetInstanceForInspectedRenderViewHost(
476 inspected_web_contents->GetRenderViewHost()); 297 inspected_web_contents->GetRenderViewHost());
477 } 298 }
478 299
479 // static 300 // static
480 bool DevToolsWindow::IsDevToolsWindow(content::RenderViewHost* window_rvh) { 301 bool DevToolsWindow::IsDevToolsWindow(content::RenderViewHost* window_rvh) {
481 return AsDevToolsWindow(window_rvh) != NULL; 302 return AsDevToolsWindow(window_rvh) != NULL;
482 } 303 }
483 304
484 // static 305 // static
485 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker( 306 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker(
486 Profile* profile, 307 Profile* profile,
487 DevToolsAgentHost* worker_agent) { 308 DevToolsAgentHost* worker_agent) {
488 DevToolsWindow* window = FindDevToolsWindow(worker_agent); 309 DevToolsWindow* window = FindDevToolsWindow(worker_agent);
489 if (!window) { 310 if (!window) {
490 window = DevToolsWindow::CreateDevToolsWindowForWorker(profile); 311 window = DevToolsWindow::CreateDevToolsWindowForWorker(profile);
491 // Will disconnect the current client host if there is one. 312 // Will disconnect the current client host if there is one.
492 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( 313 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
493 worker_agent, window->frontend_host_.get()); 314 worker_agent, window->frontend_host());
494 } 315 }
495 window->ScheduleShow(DevToolsToggleAction::Show()); 316 window->ScheduleShow(DevToolsToggleAction::Show());
496 return window; 317 return window;
497 } 318 }
498 319
499 // static 320 // static
500 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker( 321 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker(
501 Profile* profile) { 322 Profile* profile) {
502 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker")); 323 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker"));
503 return Create(profile, GURL(), NULL, true, false, false); 324 return Create(profile, GURL(), NULL, true, false, false);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 // static 375 // static
555 void DevToolsWindow::OpenExternalFrontend( 376 void DevToolsWindow::OpenExternalFrontend(
556 Profile* profile, 377 Profile* profile,
557 const std::string& frontend_url, 378 const std::string& frontend_url,
558 content::DevToolsAgentHost* agent_host) { 379 content::DevToolsAgentHost* agent_host) {
559 DevToolsWindow* window = FindDevToolsWindow(agent_host); 380 DevToolsWindow* window = FindDevToolsWindow(agent_host);
560 if (!window) { 381 if (!window) {
561 window = Create(profile, DevToolsUI::GetProxyURL(frontend_url), NULL, 382 window = Create(profile, DevToolsUI::GetProxyURL(frontend_url), NULL,
562 false, true, false); 383 false, true, false);
563 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( 384 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
564 agent_host, window->frontend_host_.get()); 385 agent_host, window->frontend_host());
565 } 386 }
566 window->ScheduleShow(DevToolsToggleAction::Show()); 387 window->ScheduleShow(DevToolsToggleAction::Show());
567 } 388 }
568 389
569 // static 390 // static
570 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow( 391 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow(
571 content::RenderViewHost* inspected_rvh, 392 content::RenderViewHost* inspected_rvh,
572 bool force_open, 393 bool force_open,
573 const DevToolsToggleAction& action) { 394 const DevToolsToggleAction& action) {
574 scoped_refptr<DevToolsAgentHost> agent( 395 scoped_refptr<DevToolsAgentHost> agent(
575 DevToolsAgentHost::GetOrCreateFor(inspected_rvh)); 396 DevToolsAgentHost::GetOrCreateFor(inspected_rvh));
576 content::DevToolsManager* manager = content::DevToolsManager::GetInstance(); 397 content::DevToolsManager* manager = content::DevToolsManager::GetInstance();
577 DevToolsWindow* window = FindDevToolsWindow(agent.get()); 398 DevToolsWindow* window = FindDevToolsWindow(agent.get());
578 bool do_open = force_open; 399 bool do_open = force_open;
579 if (!window) { 400 if (!window) {
580 Profile* profile = Profile::FromBrowserContext( 401 Profile* profile = Profile::FromBrowserContext(
581 inspected_rvh->GetProcess()->GetBrowserContext()); 402 inspected_rvh->GetProcess()->GetBrowserContext());
582 content::RecordAction( 403 content::RecordAction(
583 base::UserMetricsAction("DevTools_InspectRenderer")); 404 base::UserMetricsAction("DevTools_InspectRenderer"));
584 window = Create(profile, GURL(), inspected_rvh, false, false, true); 405 window = Create(profile, GURL(), inspected_rvh, false, false, true);
585 manager->RegisterDevToolsClientHostFor(agent.get(), 406 manager->RegisterDevToolsClientHostFor(agent.get(),
586 window->frontend_host_.get()); 407 window->frontend_host());
587 do_open = true; 408 do_open = true;
588 } 409 }
589 410
590 // Update toolbar to reflect DevTools changes. 411 // Update toolbar to reflect DevTools changes.
591 window->UpdateBrowserToolbar(); 412 window->UpdateBrowserToolbar();
592 413
593 // If window is docked and visible, we hide it on toggle. If window is 414 // If window is docked and visible, we hide it on toggle. If window is
594 // undocked, we show (activate) it. 415 // undocked, we show (activate) it.
595 if (!window->is_docked_ || do_open) 416 if (!window->is_docked_ || do_open)
596 window->ScheduleShow(action); 417 window->ScheduleShow(action);
(...skipping 12 matching lines...) Expand all
609 agent->InspectElement(x, y); 430 agent->InspectElement(x, y);
610 bool should_measure_time = FindDevToolsWindow(agent.get()) == NULL; 431 bool should_measure_time = FindDevToolsWindow(agent.get()) == NULL;
611 base::TimeTicks start_time = base::TimeTicks::Now(); 432 base::TimeTicks start_time = base::TimeTicks::Now();
612 // TODO(loislo): we should initiate DevTools window opening from within 433 // TODO(loislo): we should initiate DevTools window opening from within
613 // renderer. Otherwise, we still can hit a race condition here. 434 // renderer. Otherwise, we still can hit a race condition here.
614 DevToolsWindow* window = OpenDevToolsWindow(inspected_rvh); 435 DevToolsWindow* window = OpenDevToolsWindow(inspected_rvh);
615 if (should_measure_time) 436 if (should_measure_time)
616 window->inspect_element_start_time_ = start_time; 437 window->inspect_element_start_time_ = start_time;
617 } 438 }
618 439
619 void DevToolsWindow::InspectedContentsClosing() {
620 intercepted_page_beforeunload_ = false;
621 // This will prevent any activity after frontend is loaded.
622 action_on_load_ = DevToolsToggleAction::NoOp();
623 ignore_set_is_docked_ = true;
624 web_contents_->GetRenderViewHost()->ClosePage();
625 }
626
627 content::RenderViewHost* DevToolsWindow::GetRenderViewHost() {
628 return web_contents_->GetRenderViewHost();
629 }
630
631 const DevToolsContentsResizingStrategy& 440 const DevToolsContentsResizingStrategy&
632 DevToolsWindow::GetContentsResizingStrategy() const { 441 DevToolsWindow::GetContentsResizingStrategy() const {
633 return contents_resizing_strategy_; 442 return contents_resizing_strategy_;
634 } 443 }
635 444
636 gfx::Size DevToolsWindow::GetMinimumSize() const { 445 gfx::Size DevToolsWindow::GetMinimumSize() const {
637 const gfx::Size kMinDevToolsSize = gfx::Size(230, 100); 446 const gfx::Size kMinDevToolsSize = gfx::Size(230, 100);
638 return kMinDevToolsSize; 447 return kMinDevToolsSize;
639 } 448 }
640 449
(...skipping 22 matching lines...) Expand all
663 Browser* inspected_browser = NULL; 472 Browser* inspected_browser = NULL;
664 int inspected_tab_index = -1; 473 int inspected_tab_index = -1;
665 FindInspectedBrowserAndTabIndex(GetInspectedWebContents(), 474 FindInspectedBrowserAndTabIndex(GetInspectedWebContents(),
666 &inspected_browser, 475 &inspected_browser,
667 &inspected_tab_index); 476 &inspected_tab_index);
668 DCHECK(inspected_browser); 477 DCHECK(inspected_browser);
669 DCHECK(inspected_tab_index != -1); 478 DCHECK(inspected_tab_index != -1);
670 479
671 // Tell inspected browser to update splitter and switch to inspected panel. 480 // Tell inspected browser to update splitter and switch to inspected panel.
672 BrowserWindow* inspected_window = inspected_browser->window(); 481 BrowserWindow* inspected_window = inspected_browser->window();
673 web_contents_->SetDelegate(this); 482 web_contents()->SetDelegate(this);
674 483
675 TabStripModel* tab_strip_model = inspected_browser->tab_strip_model(); 484 TabStripModel* tab_strip_model = inspected_browser->tab_strip_model();
676 tab_strip_model->ActivateTabAt(inspected_tab_index, true); 485 tab_strip_model->ActivateTabAt(inspected_tab_index, true);
677 486
678 inspected_window->UpdateDevTools(); 487 inspected_window->UpdateDevTools();
679 web_contents_->GetView()->SetInitialFocus(); 488 web_contents()->GetView()->SetInitialFocus();
680 inspected_window->Show(); 489 inspected_window->Show();
681 // On Aura, focusing once is not enough. Do it again. 490 // On Aura, focusing once is not enough. Do it again.
682 // Note that focusing only here but not before isn't enough either. We just 491 // Note that focusing only here but not before isn't enough either. We just
683 // need to focus twice. 492 // need to focus twice.
684 web_contents_->GetView()->SetInitialFocus(); 493 web_contents()->GetView()->SetInitialFocus();
685 494
686 PrefsTabHelper::CreateForWebContents(web_contents_); 495 PrefsTabHelper::CreateForWebContents(web_contents());
687 GetRenderViewHost()->SyncRendererPrefs(); 496 web_contents()->GetRenderViewHost()->SyncRendererPrefs();
688 497
689 DoAction(action); 498 DoAction(action);
690 return; 499 return;
691 } 500 }
692 501
693 // Avoid consecutive window switching if the devtools window has been opened 502 // Avoid consecutive window switching if the devtools window has been opened
694 // and the Inspect Element shortcut is pressed in the inspected tab. 503 // and the Inspect Element shortcut is pressed in the inspected tab.
695 bool should_show_window = 504 bool should_show_window =
696 !browser_ || (action.type() != DevToolsToggleAction::kInspect); 505 !browser_ || (action.type() != DevToolsToggleAction::kInspect);
697 506
698 if (!browser_) 507 if (!browser_)
699 CreateDevToolsBrowser(); 508 CreateDevToolsBrowser();
700 509
701 if (should_show_window) { 510 if (should_show_window) {
702 browser_->window()->Show(); 511 browser_->window()->Show();
703 web_contents_->GetView()->SetInitialFocus(); 512 web_contents()->GetView()->SetInitialFocus();
704 } 513 }
705 514
706 DoAction(action); 515 DoAction(action);
707 } 516 }
708 517
709 // static 518 // static
710 bool DevToolsWindow::HandleBeforeUnload(content::WebContents* frontend_contents, 519 bool DevToolsWindow::HandleBeforeUnload(WebContents* frontend_contents,
711 bool proceed, bool* proceed_to_fire_unload) { 520 bool proceed, bool* proceed_to_fire_unload) {
712 DevToolsWindow* window = AsDevToolsWindow( 521 DevToolsWindow* window = AsDevToolsWindow(
713 frontend_contents->GetRenderViewHost()); 522 frontend_contents->GetRenderViewHost());
714 if (!window) 523 if (!window)
715 return false; 524 return false;
716 if (!window->intercepted_page_beforeunload_) 525 if (!window->intercepted_page_beforeunload_)
717 return false; 526 return false;
718 window->BeforeUnloadFired(frontend_contents, proceed, 527 window->BeforeUnloadFired(frontend_contents, proceed,
719 proceed_to_fire_unload); 528 proceed_to_fire_unload);
720 return true; 529 return true;
721 } 530 }
722 531
723 // static 532 // static
724 bool DevToolsWindow::InterceptPageBeforeUnload(content::WebContents* contents) { 533 bool DevToolsWindow::InterceptPageBeforeUnload(WebContents* contents) {
725 DevToolsWindow* window = 534 DevToolsWindow* window =
726 DevToolsWindow::GetInstanceForInspectedRenderViewHost( 535 DevToolsWindow::GetInstanceForInspectedRenderViewHost(
727 contents->GetRenderViewHost()); 536 contents->GetRenderViewHost());
728 if (!window || window->intercepted_page_beforeunload_) 537 if (!window || window->intercepted_page_beforeunload_)
729 return false; 538 return false;
730 539
731 // Not yet loaded frontend will not handle beforeunload. 540 // Not yet loaded frontend will not handle beforeunload.
732 if (window->load_state_ != kLoadCompleted) 541 if (window->load_state_ != kLoadCompleted)
733 return false; 542 return false;
734 543
735 window->intercepted_page_beforeunload_ = true; 544 window->intercepted_page_beforeunload_ = true;
736 // Handle case of devtools inspecting another devtools instance by passing 545 // Handle case of devtools inspecting another devtools instance by passing
737 // the call up to the inspecting devtools instance. 546 // the call up to the inspecting devtools instance.
738 if (!DevToolsWindow::InterceptPageBeforeUnload(window->web_contents())) { 547 if (!DevToolsWindow::InterceptPageBeforeUnload(window->web_contents())) {
739 window->web_contents()->DispatchBeforeUnload(false); 548 window->web_contents()->DispatchBeforeUnload(false);
740 } 549 }
741 return true; 550 return true;
742 } 551 }
743 552
744 // static 553 // static
745 bool DevToolsWindow::NeedsToInterceptBeforeUnload( 554 bool DevToolsWindow::NeedsToInterceptBeforeUnload(
746 content::WebContents* contents) { 555 WebContents* contents) {
747 DevToolsWindow* window = 556 DevToolsWindow* window =
748 DevToolsWindow::GetInstanceForInspectedRenderViewHost( 557 DevToolsWindow::GetInstanceForInspectedRenderViewHost(
749 contents->GetRenderViewHost()); 558 contents->GetRenderViewHost());
750 return window && !window->intercepted_page_beforeunload_; 559 return window && !window->intercepted_page_beforeunload_;
751 } 560 }
752 561
753 // static 562 // static
754 bool DevToolsWindow::HasFiredBeforeUnloadEventForDevToolsBrowser( 563 bool DevToolsWindow::HasFiredBeforeUnloadEventForDevToolsBrowser(
755 Browser* browser) { 564 Browser* browser) {
756 DCHECK(browser->is_devtools()); 565 DCHECK(browser->is_devtools());
757 // When FastUnloadController is used, devtools frontend will be detached 566 // When FastUnloadController is used, devtools frontend will be detached
758 // from the browser window at this point which means we've already fired 567 // from the browser window at this point which means we've already fired
759 // beforeunload. 568 // beforeunload.
760 if (browser->tab_strip_model()->empty()) 569 if (browser->tab_strip_model()->empty())
761 return true; 570 return true;
762 content::WebContents* contents = 571 WebContents* contents =
763 browser->tab_strip_model()->GetWebContentsAt(0); 572 browser->tab_strip_model()->GetWebContentsAt(0);
764 DevToolsWindow* window = AsDevToolsWindow(contents->GetRenderViewHost()); 573 DevToolsWindow* window = AsDevToolsWindow(contents->GetRenderViewHost());
765 if (!window) 574 if (!window)
766 return false; 575 return false;
767 return window->intercepted_page_beforeunload_; 576 return window->intercepted_page_beforeunload_;
768 } 577 }
769 578
770 // static 579 // static
771 void DevToolsWindow::OnPageCloseCanceled(content::WebContents* contents) { 580 void DevToolsWindow::OnPageCloseCanceled(WebContents* contents) {
772 DevToolsWindow *window = 581 DevToolsWindow *window =
773 DevToolsWindow::GetInstanceForInspectedRenderViewHost( 582 DevToolsWindow::GetInstanceForInspectedRenderViewHost(
774 contents->GetRenderViewHost()); 583 contents->GetRenderViewHost());
775 if (!window) 584 if (!window)
776 return; 585 return;
777 window->intercepted_page_beforeunload_ = false; 586 window->intercepted_page_beforeunload_ = false;
778 // Propagate to devtools opened on devtools if any. 587 // Propagate to devtools opened on devtools if any.
779 DevToolsWindow::OnPageCloseCanceled(window->web_contents()); 588 DevToolsWindow::OnPageCloseCanceled(window->web_contents());
780 } 589 }
781 590
782 DevToolsWindow::DevToolsWindow(Profile* profile, 591 DevToolsWindow::DevToolsWindow(Profile* profile,
783 const GURL& url, 592 const GURL& url,
784 content::RenderViewHost* inspected_rvh, 593 content::RenderViewHost* inspected_rvh,
785 bool can_dock) 594 bool can_dock)
786 : profile_(profile), 595 : DevToolsWindowBase(
596 WebContents::Create(WebContents::CreateParams(profile)), url),
787 browser_(NULL), 597 browser_(NULL),
788 is_docked_(true), 598 is_docked_(true),
789 can_dock_(can_dock), 599 can_dock_(can_dock),
790 device_listener_enabled_(false),
791 // This initialization allows external front-end to work without changes. 600 // This initialization allows external front-end to work without changes.
792 // We don't wait for docking call, but instead immediately show undocked. 601 // We don't wait for docking call, but instead immediately show undocked.
793 // Passing "dockSide=undocked" parameter ensures proper UI. 602 // Passing "dockSide=undocked" parameter ensures proper UI.
794 load_state_(can_dock ? kNotLoaded : kIsDockedSet), 603 load_state_(can_dock ? kNotLoaded : kIsDockedSet),
795 action_on_load_(DevToolsToggleAction::NoOp()), 604 action_on_load_(DevToolsToggleAction::NoOp()),
796 ignore_set_is_docked_(false), 605 ignore_set_is_docked_(false),
797 intercepted_page_beforeunload_(false), 606 intercepted_page_beforeunload_(false) {
798 weak_factory_(this) {
799 web_contents_ =
800 content::WebContents::Create(content::WebContents::CreateParams(profile));
801 frontend_contents_observer_.reset(new FrontendWebContentsObserver(this));
802 web_contents_->GetMutableRendererPrefs()->can_accept_load_drops = false;
803 607
804 // Set up delegate, so we get fully-functional window immediately. 608 // Set up delegate, so we get fully-functional window immediately.
805 // It will not appear in UI though until |load_state_ == kLoadCompleted|. 609 // It will not appear in UI though until |load_state_ == kLoadCompleted|.
806 web_contents_->SetDelegate(this); 610 web_contents()->SetDelegate(this);
807
808 web_contents_->GetController().LoadURL(url, content::Referrer(),
809 content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string());
810
811 frontend_host_.reset(content::DevToolsClientHost::CreateDevToolsFrontendHost(
812 web_contents_, this));
813 file_helper_.reset(new DevToolsFileHelper(web_contents_, profile));
814 file_system_indexer_ = new DevToolsFileSystemIndexer();
815 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
816 web_contents_);
817 611
818 g_instances.Get().push_back(this); 612 g_instances.Get().push_back(this);
819 613
820 // Wipe out page icon so that the default application icon is used.
821 content::NavigationEntry* entry =
822 web_contents_->GetController().GetActiveEntry();
823 entry->GetFavicon().image = gfx::Image();
824 entry->GetFavicon().valid = true;
825
826 // Register on-load actions.
827 registrar_.Add(
828 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
829 content::Source<ThemeService>(
830 ThemeServiceFactory::GetForProfile(profile_)));
831
832 // There is no inspected_rvh in case of shared workers. 614 // There is no inspected_rvh in case of shared workers.
833 if (inspected_rvh) 615 if (inspected_rvh)
834 inspected_contents_observer_.reset(new InspectedWebContentsObserver( 616 inspected_contents_observer_.reset(new InspectedWebContentsObserver(
835 content::WebContents::FromRenderViewHost(inspected_rvh))); 617 content::WebContents::FromRenderViewHost(inspected_rvh)));
836
837 embedder_message_dispatcher_.reset(
838 DevToolsEmbedderMessageDispatcher::createForDevToolsFrontend(this));
839 event_forwarder_.reset(new DevToolsEventForwarder(this)); 618 event_forwarder_.reset(new DevToolsEventForwarder(this));
840 } 619 }
841 620
842 // static 621 // static
843 DevToolsWindow* DevToolsWindow::Create( 622 DevToolsWindow* DevToolsWindow::Create(
844 Profile* profile, 623 Profile* profile,
845 const GURL& frontend_url, 624 const GURL& frontend_url,
846 content::RenderViewHost* inspected_rvh, 625 content::RenderViewHost* inspected_rvh,
847 bool shared_worker_frontend, 626 bool shared_worker_frontend,
848 bool external_frontend, 627 bool external_frontend,
849 bool can_dock) { 628 bool can_dock) {
850 if (inspected_rvh) { 629 if (inspected_rvh) {
851 // Check for a place to dock. 630 // Check for a place to dock.
852 Browser* browser = NULL; 631 Browser* browser = NULL;
853 int tab; 632 int tab;
854 content::WebContents* inspected_web_contents = 633 WebContents* inspected_web_contents =
855 content::WebContents::FromRenderViewHost(inspected_rvh); 634 content::WebContents::FromRenderViewHost(inspected_rvh);
856 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents, 635 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents,
857 &browser, &tab) || 636 &browser, &tab) ||
858 inspected_rvh->GetMainFrame()->IsCrossProcessSubframe() || 637 inspected_rvh->GetMainFrame()->IsCrossProcessSubframe() ||
859 browser->is_type_popup()) { 638 browser->is_type_popup()) {
860 can_dock = false; 639 can_dock = false;
861 } 640 }
862 } 641 }
863 642
864 // Create WebContents with devtools. 643 // Create WebContents with devtools.
865 GURL url(GetDevToolsURL(profile, frontend_url, 644 GURL url(GetDevToolsURL(profile, frontend_url,
866 shared_worker_frontend, 645 shared_worker_frontend,
867 external_frontend, 646 external_frontend,
868 can_dock)); 647 can_dock));
869 return new DevToolsWindow(profile, url, inspected_rvh, can_dock); 648 return new DevToolsWindow(profile, url, inspected_rvh, can_dock);
870 } 649 }
871 650
872 // static 651 // static
873 GURL DevToolsWindow::GetDevToolsURL(Profile* profile, 652 GURL DevToolsWindow::GetDevToolsURL(Profile* profile,
874 const GURL& base_url, 653 const GURL& base_url,
875 bool shared_worker_frontend, 654 bool shared_worker_frontend,
876 bool external_frontend, 655 bool external_frontend,
877 bool can_dock) { 656 bool can_dock) {
878 if (base_url.SchemeIs("data"))
Vladislav Kaznacheev 2014/04/29 11:10:05 Why remove this? This has been added in https://co
pfeldman 2014/04/29 11:28:11 It is good we have you remembering it. How about a
879 return base_url;
880
881 std::string frontend_url( 657 std::string frontend_url(
882 base_url.is_empty() ? chrome::kChromeUIDevToolsURL : base_url.spec()); 658 base_url.is_empty() ? chrome::kChromeUIDevToolsURL : base_url.spec());
883 ThemeService* tp = ThemeServiceFactory::GetForProfile(profile);
884 DCHECK(tp);
885 std::string url_string( 659 std::string url_string(
886 frontend_url + 660 frontend_url +
887 ((frontend_url.find("?") == std::string::npos) ? "?" : "&") + 661 ((frontend_url.find("?") == std::string::npos) ? "?" : "&"));
888 "dockSide=undocked" + // TODO(dgozman): remove this support in M38.
889 "&toolbarColor=" +
890 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_TOOLBAR)) +
891 "&textColor=" +
892 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT)));
893 if (shared_worker_frontend) 662 if (shared_worker_frontend)
894 url_string += "&isSharedWorker=true"; 663 url_string += "&isSharedWorker=true";
895 if (external_frontend) 664 if (external_frontend)
896 url_string += "&remoteFrontend=true"; 665 url_string += "&remoteFrontend=true";
897 if (can_dock) 666 if (can_dock)
898 url_string += "&can_dock=true"; 667 url_string += "&can_dock=true";
899 if (CommandLine::ForCurrentProcess()->HasSwitch(
900 switches::kEnableDevToolsExperiments))
901 url_string += "&experiments=true";
902 url_string += "&updateAppcache";
903 return GURL(url_string); 668 return GURL(url_string);
904 } 669 }
905 670
906 // static 671 // static
907 DevToolsWindow* DevToolsWindow::FindDevToolsWindow( 672 DevToolsWindow* DevToolsWindow::FindDevToolsWindow(
908 DevToolsAgentHost* agent_host) { 673 DevToolsAgentHost* agent_host) {
909 DevToolsWindows* instances = g_instances.Pointer(); 674 DevToolsWindows* instances = g_instances.Pointer();
910 content::DevToolsManager* manager = content::DevToolsManager::GetInstance(); 675 content::DevToolsManager* manager = content::DevToolsManager::GetInstance();
911 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end(); 676 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end();
912 ++it) { 677 ++it) {
913 if (manager->GetDevToolsAgentHostFor((*it)->frontend_host_.get()) == 678 if (manager->GetDevToolsAgentHostFor((*it)->frontend_host()) ==
914 agent_host) 679 agent_host)
915 return *it; 680 return *it;
916 } 681 }
917 return NULL; 682 return NULL;
918 } 683 }
919 684
920 // static 685 // static
921 DevToolsWindow* DevToolsWindow::AsDevToolsWindow( 686 DevToolsWindow* DevToolsWindow::AsDevToolsWindow(
922 content::RenderViewHost* window_rvh) { 687 content::RenderViewHost* window_rvh) {
923 if (g_instances == NULL) 688 if (g_instances == NULL)
924 return NULL; 689 return NULL;
925 DevToolsWindows* instances = g_instances.Pointer(); 690 DevToolsWindows* instances = g_instances.Pointer();
926 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end(); 691 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end();
927 ++it) { 692 ++it) {
928 if ((*it)->web_contents_->GetRenderViewHost() == window_rvh) 693 if ((*it)->web_contents()->GetRenderViewHost() == window_rvh)
929 return *it; 694 return *it;
930 } 695 }
931 return NULL; 696 return NULL;
932 } 697 }
933 698
934 void DevToolsWindow::Observe(int type, 699 void DevToolsWindow::InspectedContentsClosing() {
935 const content::NotificationSource& source, 700 intercepted_page_beforeunload_ = false;
936 const content::NotificationDetails& details) { 701 // This will prevent any activity after frontend is loaded.
937 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); 702 action_on_load_ = DevToolsToggleAction::NoOp();
938 UpdateTheme(); 703 ignore_set_is_docked_ = true;
704 DevToolsWindowBase::InspectedContentsClosing();
939 } 705 }
940 706
941 content::WebContents* DevToolsWindow::OpenURLFromTab( 707 WebContents* DevToolsWindow::OpenURLFromTab(
942 content::WebContents* source, 708 WebContents* source,
943 const content::OpenURLParams& params) { 709 const content::OpenURLParams& params) {
944 DCHECK(source == web_contents_); 710 DCHECK(source == web_contents());
945 if (!params.url.SchemeIs(content::kChromeDevToolsScheme)) { 711 if (!params.url.SchemeIs(content::kChromeDevToolsScheme)) {
946 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 712 WebContents* inspected_web_contents = GetInspectedWebContents();
947 return inspected_web_contents ? 713 return inspected_web_contents ?
948 inspected_web_contents->OpenURL(params) : NULL; 714 inspected_web_contents->OpenURL(params) : NULL;
949 } 715 }
950 716
951 content::DevToolsManager* manager = content::DevToolsManager::GetInstance(); 717 content::DevToolsManager* manager = content::DevToolsManager::GetInstance();
952 scoped_refptr<DevToolsAgentHost> agent_host( 718 scoped_refptr<DevToolsAgentHost> agent_host(
953 manager->GetDevToolsAgentHostFor(frontend_host_.get())); 719 manager->GetDevToolsAgentHostFor(frontend_host()));
954 if (!agent_host.get()) 720 if (!agent_host.get())
955 return NULL; 721 return NULL;
956 manager->ClientHostClosing(frontend_host_.get()); 722 manager->ClientHostClosing(frontend_host());
957 manager->RegisterDevToolsClientHostFor(agent_host.get(), 723 manager->RegisterDevToolsClientHostFor(agent_host.get(),
958 frontend_host_.get()); 724 frontend_host());
959 725
960 content::NavigationController::LoadURLParams load_url_params(params.url); 726 content::NavigationController::LoadURLParams load_url_params(params.url);
961 web_contents_->GetController().LoadURLWithParams(load_url_params); 727 web_contents()->GetController().LoadURLWithParams(load_url_params);
962 return web_contents_; 728 return web_contents();
963 } 729 }
964 730
965 void DevToolsWindow::AddNewContents(content::WebContents* source, 731 void DevToolsWindow::ActivateContents(WebContents* contents) {
966 content::WebContents* new_contents, 732 if (is_docked_) {
733 WebContents* inspected_tab = this->GetInspectedWebContents();
Vladislav Kaznacheev 2014/04/29 11:10:05 How does explicit 'this' help us here?
pfeldman 2014/04/29 11:28:11 This code just moved - for some reason it was defi
734 inspected_tab->GetDelegate()->ActivateContents(inspected_tab);
735 } else {
736 browser_->window()->Activate();
737 }
738 }
739
740 void DevToolsWindow::AddNewContents(WebContents* source,
741 WebContents* new_contents,
967 WindowOpenDisposition disposition, 742 WindowOpenDisposition disposition,
968 const gfx::Rect& initial_pos, 743 const gfx::Rect& initial_pos,
969 bool user_gesture, 744 bool user_gesture,
970 bool* was_blocked) { 745 bool* was_blocked) {
971 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 746 WebContents* inspected_web_contents = GetInspectedWebContents();
972 if (inspected_web_contents) { 747 if (inspected_web_contents) {
973 inspected_web_contents->GetDelegate()->AddNewContents( 748 inspected_web_contents->GetDelegate()->AddNewContents(
974 source, new_contents, disposition, initial_pos, user_gesture, 749 source, new_contents, disposition, initial_pos, user_gesture,
975 was_blocked); 750 was_blocked);
976 } 751 }
977 } 752 }
978 753
979 void DevToolsWindow::CloseContents(content::WebContents* source) { 754 void DevToolsWindow::CloseContents(WebContents* source) {
980 CHECK(is_docked_); 755 CHECK(is_docked_);
981 // This will prevent any activity after frontend is loaded. 756 // This will prevent any activity after frontend is loaded.
982 action_on_load_ = DevToolsToggleAction::NoOp(); 757 action_on_load_ = DevToolsToggleAction::NoOp();
983 ignore_set_is_docked_ = true; 758 ignore_set_is_docked_ = true;
984 // Update dev tools to reflect removed dev tools window. 759 // Update dev tools to reflect removed dev tools window.
985 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); 760 BrowserWindow* inspected_window = GetInspectedBrowserWindow();
986 if (inspected_window) 761 if (inspected_window)
987 inspected_window->UpdateDevTools(); 762 inspected_window->UpdateDevTools();
988 // In case of docked web_contents_, we own it so delete here. 763 // In case of docked web_contents(), we own it so delete here.
989 // Embedding DevTools window will be deleted as a result of 764 // Embedding DevTools window will be deleted as a result of
990 // WebContentsDestroyed callback. 765 // WebContentsDestroyed callback.
991 delete web_contents_; 766 delete web_contents();
992 } 767 }
993 768
994 void DevToolsWindow::ContentsZoomChange(bool zoom_in) { 769 void DevToolsWindow::ContentsZoomChange(bool zoom_in) {
995 DCHECK(is_docked_); 770 DCHECK(is_docked_);
996 chrome_page_zoom::Zoom(web_contents(), 771 chrome_page_zoom::Zoom(web_contents(),
997 zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); 772 zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT);
998 } 773 }
999 774
1000 void DevToolsWindow::BeforeUnloadFired(content::WebContents* tab, 775 void DevToolsWindow::BeforeUnloadFired(WebContents* tab,
1001 bool proceed, 776 bool proceed,
1002 bool* proceed_to_fire_unload) { 777 bool* proceed_to_fire_unload) {
1003 if (!intercepted_page_beforeunload_) { 778 if (!intercepted_page_beforeunload_) {
1004 // Docked devtools window closed directly. 779 // Docked devtools window closed directly.
1005 if (proceed) { 780 if (proceed) {
1006 content::DevToolsManager::GetInstance()->ClientHostClosing( 781 content::DevToolsManager::GetInstance()->ClientHostClosing(
1007 frontend_host_.get()); 782 frontend_host());
1008 } 783 }
1009 *proceed_to_fire_unload = proceed; 784 *proceed_to_fire_unload = proceed;
1010 } else { 785 } else {
1011 // Inspected page is attempting to close. 786 // Inspected page is attempting to close.
1012 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 787 WebContents* inspected_web_contents = GetInspectedWebContents();
1013 if (proceed) { 788 if (proceed) {
1014 inspected_web_contents->DispatchBeforeUnload(false); 789 inspected_web_contents->DispatchBeforeUnload(false);
1015 } else { 790 } else {
1016 bool should_proceed; 791 bool should_proceed;
1017 inspected_web_contents->GetDelegate()->BeforeUnloadFired( 792 inspected_web_contents->GetDelegate()->BeforeUnloadFired(
1018 inspected_web_contents, false, &should_proceed); 793 inspected_web_contents, false, &should_proceed);
1019 DCHECK(!should_proceed); 794 DCHECK(!should_proceed);
1020 } 795 }
1021 *proceed_to_fire_unload = false; 796 *proceed_to_fire_unload = false;
1022 } 797 }
1023 } 798 }
1024 799
1025 bool DevToolsWindow::PreHandleKeyboardEvent( 800 bool DevToolsWindow::PreHandleKeyboardEvent(
1026 content::WebContents* source, 801 WebContents* source,
1027 const content::NativeWebKeyboardEvent& event, 802 const content::NativeWebKeyboardEvent& event,
1028 bool* is_keyboard_shortcut) { 803 bool* is_keyboard_shortcut) {
1029 if (is_docked_) { 804 if (is_docked_) {
1030 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); 805 BrowserWindow* inspected_window = GetInspectedBrowserWindow();
1031 if (inspected_window) { 806 if (inspected_window) {
1032 return inspected_window->PreHandleKeyboardEvent(event, 807 return inspected_window->PreHandleKeyboardEvent(event,
1033 is_keyboard_shortcut); 808 is_keyboard_shortcut);
1034 } 809 }
1035 } 810 }
1036 return false; 811 return false;
1037 } 812 }
1038 813
1039 void DevToolsWindow::HandleKeyboardEvent( 814 void DevToolsWindow::HandleKeyboardEvent(
1040 content::WebContents* source, 815 WebContents* source,
1041 const content::NativeWebKeyboardEvent& event) { 816 const content::NativeWebKeyboardEvent& event) {
1042 if (is_docked_) { 817 if (is_docked_) {
1043 if (event.windowsKeyCode == 0x08) { 818 if (event.windowsKeyCode == 0x08) {
1044 // Do not navigate back in history on Windows (http://crbug.com/74156). 819 // Do not navigate back in history on Windows (http://crbug.com/74156).
dgozman 2014/04/29 11:32:26 I'm not sure whether this belongs to base.
pfeldman 2014/04/29 11:37:12 That's why it is not there.
1045 return; 820 return;
1046 } 821 }
1047 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); 822 BrowserWindow* inspected_window = GetInspectedBrowserWindow();
1048 if (inspected_window) 823 if (inspected_window)
1049 inspected_window->HandleKeyboardEvent(event); 824 inspected_window->HandleKeyboardEvent(event);
1050 } 825 }
1051 } 826 }
1052 827
1053 content::JavaScriptDialogManager* DevToolsWindow::GetJavaScriptDialogManager() { 828 content::JavaScriptDialogManager* DevToolsWindow::GetJavaScriptDialogManager() {
1054 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 829 WebContents* inspected_web_contents = GetInspectedWebContents();
1055 return (inspected_web_contents && inspected_web_contents->GetDelegate()) ? 830 return (inspected_web_contents && inspected_web_contents->GetDelegate()) ?
1056 inspected_web_contents->GetDelegate()->GetJavaScriptDialogManager() : 831 inspected_web_contents->GetDelegate()->GetJavaScriptDialogManager() :
1057 content::WebContentsDelegate::GetJavaScriptDialogManager(); 832 content::WebContentsDelegate::GetJavaScriptDialogManager();
1058 } 833 }
1059 834
1060 content::ColorChooser* DevToolsWindow::OpenColorChooser( 835 content::ColorChooser* DevToolsWindow::OpenColorChooser(
1061 content::WebContents* web_contents, 836 WebContents* web_contents,
1062 SkColor initial_color, 837 SkColor initial_color,
1063 const std::vector<content::ColorSuggestion>& suggestions) { 838 const std::vector<content::ColorSuggestion>& suggestions) {
1064 return chrome::ShowColorChooser(web_contents, initial_color); 839 return chrome::ShowColorChooser(web_contents, initial_color);
1065 } 840 }
1066 841
1067 void DevToolsWindow::RunFileChooser(content::WebContents* web_contents, 842 void DevToolsWindow::RunFileChooser(WebContents* web_contents,
1068 const content::FileChooserParams& params) { 843 const content::FileChooserParams& params) {
1069 FileSelectHelper::RunFileChooser(web_contents, params); 844 FileSelectHelper::RunFileChooser(web_contents, params);
1070 } 845 }
1071 846
1072 void DevToolsWindow::WebContentsFocused(content::WebContents* contents) { 847 void DevToolsWindow::WebContentsFocused(WebContents* contents) {
1073 Browser* inspected_browser = NULL; 848 Browser* inspected_browser = NULL;
1074 int inspected_tab_index = -1; 849 int inspected_tab_index = -1;
1075 if (is_docked_ && FindInspectedBrowserAndTabIndex(GetInspectedWebContents(), 850 if (is_docked_ && FindInspectedBrowserAndTabIndex(GetInspectedWebContents(),
1076 &inspected_browser, 851 &inspected_browser,
1077 &inspected_tab_index)) 852 &inspected_tab_index))
1078 inspected_browser->window()->WebContentsFocused(contents); 853 inspected_browser->window()->WebContentsFocused(contents);
1079 } 854 }
1080 855
1081 bool DevToolsWindow::PreHandleGestureEvent( 856 bool DevToolsWindow::PreHandleGestureEvent(
dgozman 2014/04/29 11:32:26 This is DevToolsWindowBase functionality.
pfeldman 2014/04/29 11:37:12 It can't be - DevToolsWindowBase is not a WebConte
1082 content::WebContents* source, 857 WebContents* source,
1083 const blink::WebGestureEvent& event) { 858 const blink::WebGestureEvent& event) {
1084 // Disable pinch zooming. 859 // Disable pinch zooming.
1085 return event.type == blink::WebGestureEvent::GesturePinchBegin || 860 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
1086 event.type == blink::WebGestureEvent::GesturePinchUpdate || 861 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
1087 event.type == blink::WebGestureEvent::GesturePinchEnd; 862 event.type == blink::WebGestureEvent::GesturePinchEnd;
1088 } 863 }
1089 864
1090 void DevToolsWindow::DispatchOnEmbedder(const std::string& message) {
1091 std::string method;
1092 base::ListValue empty_params;
1093 base::ListValue* params = &empty_params;
1094
1095 base::DictionaryValue* dict = NULL;
1096 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message));
1097 if (!parsed_message ||
1098 !parsed_message->GetAsDictionary(&dict) ||
1099 !dict->GetString(kFrontendHostMethod, &method) ||
1100 (dict->HasKey(kFrontendHostParams) &&
1101 !dict->GetList(kFrontendHostParams, &params))) {
1102 LOG(ERROR) << "Invalid message was sent to embedder: " << message;
1103 return;
1104 }
1105
1106 int id = 0;
1107 dict->GetInteger(kFrontendHostId, &id);
1108
1109 std::string error;
1110 embedder_message_dispatcher_->Dispatch(method, params, &error);
1111 if (id) {
1112 scoped_ptr<base::Value> id_value(base::Value::CreateIntegerValue(id));
1113 scoped_ptr<base::Value> error_value(base::Value::CreateStringValue(error));
1114 CallClientFunction("InspectorFrontendAPI.embedderMessageAck",
1115 id_value.get(), error_value.get(), NULL);
1116 }
1117 }
1118
1119 void DevToolsWindow::ActivateWindow() { 865 void DevToolsWindow::ActivateWindow() {
dgozman 2014/04/29 11:32:26 Having this functionality for in-tab DevTools woul
pfeldman 2014/04/29 11:37:12 That's why it is implemented there.
1120 if (is_docked_ && GetInspectedBrowserWindow()) 866 if (is_docked_ && GetInspectedBrowserWindow())
1121 web_contents_->GetView()->Focus(); 867 web_contents()->GetView()->Focus();
1122 else if (!is_docked_ && !browser_->window()->IsActive()) 868 else if (!is_docked_ && !browser_->window()->IsActive())
1123 browser_->window()->Activate(); 869 browser_->window()->Activate();
1124 } 870 }
1125 871
1126 void DevToolsWindow::ActivateContents(content::WebContents* contents) {
1127 if (is_docked_) {
1128 content::WebContents* inspected_tab = this->GetInspectedWebContents();
1129 inspected_tab->GetDelegate()->ActivateContents(inspected_tab);
1130 } else {
1131 browser_->window()->Activate();
1132 }
1133 }
1134
1135 void DevToolsWindow::CloseWindow() { 872 void DevToolsWindow::CloseWindow() {
1136 DCHECK(is_docked_); 873 DCHECK(is_docked_);
1137 // This will prevent any activity after frontend is loaded. 874 // This will prevent any activity after frontend is loaded.
1138 action_on_load_ = DevToolsToggleAction::NoOp(); 875 action_on_load_ = DevToolsToggleAction::NoOp();
1139 ignore_set_is_docked_ = true; 876 ignore_set_is_docked_ = true;
1140 web_contents_->DispatchBeforeUnload(false); 877 web_contents()->DispatchBeforeUnload(false);
1141 } 878 }
1142 879
1143 void DevToolsWindow::SetContentsInsets( 880 void DevToolsWindow::SetContentsInsets(
1144 int top, int left, int bottom, int right) { 881 int top, int left, int bottom, int right) {
1145 gfx::Insets insets(top, left, bottom, right); 882 gfx::Insets insets(top, left, bottom, right);
1146 SetContentsResizingStrategy(insets, contents_resizing_strategy_.min_size()); 883 SetContentsResizingStrategy(insets, contents_resizing_strategy_.min_size());
1147 } 884 }
1148 885
1149 void DevToolsWindow::SetContentsResizingStrategy( 886 void DevToolsWindow::SetContentsResizingStrategy(
1150 const gfx::Insets& insets, const gfx::Size& min_size) { 887 const gfx::Insets& insets, const gfx::Size& min_size) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 } 952 }
1216 953
1217 if (dock_requested == was_docked) 954 if (dock_requested == was_docked)
1218 return; 955 return;
1219 956
1220 if (dock_requested && !was_docked) { 957 if (dock_requested && !was_docked) {
1221 // Detach window from the external devtools browser. It will lead to 958 // Detach window from the external devtools browser. It will lead to
1222 // the browser object's close and delete. Remove observer first. 959 // the browser object's close and delete. Remove observer first.
1223 TabStripModel* tab_strip_model = browser_->tab_strip_model(); 960 TabStripModel* tab_strip_model = browser_->tab_strip_model();
1224 tab_strip_model->DetachWebContentsAt( 961 tab_strip_model->DetachWebContentsAt(
1225 tab_strip_model->GetIndexOfWebContents(web_contents_)); 962 tab_strip_model->GetIndexOfWebContents(web_contents()));
1226 browser_ = NULL; 963 browser_ = NULL;
1227 } else if (!dock_requested && was_docked) { 964 } else if (!dock_requested && was_docked) {
1228 // Update inspected window to hide split and reset it. 965 // Update inspected window to hide split and reset it.
1229 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); 966 BrowserWindow* inspected_window = GetInspectedBrowserWindow();
1230 if (inspected_window) 967 if (inspected_window)
1231 inspected_window->UpdateDevTools(); 968 inspected_window->UpdateDevTools();
1232 } 969 }
1233 970
1234 Show(DevToolsToggleAction::Show()); 971 Show(DevToolsToggleAction::Show());
1235 } 972 }
1236 973
1237 void DevToolsWindow::OpenInNewTab(const std::string& url) { 974 void DevToolsWindow::OpenInNewTab(const std::string& url) {
1238 content::OpenURLParams params( 975 content::OpenURLParams params(
1239 GURL(url), content::Referrer(), NEW_FOREGROUND_TAB, 976 GURL(url), content::Referrer(), NEW_FOREGROUND_TAB,
1240 content::PAGE_TRANSITION_LINK, false); 977 content::PAGE_TRANSITION_LINK, false);
1241 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 978 WebContents* inspected_web_contents = GetInspectedWebContents();
1242 if (inspected_web_contents) { 979 if (inspected_web_contents) {
1243 inspected_web_contents->OpenURL(params); 980 inspected_web_contents->OpenURL(params);
1244 } else { 981 } else {
1245 chrome::HostDesktopType host_desktop_type; 982 chrome::HostDesktopType host_desktop_type;
1246 if (browser_) { 983 if (browser_) {
1247 host_desktop_type = browser_->host_desktop_type(); 984 host_desktop_type = browser_->host_desktop_type();
1248 } else { 985 } else {
1249 // There should always be a browser when there are no inspected web 986 // There should always be a browser when there are no inspected web
1250 // contents. 987 // contents.
1251 NOTREACHED(); 988 NOTREACHED();
1252 host_desktop_type = chrome::GetActiveDesktop(); 989 host_desktop_type = chrome::GetActiveDesktop();
1253 } 990 }
1254 991
1255 const BrowserList* browser_list = 992 const BrowserList* browser_list =
1256 BrowserList::GetInstance(host_desktop_type); 993 BrowserList::GetInstance(host_desktop_type);
1257 for (BrowserList::const_iterator it = browser_list->begin(); 994 for (BrowserList::const_iterator it = browser_list->begin();
1258 it != browser_list->end(); ++it) { 995 it != browser_list->end(); ++it) {
1259 if ((*it)->type() == Browser::TYPE_TABBED) { 996 if ((*it)->type() == Browser::TYPE_TABBED) {
1260 (*it)->OpenURL(params); 997 (*it)->OpenURL(params);
1261 break; 998 break;
1262 } 999 }
1263 } 1000 }
1264 } 1001 }
1265 } 1002 }
1266 1003
1267 void DevToolsWindow::SaveToFile(const std::string& url, 1004 void DevToolsWindow::SetWhitelistedShortcuts(
1268 const std::string& content, 1005 const std::string& message) {
1269 bool save_as) { 1006 event_forwarder_->SetWhitelistedShortcuts(message);
1270 file_helper_->Save(url, content, save_as,
1271 base::Bind(&DevToolsWindow::FileSavedAs,
1272 weak_factory_.GetWeakPtr(), url),
1273 base::Bind(&DevToolsWindow::CanceledFileSaveAs,
1274 weak_factory_.GetWeakPtr(), url));
1275 }
1276
1277 void DevToolsWindow::AppendToFile(const std::string& url,
1278 const std::string& content) {
1279 file_helper_->Append(url, content,
1280 base::Bind(&DevToolsWindow::AppendedTo,
1281 weak_factory_.GetWeakPtr(), url));
1282 }
1283
1284 void DevToolsWindow::RequestFileSystems() {
1285 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1286 file_helper_->RequestFileSystems(base::Bind(
1287 &DevToolsWindow::FileSystemsLoaded, weak_factory_.GetWeakPtr()));
1288 }
1289
1290 void DevToolsWindow::AddFileSystem() {
1291 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1292 file_helper_->AddFileSystem(
1293 base::Bind(&DevToolsWindow::FileSystemAdded, weak_factory_.GetWeakPtr()),
1294 base::Bind(&DevToolsWindow::ShowDevToolsConfirmInfoBar,
1295 weak_factory_.GetWeakPtr()));
1296 }
1297
1298 void DevToolsWindow::RemoveFileSystem(const std::string& file_system_path) {
1299 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1300 file_helper_->RemoveFileSystem(file_system_path);
1301 base::StringValue file_system_path_value(file_system_path);
1302 CallClientFunction("InspectorFrontendAPI.fileSystemRemoved",
1303 &file_system_path_value, NULL, NULL);
1304 }
1305
1306 void DevToolsWindow::UpgradeDraggedFileSystemPermissions(
1307 const std::string& file_system_url) {
1308 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1309 file_helper_->UpgradeDraggedFileSystemPermissions(
1310 file_system_url,
1311 base::Bind(&DevToolsWindow::FileSystemAdded, weak_factory_.GetWeakPtr()),
1312 base::Bind(&DevToolsWindow::ShowDevToolsConfirmInfoBar,
1313 weak_factory_.GetWeakPtr()));
1314 }
1315
1316 void DevToolsWindow::IndexPath(int request_id,
1317 const std::string& file_system_path) {
1318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1319 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1320 if (!file_helper_->IsFileSystemAdded(file_system_path)) {
1321 IndexingDone(request_id, file_system_path);
1322 return;
1323 }
1324 indexing_jobs_[request_id] =
1325 scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob>(
1326 file_system_indexer_->IndexPath(
1327 file_system_path,
1328 Bind(&DevToolsWindow::IndexingTotalWorkCalculated,
1329 weak_factory_.GetWeakPtr(),
1330 request_id,
1331 file_system_path),
1332 Bind(&DevToolsWindow::IndexingWorked,
1333 weak_factory_.GetWeakPtr(),
1334 request_id,
1335 file_system_path),
1336 Bind(&DevToolsWindow::IndexingDone,
1337 weak_factory_.GetWeakPtr(),
1338 request_id,
1339 file_system_path)));
1340 }
1341
1342 void DevToolsWindow::StopIndexing(int request_id) {
1343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1344 IndexingJobsMap::iterator it = indexing_jobs_.find(request_id);
1345 if (it == indexing_jobs_.end())
1346 return;
1347 it->second->Stop();
1348 indexing_jobs_.erase(it);
1349 }
1350
1351 void DevToolsWindow::SearchInPath(int request_id,
1352 const std::string& file_system_path,
1353 const std::string& query) {
1354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1355 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme));
1356 if (!file_helper_->IsFileSystemAdded(file_system_path)) {
1357 SearchCompleted(request_id, file_system_path, std::vector<std::string>());
1358 return;
1359 }
1360 file_system_indexer_->SearchInPath(file_system_path,
1361 query,
1362 Bind(&DevToolsWindow::SearchCompleted,
1363 weak_factory_.GetWeakPtr(),
1364 request_id,
1365 file_system_path));
1366 }
1367
1368 void DevToolsWindow::ZoomIn() {
1369 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_IN);
1370 }
1371
1372 void DevToolsWindow::ZoomOut() {
1373 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_OUT);
1374 }
1375
1376 void DevToolsWindow::ResetZoom() {
1377 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_RESET);
1378 }
1379
1380 void DevToolsWindow::OpenUrlOnRemoteDeviceAndInspect(
1381 const std::string& browser_id,
1382 const std::string& url) {
1383 if (remote_targets_handler_)
1384 remote_targets_handler_->OpenAndInspect(browser_id, url, profile_);
1385 }
1386
1387 void DevToolsWindow::StartRemoteDevicesListener() {
1388 remote_targets_handler_ = DevToolsRemoteTargetsUIHandler::CreateForAdb(
1389 base::Bind(&DevToolsWindow::PopulateRemoteDevices,
1390 base::Unretained(this)),
1391 profile_);
1392 }
1393
1394 void DevToolsWindow::StopRemoteDevicesListener() {
1395 remote_targets_handler_.reset();
1396 }
1397
1398 void DevToolsWindow::EnableRemoteDeviceCounter(bool enable) {
1399 DevToolsAndroidBridge* adb_bridge =
1400 DevToolsAndroidBridge::Factory::GetForProfile(profile_);
1401 if (!adb_bridge)
1402 return;
1403
1404 DCHECK(device_listener_enabled_ != enable);
1405 device_listener_enabled_ = enable;
1406 if (enable)
1407 adb_bridge->AddDeviceCountListener(this);
1408 else
1409 adb_bridge->RemoveDeviceCountListener(this);
1410 }
1411
1412 void DevToolsWindow::DeviceCountChanged(int count) {
1413 base::FundamentalValue value(count);
1414 CallClientFunction(
1415 "InspectorFrontendAPI.setRemoteDeviceCount", &value, NULL, NULL);
1416 }
1417
1418 void DevToolsWindow::PopulateRemoteDevices(
1419 const std::string& source,
1420 scoped_ptr<base::ListValue> targets) {
1421 CallClientFunction(
1422 "InspectorFrontendAPI.populateRemoteDevices", targets.get(), NULL, NULL);
1423 }
1424
1425 void DevToolsWindow::FileSavedAs(const std::string& url) {
1426 base::StringValue url_value(url);
1427 CallClientFunction("InspectorFrontendAPI.savedURL", &url_value, NULL, NULL);
1428 }
1429
1430 void DevToolsWindow::CanceledFileSaveAs(const std::string& url) {
1431 base::StringValue url_value(url);
1432 CallClientFunction("InspectorFrontendAPI.canceledSaveURL",
1433 &url_value, NULL, NULL);
1434 }
1435
1436 void DevToolsWindow::AppendedTo(const std::string& url) {
1437 base::StringValue url_value(url);
1438 CallClientFunction("InspectorFrontendAPI.appendedToURL", &url_value, NULL,
1439 NULL);
1440 }
1441
1442 void DevToolsWindow::FileSystemsLoaded(
1443 const std::vector<DevToolsFileHelper::FileSystem>& file_systems) {
1444 base::ListValue file_systems_value;
1445 for (size_t i = 0; i < file_systems.size(); ++i)
1446 file_systems_value.Append(CreateFileSystemValue(file_systems[i]));
1447 CallClientFunction("InspectorFrontendAPI.fileSystemsLoaded",
1448 &file_systems_value, NULL, NULL);
1449 }
1450
1451 void DevToolsWindow::FileSystemAdded(
1452 const DevToolsFileHelper::FileSystem& file_system) {
1453 scoped_ptr<base::StringValue> error_string_value(
1454 new base::StringValue(std::string()));
1455 scoped_ptr<base::DictionaryValue> file_system_value;
1456 if (!file_system.file_system_path.empty())
1457 file_system_value.reset(CreateFileSystemValue(file_system));
1458 CallClientFunction("InspectorFrontendAPI.fileSystemAdded",
1459 error_string_value.get(), file_system_value.get(), NULL);
1460 }
1461
1462 void DevToolsWindow::IndexingTotalWorkCalculated(
1463 int request_id,
1464 const std::string& file_system_path,
1465 int total_work) {
1466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1467 base::FundamentalValue request_id_value(request_id);
1468 base::StringValue file_system_path_value(file_system_path);
1469 base::FundamentalValue total_work_value(total_work);
1470 CallClientFunction("InspectorFrontendAPI.indexingTotalWorkCalculated",
1471 &request_id_value, &file_system_path_value,
1472 &total_work_value);
1473 }
1474
1475 void DevToolsWindow::IndexingWorked(int request_id,
1476 const std::string& file_system_path,
1477 int worked) {
1478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1479 base::FundamentalValue request_id_value(request_id);
1480 base::StringValue file_system_path_value(file_system_path);
1481 base::FundamentalValue worked_value(worked);
1482 CallClientFunction("InspectorFrontendAPI.indexingWorked", &request_id_value,
1483 &file_system_path_value, &worked_value);
1484 }
1485
1486 void DevToolsWindow::IndexingDone(int request_id,
1487 const std::string& file_system_path) {
1488 indexing_jobs_.erase(request_id);
1489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1490 base::FundamentalValue request_id_value(request_id);
1491 base::StringValue file_system_path_value(file_system_path);
1492 CallClientFunction("InspectorFrontendAPI.indexingDone", &request_id_value,
1493 &file_system_path_value, NULL);
1494 }
1495
1496 void DevToolsWindow::SearchCompleted(
1497 int request_id,
1498 const std::string& file_system_path,
1499 const std::vector<std::string>& file_paths) {
1500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1501 base::ListValue file_paths_value;
1502 for (std::vector<std::string>::const_iterator it(file_paths.begin());
1503 it != file_paths.end(); ++it) {
1504 file_paths_value.AppendString(*it);
1505 }
1506 base::FundamentalValue request_id_value(request_id);
1507 base::StringValue file_system_path_value(file_system_path);
1508 CallClientFunction("InspectorFrontendAPI.searchCompleted", &request_id_value,
1509 &file_system_path_value, &file_paths_value);
1510 }
1511
1512 void DevToolsWindow::ShowDevToolsConfirmInfoBar(
1513 const base::string16& message,
1514 const InfoBarCallback& callback) {
1515 DevToolsConfirmInfoBarDelegate::Create(
1516 is_docked_ ?
1517 InfoBarService::FromWebContents(GetInspectedWebContents()) :
1518 InfoBarService::FromWebContents(web_contents_),
1519 callback, message);
1520 } 1007 }
1521 1008
1522 void DevToolsWindow::CreateDevToolsBrowser() { 1009 void DevToolsWindow::CreateDevToolsBrowser() {
1523 std::string wp_key = GetDevToolsWindowPlacementPrefKey(); 1010 std::string wp_key = GetDevToolsWindowPlacementPrefKey();
1524 PrefService* prefs = profile_->GetPrefs(); 1011 PrefService* prefs = profile()->GetPrefs();
1525 const base::DictionaryValue* wp_pref = prefs->GetDictionary(wp_key.c_str()); 1012 const base::DictionaryValue* wp_pref = prefs->GetDictionary(wp_key.c_str());
1526 if (!wp_pref || wp_pref->empty()) { 1013 if (!wp_pref || wp_pref->empty()) {
1527 DictionaryPrefUpdate update(prefs, wp_key.c_str()); 1014 DictionaryPrefUpdate update(prefs, wp_key.c_str());
1528 base::DictionaryValue* defaults = update.Get(); 1015 base::DictionaryValue* defaults = update.Get();
1529 defaults->SetInteger("left", 100); 1016 defaults->SetInteger("left", 100);
1530 defaults->SetInteger("top", 100); 1017 defaults->SetInteger("top", 100);
1531 defaults->SetInteger("right", 740); 1018 defaults->SetInteger("right", 740);
1532 defaults->SetInteger("bottom", 740); 1019 defaults->SetInteger("bottom", 740);
1533 defaults->SetBoolean("maximized", false); 1020 defaults->SetBoolean("maximized", false);
1534 defaults->SetBoolean("always_on_top", false); 1021 defaults->SetBoolean("always_on_top", false);
1535 } 1022 }
1536 1023
1537 browser_ = new Browser(Browser::CreateParams::CreateForDevTools( 1024 browser_ = new Browser(Browser::CreateParams::CreateForDevTools(
1538 profile_, 1025 profile(),
1539 chrome::GetHostDesktopTypeForNativeView( 1026 chrome::GetHostDesktopTypeForNativeView(
1540 web_contents_->GetView()->GetNativeView()))); 1027 web_contents()->GetView()->GetNativeView())));
1541 browser_->tab_strip_model()->AddWebContents( 1028 browser_->tab_strip_model()->AddWebContents(
1542 web_contents_, -1, content::PAGE_TRANSITION_AUTO_TOPLEVEL, 1029 web_contents(), -1, content::PAGE_TRANSITION_AUTO_TOPLEVEL,
1543 TabStripModel::ADD_ACTIVE); 1030 TabStripModel::ADD_ACTIVE);
1544 GetRenderViewHost()->SyncRendererPrefs(); 1031 web_contents()->GetRenderViewHost()->SyncRendererPrefs();
1545 } 1032 }
1546 1033
1547 // static 1034 // static
1548 bool DevToolsWindow::FindInspectedBrowserAndTabIndex( 1035 bool DevToolsWindow::FindInspectedBrowserAndTabIndex(
1549 content::WebContents* inspected_web_contents, Browser** browser, int* tab) { 1036 WebContents* inspected_web_contents, Browser** browser, int* tab) {
1550 if (!inspected_web_contents) 1037 if (!inspected_web_contents)
1551 return false; 1038 return false;
1552 1039
1553 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 1040 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
1554 int tab_index = it->tab_strip_model()->GetIndexOfWebContents( 1041 int tab_index = it->tab_strip_model()->GetIndexOfWebContents(
1555 inspected_web_contents); 1042 inspected_web_contents);
1556 if (tab_index != TabStripModel::kNoTab) { 1043 if (tab_index != TabStripModel::kNoTab) {
1557 *browser = *it; 1044 *browser = *it;
1558 *tab = tab_index; 1045 *tab = tab_index;
1559 return true; 1046 return true;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1599 &line_value, 1086 &line_value,
1600 &column_value); 1087 &column_value);
1601 break; 1088 break;
1602 } 1089 }
1603 default: 1090 default:
1604 NOTREACHED(); 1091 NOTREACHED();
1605 break; 1092 break;
1606 } 1093 }
1607 } 1094 }
1608 1095
1609 void DevToolsWindow::UpdateTheme() {
1610 ThemeService* tp = ThemeServiceFactory::GetForProfile(profile_);
1611 DCHECK(tp);
1612
1613 std::string command("InspectorFrontendAPI.setToolbarColors(\"" +
1614 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_TOOLBAR)) +
1615 "\", \"" +
1616 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT)) +
1617 "\")");
1618 web_contents_->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(command));
1619 }
1620
1621 void DevToolsWindow::AddDevToolsExtensionsToClient() { 1096 void DevToolsWindow::AddDevToolsExtensionsToClient() {
1622 content::WebContents* inspected_web_contents = GetInspectedWebContents(); 1097 WebContents* inspected_web_contents = GetInspectedWebContents();
1623 if (inspected_web_contents) { 1098 if (inspected_web_contents) {
1624 SessionTabHelper* session_tab_helper = 1099 SessionTabHelper* session_tab_helper =
1625 SessionTabHelper::FromWebContents(inspected_web_contents); 1100 SessionTabHelper::FromWebContents(inspected_web_contents);
1626 if (session_tab_helper) { 1101 if (session_tab_helper) {
1627 base::FundamentalValue tabId(session_tab_helper->session_id().id()); 1102 base::FundamentalValue tabId(session_tab_helper->session_id().id());
1628 CallClientFunction("WebInspector.setInspectedTabId", &tabId, NULL, NULL); 1103 CallClientFunction("WebInspector.setInspectedTabId", &tabId, NULL, NULL);
1629 } 1104 }
1630 } 1105 }
1631 1106 DevToolsWindowBase::AddDevToolsExtensionsToClient();
1632 Profile* profile =
1633 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
1634 const ExtensionService* extension_service = extensions::ExtensionSystem::Get(
1635 profile->GetOriginalProfile())->extension_service();
1636 if (!extension_service)
1637 return;
1638 const extensions::ExtensionSet* extensions = extension_service->extensions();
1639
1640 base::ListValue results;
1641 for (extensions::ExtensionSet::const_iterator extension(extensions->begin());
1642 extension != extensions->end(); ++extension) {
1643 if (extensions::ManifestURL::GetDevToolsPage(extension->get()).is_empty())
1644 continue;
1645 base::DictionaryValue* extension_info = new base::DictionaryValue();
1646 extension_info->Set(
1647 "startPage",
1648 new base::StringValue(
1649 extensions::ManifestURL::GetDevToolsPage(extension->get()).spec()));
1650 extension_info->Set("name", new base::StringValue((*extension)->name()));
1651 extension_info->Set(
1652 "exposeExperimentalAPIs",
1653 new base::FundamentalValue((*extension)->HasAPIPermission(
1654 extensions::APIPermission::kExperimental)));
1655 results.Append(extension_info);
1656 }
1657 CallClientFunction("WebInspector.addExtensions", &results, NULL, NULL);
1658 }
1659
1660 void DevToolsWindow::CallClientFunction(const std::string& function_name,
1661 const base::Value* arg1,
1662 const base::Value* arg2,
1663 const base::Value* arg3) {
1664 std::string params;
1665 if (arg1) {
1666 std::string json;
1667 base::JSONWriter::Write(arg1, &json);
1668 params.append(json);
1669 if (arg2) {
1670 base::JSONWriter::Write(arg2, &json);
1671 params.append(", " + json);
1672 if (arg3) {
1673 base::JSONWriter::Write(arg3, &json);
1674 params.append(", " + json);
1675 }
1676 }
1677 }
1678 base::string16 javascript =
1679 base::UTF8ToUTF16(function_name + "(" + params + ");");
1680 web_contents_->GetMainFrame()->ExecuteJavaScript(javascript);
1681 } 1107 }
1682 1108
1683 void DevToolsWindow::UpdateBrowserToolbar() { 1109 void DevToolsWindow::UpdateBrowserToolbar() {
1684 BrowserWindow* inspected_window = GetInspectedBrowserWindow(); 1110 BrowserWindow* inspected_window = GetInspectedBrowserWindow();
1685 if (inspected_window) 1111 if (inspected_window)
1686 inspected_window->UpdateToolbar(NULL); 1112 inspected_window->UpdateToolbar(NULL);
1687 } 1113 }
1688 1114
1689 content::WebContents* DevToolsWindow::GetInspectedWebContents() { 1115 WebContents* DevToolsWindow::GetInspectedWebContents() {
1690 return inspected_contents_observer_ ? 1116 return inspected_contents_observer_ ?
1691 inspected_contents_observer_->web_contents() : NULL; 1117 inspected_contents_observer_->web_contents() : NULL;
1692 } 1118 }
1693 1119
1694 void DevToolsWindow::DocumentOnLoadCompletedInMainFrame() { 1120 void DevToolsWindow::DocumentOnLoadCompletedInMainFrame() {
1121 DevToolsWindowBase::DocumentOnLoadCompletedInMainFrame();
1695 // We could be in kLoadCompleted state already if frontend reloads itself. 1122 // We could be in kLoadCompleted state already if frontend reloads itself.
1696 if (load_state_ != kLoadCompleted) { 1123 if (load_state_ != kLoadCompleted) {
1697 // Load is completed when both kIsDockedSet and kOnLoadFired happened. 1124 // Load is completed when both kIsDockedSet and kOnLoadFired happened.
1698 // Here we set kOnLoadFired. 1125 // Here we set kOnLoadFired.
1699 load_state_ = load_state_ == kIsDockedSet ? kLoadCompleted : kOnLoadFired; 1126 load_state_ = load_state_ == kIsDockedSet ? kLoadCompleted : kOnLoadFired;
1700 } 1127 }
1701 if (load_state_ == kLoadCompleted) 1128 if (load_state_ == kLoadCompleted)
1702 LoadCompleted(); 1129 LoadCompleted();
1703 } 1130 }
1704 1131
1705 void DevToolsWindow::LoadCompleted() { 1132 void DevToolsWindow::LoadCompleted() {
1706 Show(action_on_load_); 1133 Show(action_on_load_);
1707 action_on_load_ = DevToolsToggleAction::NoOp(); 1134 action_on_load_ = DevToolsToggleAction::NoOp();
1708 UpdateTheme();
1709 AddDevToolsExtensionsToClient();
1710 if (!load_completed_callback_.is_null()) { 1135 if (!load_completed_callback_.is_null()) {
1711 load_completed_callback_.Run(); 1136 load_completed_callback_.Run();
1712 load_completed_callback_ = base::Closure(); 1137 load_completed_callback_ = base::Closure();
1713 } 1138 }
1714 } 1139 }
1715 1140
1716 void DevToolsWindow::SetLoadCompletedCallback(const base::Closure& closure) { 1141 void DevToolsWindow::SetLoadCompletedCallback(const base::Closure& closure) {
1717 if (load_state_ == kLoadCompleted) { 1142 if (load_state_ == kLoadCompleted) {
1718 if (!closure.is_null()) 1143 if (!closure.is_null())
1719 closure.Run(); 1144 closure.Run();
1720 return; 1145 return;
1721 } 1146 }
1722 load_completed_callback_ = closure; 1147 load_completed_callback_ = closure;
1723 } 1148 }
1724 1149
1725 void DevToolsWindow::SetWhitelistedShortcuts(
1726 const std::string& message) {
1727 event_forwarder_->SetWhitelistedShortcuts(message);
1728 }
1729
1730 bool DevToolsWindow::ForwardKeyboardEvent( 1150 bool DevToolsWindow::ForwardKeyboardEvent(
1731 const content::NativeWebKeyboardEvent& event) { 1151 const content::NativeWebKeyboardEvent& event) {
1732 return event_forwarder_->ForwardEvent(event); 1152 return event_forwarder_->ForwardEvent(event);
1733 } 1153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698