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

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

Issue 395783002: [DevTools] Move DevToolsWindow testing code to a separate class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: includes Created 6 years, 5 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/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 21 matching lines...) Expand all
32 #include "chrome/common/render_messages.h" 32 #include "chrome/common/render_messages.h"
33 #include "chrome/common/url_constants.h" 33 #include "chrome/common/url_constants.h"
34 #include "components/pref_registry/pref_registry_syncable.h" 34 #include "components/pref_registry/pref_registry_syncable.h"
35 #include "content/public/browser/browser_thread.h" 35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/devtools_agent_host.h" 36 #include "content/public/browser/devtools_agent_host.h"
37 #include "content/public/browser/devtools_client_host.h" 37 #include "content/public/browser/devtools_client_host.h"
38 #include "content/public/browser/devtools_manager.h" 38 #include "content/public/browser/devtools_manager.h"
39 #include "content/public/browser/native_web_keyboard_event.h" 39 #include "content/public/browser/native_web_keyboard_event.h"
40 #include "content/public/browser/navigation_controller.h" 40 #include "content/public/browser/navigation_controller.h"
41 #include "content/public/browser/navigation_entry.h" 41 #include "content/public/browser/navigation_entry.h"
42 #include "content/public/browser/notification_source.h"
43 #include "content/public/browser/render_frame_host.h" 42 #include "content/public/browser/render_frame_host.h"
44 #include "content/public/browser/render_process_host.h" 43 #include "content/public/browser/render_process_host.h"
45 #include "content/public/browser/render_view_host.h" 44 #include "content/public/browser/render_view_host.h"
46 #include "content/public/browser/render_widget_host_view.h" 45 #include "content/public/browser/render_widget_host_view.h"
47 #include "content/public/browser/user_metrics.h" 46 #include "content/public/browser/user_metrics.h"
48 #include "content/public/browser/web_contents.h" 47 #include "content/public/browser/web_contents.h"
49 #include "content/public/common/content_client.h" 48 #include "content/public/common/content_client.h"
50 #include "content/public/common/page_transition_types.h" 49 #include "content/public/common/page_transition_types.h"
51 #include "content/public/common/url_constants.h" 50 #include "content/public/common/url_constants.h"
52 #include "content/public/test/test_utils.h"
53 #include "third_party/WebKit/public/web/WebInputEvent.h" 51 #include "third_party/WebKit/public/web/WebInputEvent.h"
54 #include "ui/events/keycodes/keyboard_codes.h" 52 #include "ui/events/keycodes/keyboard_codes.h"
55 53
56 using base::DictionaryValue; 54 using base::DictionaryValue;
57 using blink::WebInputEvent; 55 using blink::WebInputEvent;
58 using content::BrowserThread; 56 using content::BrowserThread;
59 using content::DevToolsAgentHost; 57 using content::DevToolsAgentHost;
60 using content::WebContents; 58 using content::WebContents;
61 59
62 namespace { 60 namespace {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 UpdateBrowserToolbar(); 307 UpdateBrowserToolbar();
310 308
311 if (toolbox_web_contents_) 309 if (toolbox_web_contents_)
312 delete toolbox_web_contents_; 310 delete toolbox_web_contents_;
313 311
314 DevToolsWindows* instances = g_instances.Pointer(); 312 DevToolsWindows* instances = g_instances.Pointer();
315 DevToolsWindows::iterator it( 313 DevToolsWindows::iterator it(
316 std::find(instances->begin(), instances->end(), this)); 314 std::find(instances->begin(), instances->end(), this));
317 DCHECK(it != instances->end()); 315 DCHECK(it != instances->end());
318 instances->erase(it); 316 instances->erase(it);
317
318 if (!close_callback_.is_null()) {
319 close_callback_.Run();
320 close_callback_ = base::Closure();
321 }
319 } 322 }
320 323
321 // static 324 // static
322 std::string DevToolsWindow::GetDevToolsWindowPlacementPrefKey() { 325 std::string DevToolsWindow::GetDevToolsWindowPlacementPrefKey() {
323 return std::string(prefs::kBrowserWindowPlacement) + "_" + 326 return std::string(prefs::kBrowserWindowPlacement) + "_" +
324 std::string(kDevToolsApp); 327 std::string(kDevToolsApp);
325 } 328 }
326 329
327 // static 330 // static
328 void DevToolsWindow::RegisterProfilePrefs( 331 void DevToolsWindow::RegisterProfilePrefs(
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 worker_agent, window->bindings_->frontend_host()); 419 worker_agent, window->bindings_->frontend_host());
417 } 420 }
418 window->ScheduleShow(DevToolsToggleAction::Show()); 421 window->ScheduleShow(DevToolsToggleAction::Show());
419 return window; 422 return window;
420 } 423 }
421 424
422 // static 425 // static
423 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker( 426 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker(
424 Profile* profile) { 427 Profile* profile) {
425 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker")); 428 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker"));
426 return Create(profile, GURL(), NULL, true, false, false); 429 return Create(profile, GURL(), NULL, true, false, false, "");
427 } 430 }
428 431
429 // static 432 // static
430 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow( 433 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow(
431 content::RenderViewHost* inspected_rvh) { 434 content::RenderViewHost* inspected_rvh) {
432 return ToggleDevToolsWindow( 435 return ToggleDevToolsWindow(
433 inspected_rvh, true, DevToolsToggleAction::Show()); 436 inspected_rvh, true, DevToolsToggleAction::Show(), "");
434 } 437 }
435 438
436 // static 439 // static
437 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow( 440 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow(
438 content::RenderViewHost* inspected_rvh, 441 content::RenderViewHost* inspected_rvh,
439 const DevToolsToggleAction& action) { 442 const DevToolsToggleAction& action) {
440 return ToggleDevToolsWindow( 443 return ToggleDevToolsWindow(inspected_rvh, true, action, "");
441 inspected_rvh, true, action);
442 } 444 }
443 445
444 // static 446 // static
445 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForTest(
446 content::RenderViewHost* inspected_rvh,
447 bool is_docked) {
448 DevToolsWindow* window = OpenDevToolsWindow(inspected_rvh);
449 window->SetIsDockedAndShowImmediatelyForTest(is_docked);
450 return window;
451 }
452
453 // static
454 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForTest(
455 Browser* browser,
456 bool is_docked) {
457 return OpenDevToolsWindowForTest(
458 browser->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
459 is_docked);
460 }
461
462 // static
463 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow( 447 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow(
464 Browser* browser, 448 Browser* browser,
465 const DevToolsToggleAction& action) { 449 const DevToolsToggleAction& action) {
466 if (action.type() == DevToolsToggleAction::kToggle && 450 if (action.type() == DevToolsToggleAction::kToggle &&
467 browser->is_devtools()) { 451 browser->is_devtools()) {
468 browser->tab_strip_model()->CloseAllTabs(); 452 browser->tab_strip_model()->CloseAllTabs();
469 return NULL; 453 return NULL;
470 } 454 }
471 455
472 return ToggleDevToolsWindow( 456 return ToggleDevToolsWindow(
473 browser->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(), 457 browser->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(),
474 action.type() == DevToolsToggleAction::kInspect, action); 458 action.type() == DevToolsToggleAction::kInspect, action, "");
475 } 459 }
476 460
477 // static 461 // static
478 void DevToolsWindow::OpenExternalFrontend( 462 void DevToolsWindow::OpenExternalFrontend(
479 Profile* profile, 463 Profile* profile,
480 const std::string& frontend_url, 464 const std::string& frontend_url,
481 content::DevToolsAgentHost* agent_host) { 465 content::DevToolsAgentHost* agent_host) {
482 DevToolsWindow* window = FindDevToolsWindow(agent_host); 466 DevToolsWindow* window = FindDevToolsWindow(agent_host);
483 if (!window) { 467 if (!window) {
484 window = Create(profile, DevToolsUI::GetProxyURL(frontend_url), NULL, 468 window = Create(profile, DevToolsUI::GetProxyURL(frontend_url), NULL,
485 false, true, false); 469 false, true, false, "");
486 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( 470 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
487 agent_host, window->bindings_->frontend_host()); 471 agent_host, window->bindings_->frontend_host());
488 } 472 }
489 window->ScheduleShow(DevToolsToggleAction::Show()); 473 window->ScheduleShow(DevToolsToggleAction::Show());
490 } 474 }
491 475
492 // static 476 // static
493 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow( 477 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow(
494 content::RenderViewHost* inspected_rvh, 478 content::RenderViewHost* inspected_rvh,
495 bool force_open, 479 bool force_open,
496 const DevToolsToggleAction& action) { 480 const DevToolsToggleAction& action,
481 const std::string& settings) {
497 scoped_refptr<DevToolsAgentHost> agent( 482 scoped_refptr<DevToolsAgentHost> agent(
498 DevToolsAgentHost::GetOrCreateFor(inspected_rvh)); 483 DevToolsAgentHost::GetOrCreateFor(inspected_rvh));
499 content::DevToolsManager* manager = content::DevToolsManager::GetInstance(); 484 content::DevToolsManager* manager = content::DevToolsManager::GetInstance();
500 DevToolsWindow* window = FindDevToolsWindow(agent.get()); 485 DevToolsWindow* window = FindDevToolsWindow(agent.get());
501 bool do_open = force_open; 486 bool do_open = force_open;
502 if (!window) { 487 if (!window) {
503 Profile* profile = Profile::FromBrowserContext( 488 Profile* profile = Profile::FromBrowserContext(
504 inspected_rvh->GetProcess()->GetBrowserContext()); 489 inspected_rvh->GetProcess()->GetBrowserContext());
505 content::RecordAction( 490 content::RecordAction(
506 base::UserMetricsAction("DevTools_InspectRenderer")); 491 base::UserMetricsAction("DevTools_InspectRenderer"));
507 window = Create(profile, GURL(), inspected_rvh, false, false, true); 492 window = Create(
493 profile, GURL(), inspected_rvh, false, false, true, settings);
508 manager->RegisterDevToolsClientHostFor(agent.get(), 494 manager->RegisterDevToolsClientHostFor(agent.get(),
509 window->bindings_->frontend_host()); 495 window->bindings_->frontend_host());
510 do_open = true; 496 do_open = true;
511 } 497 }
512 498
513 // Update toolbar to reflect DevTools changes. 499 // Update toolbar to reflect DevTools changes.
514 window->UpdateBrowserToolbar(); 500 window->UpdateBrowserToolbar();
515 501
516 // If window is docked and visible, we hide it on toggle. If window is 502 // If window is docked and visible, we hide it on toggle. If window is
517 // undocked, we show (activate) it. 503 // undocked, we show (activate) it.
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 toolbox_web_contents_(NULL), 677 toolbox_web_contents_(NULL),
692 bindings_(NULL), 678 bindings_(NULL),
693 browser_(NULL), 679 browser_(NULL),
694 is_docked_(true), 680 is_docked_(true),
695 can_dock_(can_dock), 681 can_dock_(can_dock),
696 // This initialization allows external front-end to work without changes. 682 // This initialization allows external front-end to work without changes.
697 // We don't wait for docking call, but instead immediately show undocked. 683 // We don't wait for docking call, but instead immediately show undocked.
698 // Passing "dockSide=undocked" parameter ensures proper UI. 684 // Passing "dockSide=undocked" parameter ensures proper UI.
699 life_stage_(can_dock ? kNotLoaded : kIsDockedSet), 685 life_stage_(can_dock ? kNotLoaded : kIsDockedSet),
700 action_on_load_(DevToolsToggleAction::NoOp()), 686 action_on_load_(DevToolsToggleAction::NoOp()),
701 ignore_set_is_docked_(false),
702 intercepted_page_beforeunload_(false) { 687 intercepted_page_beforeunload_(false) {
703 // Set up delegate, so we get fully-functional window immediately. 688 // Set up delegate, so we get fully-functional window immediately.
704 // It will not appear in UI though until |life_stage_ == kLoadCompleted|. 689 // It will not appear in UI though until |life_stage_ == kLoadCompleted|.
705 main_web_contents_->SetDelegate(this); 690 main_web_contents_->SetDelegate(this);
706 bindings_ = new DevToolsUIBindings( 691 bindings_ = new DevToolsUIBindings(
707 main_web_contents_, 692 main_web_contents_,
708 DevToolsUIBindings::ApplyThemeToURL(profile, url)); 693 DevToolsUIBindings::ApplyThemeToURL(profile, url));
709 // Bindings take ownership over devtools as its delegate. 694 // Bindings take ownership over devtools as its delegate.
710 bindings_->SetDelegate(this); 695 bindings_->SetDelegate(this);
711 // DevTools uses chrome_page_zoom::Zoom(), so main_web_contents_ requires a 696 // DevTools uses chrome_page_zoom::Zoom(), so main_web_contents_ requires a
(...skipping 11 matching lines...) Expand all
723 event_forwarder_.reset(new DevToolsEventForwarder(this)); 708 event_forwarder_.reset(new DevToolsEventForwarder(this));
724 } 709 }
725 710
726 // static 711 // static
727 DevToolsWindow* DevToolsWindow::Create( 712 DevToolsWindow* DevToolsWindow::Create(
728 Profile* profile, 713 Profile* profile,
729 const GURL& frontend_url, 714 const GURL& frontend_url,
730 content::RenderViewHost* inspected_rvh, 715 content::RenderViewHost* inspected_rvh,
731 bool shared_worker_frontend, 716 bool shared_worker_frontend,
732 bool external_frontend, 717 bool external_frontend,
733 bool can_dock) { 718 bool can_dock,
719 const std::string& settings) {
734 if (inspected_rvh) { 720 if (inspected_rvh) {
735 // Check for a place to dock. 721 // Check for a place to dock.
736 Browser* browser = NULL; 722 Browser* browser = NULL;
737 int tab; 723 int tab;
738 WebContents* inspected_web_contents = 724 WebContents* inspected_web_contents =
739 content::WebContents::FromRenderViewHost(inspected_rvh); 725 content::WebContents::FromRenderViewHost(inspected_rvh);
740 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents, 726 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents,
741 &browser, &tab) || 727 &browser, &tab) ||
742 inspected_rvh->GetMainFrame()->IsCrossProcessSubframe() || 728 inspected_rvh->GetMainFrame()->IsCrossProcessSubframe() ||
743 browser->is_type_popup()) { 729 browser->is_type_popup()) {
744 can_dock = false; 730 can_dock = false;
745 } 731 }
746 } 732 }
747 733
748 // Create WebContents with devtools. 734 // Create WebContents with devtools.
749 GURL url(GetDevToolsURL(profile, frontend_url, 735 GURL url(GetDevToolsURL(profile, frontend_url,
750 shared_worker_frontend, 736 shared_worker_frontend,
751 external_frontend, 737 external_frontend,
752 can_dock)); 738 can_dock, settings));
753 return new DevToolsWindow(profile, url, inspected_rvh, can_dock); 739 return new DevToolsWindow(profile, url, inspected_rvh, can_dock);
754 } 740 }
755 741
756 // static 742 // static
757 GURL DevToolsWindow::GetDevToolsURL(Profile* profile, 743 GURL DevToolsWindow::GetDevToolsURL(Profile* profile,
758 const GURL& base_url, 744 const GURL& base_url,
759 bool shared_worker_frontend, 745 bool shared_worker_frontend,
760 bool external_frontend, 746 bool external_frontend,
761 bool can_dock) { 747 bool can_dock,
748 const std::string& settings) {
762 // Compatibility errors are encoded with data urls, pass them 749 // Compatibility errors are encoded with data urls, pass them
763 // through with no decoration. 750 // through with no decoration.
764 if (base_url.SchemeIs("data")) 751 if (base_url.SchemeIs("data"))
765 return base_url; 752 return base_url;
766 753
767 std::string frontend_url( 754 std::string frontend_url(
768 base_url.is_empty() ? chrome::kChromeUIDevToolsURL : base_url.spec()); 755 base_url.is_empty() ? chrome::kChromeUIDevToolsURL : base_url.spec());
769 std::string url_string( 756 std::string url_string(
770 frontend_url + 757 frontend_url +
771 ((frontend_url.find("?") == std::string::npos) ? "?" : "&")); 758 ((frontend_url.find("?") == std::string::npos) ? "?" : "&"));
772 if (shared_worker_frontend) 759 if (shared_worker_frontend)
773 url_string += "&isSharedWorker=true"; 760 url_string += "&isSharedWorker=true";
774 if (external_frontend) 761 if (external_frontend)
775 url_string += "&remoteFrontend=true"; 762 url_string += "&remoteFrontend=true";
776 if (can_dock) 763 if (can_dock)
777 url_string += "&can_dock=true"; 764 url_string += "&can_dock=true";
765 if (settings.size())
766 url_string += "&settings=" + settings;
778 return GURL(url_string); 767 return GURL(url_string);
779 } 768 }
780 769
781 // static 770 // static
782 DevToolsWindow* DevToolsWindow::FindDevToolsWindow( 771 DevToolsWindow* DevToolsWindow::FindDevToolsWindow(
783 DevToolsAgentHost* agent_host) { 772 DevToolsAgentHost* agent_host) {
784 if (!agent_host || g_instances == NULL) 773 if (!agent_host || g_instances == NULL)
785 return NULL; 774 return NULL;
786 DevToolsWindows* instances = g_instances.Pointer(); 775 DevToolsWindows* instances = g_instances.Pointer();
787 content::DevToolsManager* manager = content::DevToolsManager::GetInstance(); 776 content::DevToolsManager* manager = content::DevToolsManager::GetInstance();
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 } 1010 }
1022 1011
1023 void DevToolsWindow::MoveWindow(int x, int y) { 1012 void DevToolsWindow::MoveWindow(int x, int y) {
1024 if (!is_docked_) { 1013 if (!is_docked_) {
1025 gfx::Rect bounds = browser_->window()->GetBounds(); 1014 gfx::Rect bounds = browser_->window()->GetBounds();
1026 bounds.Offset(x, y); 1015 bounds.Offset(x, y);
1027 browser_->window()->SetBounds(bounds); 1016 browser_->window()->SetBounds(bounds);
1028 } 1017 }
1029 } 1018 }
1030 1019
1031 void DevToolsWindow::SetIsDockedAndShowImmediatelyForTest(bool is_docked) {
1032 DCHECK(!is_docked || can_dock_);
1033 DCHECK(life_stage_ != kClosing);
1034 if (life_stage_ == kLoadCompleted) {
1035 SetIsDocked(is_docked);
1036 } else {
1037 is_docked_ = is_docked;
1038 // Load is completed when both kIsDockedSet and kOnLoadFired happened.
1039 // Note that kIsDockedSet may be already set when can_dock_ is false.
1040 life_stage_ = life_stage_ == kOnLoadFired ? kLoadCompleted : kIsDockedSet;
1041 // Note that action_on_load_ will be performed after the load is actually
1042 // completed. For now, just show the window.
1043 Show(DevToolsToggleAction::Show());
1044 if (life_stage_ == kLoadCompleted)
1045 LoadCompleted();
1046 }
1047 ignore_set_is_docked_ = true;
1048 }
1049
1050 void DevToolsWindow::SetIsDocked(bool dock_requested) { 1020 void DevToolsWindow::SetIsDocked(bool dock_requested) {
1051 if (ignore_set_is_docked_ || life_stage_ == kClosing) 1021 if (life_stage_ == kClosing)
1052 return; 1022 return;
1053 1023
1054 DCHECK(can_dock_ || !dock_requested); 1024 DCHECK(can_dock_ || !dock_requested);
1055 if (!can_dock_) 1025 if (!can_dock_)
1056 dock_requested = false; 1026 dock_requested = false;
1057 1027
1058 bool was_docked = is_docked_; 1028 bool was_docked = is_docked_;
1059 is_docked_ = dock_requested; 1029 is_docked_ = dock_requested;
1060 1030
1061 if (life_stage_ != kLoadCompleted) { 1031 if (life_stage_ != kLoadCompleted) {
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1252 void DevToolsWindow::LoadCompleted() { 1222 void DevToolsWindow::LoadCompleted() {
1253 Show(action_on_load_); 1223 Show(action_on_load_);
1254 action_on_load_ = DevToolsToggleAction::NoOp(); 1224 action_on_load_ = DevToolsToggleAction::NoOp();
1255 if (!load_completed_callback_.is_null()) { 1225 if (!load_completed_callback_.is_null()) {
1256 load_completed_callback_.Run(); 1226 load_completed_callback_.Run();
1257 load_completed_callback_ = base::Closure(); 1227 load_completed_callback_ = base::Closure();
1258 } 1228 }
1259 } 1229 }
1260 1230
1261 void DevToolsWindow::SetLoadCompletedCallback(const base::Closure& closure) { 1231 void DevToolsWindow::SetLoadCompletedCallback(const base::Closure& closure) {
1262 if (life_stage_ == kLoadCompleted) { 1232 if (life_stage_ == kLoadCompleted || life_stage_ == kClosing) {
1263 if (!closure.is_null()) 1233 if (!closure.is_null())
1264 closure.Run(); 1234 closure.Run();
1265 return; 1235 return;
1266 } 1236 }
1267 load_completed_callback_ = closure; 1237 load_completed_callback_ = closure;
1268 } 1238 }
1269 1239
1270 bool DevToolsWindow::ForwardKeyboardEvent( 1240 bool DevToolsWindow::ForwardKeyboardEvent(
1271 const content::NativeWebKeyboardEvent& event) { 1241 const content::NativeWebKeyboardEvent& event) {
1272 return event_forwarder_->ForwardEvent(event); 1242 return event_forwarder_->ForwardEvent(event);
1273 } 1243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698