Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 inspected_contents_observer_->web_contents(); | 165 inspected_contents_observer_->web_contents(); |
| 166 if (!inspected_contents) | 166 if (!inspected_contents) |
| 167 return NULL; | 167 return NULL; |
| 168 Browser* browser = NULL; | 168 Browser* browser = NULL; |
| 169 int tab = 0; | 169 int tab = 0; |
| 170 if (FindInspectedBrowserAndTabIndex(inspected_contents, &browser, &tab)) | 170 if (FindInspectedBrowserAndTabIndex(inspected_contents, &browser, &tab)) |
| 171 return browser->window(); | 171 return browser->window(); |
| 172 return NULL; | 172 return NULL; |
| 173 } | 173 } |
| 174 | 174 |
| 175 bool AreDeveloperToolsDisabled( | |
| 176 const WebContents* const inspected_web_contents) { | |
| 177 if (!inspected_web_contents) return false; | |
| 178 Profile* profile = | |
| 179 Profile::FromBrowserContext(inspected_web_contents->GetBrowserContext()); | |
| 180 return profile->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled); | |
| 181 } | |
| 182 | |
| 175 } // namespace | 183 } // namespace |
| 176 | 184 |
| 177 // DevToolsEventForwarder ----------------------------------------------------- | 185 // DevToolsEventForwarder ----------------------------------------------------- |
| 178 | 186 |
| 179 class DevToolsEventForwarder { | 187 class DevToolsEventForwarder { |
| 180 public: | 188 public: |
| 181 explicit DevToolsEventForwarder(DevToolsWindow* window) | 189 explicit DevToolsEventForwarder(DevToolsWindow* window) |
| 182 : devtools_window_(window) {} | 190 : devtools_window_(window) {} |
| 183 | 191 |
| 184 // Registers whitelisted shortcuts with the forwarder. | 192 // Registers whitelisted shortcuts with the forwarder. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end(); | 389 for (DevToolsWindows::iterator it(instances->begin()); it != instances->end(); |
| 382 ++it) { | 390 ++it) { |
| 383 if ((*it)->main_web_contents_ == web_contents || | 391 if ((*it)->main_web_contents_ == web_contents || |
| 384 (*it)->toolbox_web_contents_ == web_contents) | 392 (*it)->toolbox_web_contents_ == web_contents) |
| 385 return true; | 393 return true; |
| 386 } | 394 } |
| 387 return false; | 395 return false; |
| 388 } | 396 } |
| 389 | 397 |
| 390 // static | 398 // static |
| 391 DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker( | 399 void DevToolsWindow::OpenDevToolsWindowForWorker( |
| 392 Profile* profile, | 400 Profile* profile, |
| 393 const scoped_refptr<DevToolsAgentHost>& worker_agent) { | 401 const scoped_refptr<DevToolsAgentHost>& worker_agent) { |
| 394 DevToolsWindow* window = FindDevToolsWindow(worker_agent.get()); | 402 DevToolsWindow* window = FindDevToolsWindow(worker_agent.get()); |
| 395 if (!window) { | 403 if (!window) { |
| 396 window = DevToolsWindow::CreateDevToolsWindowForWorker(profile); | 404 window = DevToolsWindow::CreateDevToolsWindowForWorker(profile); |
| 405 | |
| 397 DCHECK(window); | 406 DCHECK(window); |
|
dgozman
2015/03/10 19:36:34
Remove DCHECK.
| |
| 407 if (!window) | |
| 408 return; | |
| 409 | |
| 398 window->bindings_->AttachTo(worker_agent); | 410 window->bindings_->AttachTo(worker_agent); |
| 399 } | 411 } |
| 400 window->ScheduleShow(DevToolsToggleAction::Show()); | 412 window->ScheduleShow(DevToolsToggleAction::Show()); |
| 401 return window; | |
| 402 } | 413 } |
| 403 | 414 |
| 404 // static | 415 // static |
| 405 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker( | 416 DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker( |
| 406 Profile* profile) { | 417 Profile* profile) { |
| 407 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker")); | 418 content::RecordAction(base::UserMetricsAction("DevTools_InspectWorker")); |
| 408 return Create(profile, GURL(), NULL, true, std::string(), false, ""); | 419 return Create(profile, GURL(), NULL, true, std::string(), false, ""); |
| 409 } | 420 } |
| 410 | 421 |
| 411 // static | 422 // static |
| 412 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow( | 423 void DevToolsWindow::OpenDevToolsWindow( |
| 413 content::WebContents* inspected_web_contents) { | 424 content::WebContents* inspected_web_contents) { |
| 414 return ToggleDevToolsWindow( | 425 DevToolsWindow::OpenDevToolsWindow(inspected_web_contents, |
|
dgozman
2015/03/10 19:36:34
nit: let's revert back to ToggleDevToolsWindow
| |
| 415 inspected_web_contents, true, DevToolsToggleAction::Show(), ""); | 426 DevToolsToggleAction::Show()); |
| 416 } | 427 } |
| 417 | 428 |
| 418 // static | 429 // static |
| 419 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow( | 430 void DevToolsWindow::OpenDevToolsWindow( |
| 420 content::WebContents* inspected_web_contents, | 431 content::WebContents* inspected_web_contents, |
| 421 const DevToolsToggleAction& action) { | 432 const DevToolsToggleAction& action) { |
| 422 return ToggleDevToolsWindow(inspected_web_contents, true, action, ""); | 433 ToggleDevToolsWindow(inspected_web_contents, true, action, ""); |
| 423 } | 434 } |
| 424 | 435 |
| 425 // static | 436 // static |
| 426 DevToolsWindow* DevToolsWindow::OpenDevToolsWindow( | 437 void DevToolsWindow::OpenDevToolsWindow( |
| 427 Profile* profile, | 438 Profile* profile, |
| 428 const scoped_refptr<content::DevToolsAgentHost>& agent_host) { | 439 const scoped_refptr<content::DevToolsAgentHost>& agent_host) { |
| 429 DevToolsWindow* window = FindDevToolsWindow(agent_host.get()); | 440 DevToolsWindow* window = FindDevToolsWindow(agent_host.get()); |
| 430 if (!window) { | 441 if (!window) { |
| 431 window = DevToolsWindow::Create( | 442 window = DevToolsWindow::Create( |
| 432 profile, GURL(), nullptr, false, std::string(), false, std::string()); | 443 profile, GURL(), nullptr, false, std::string(), false, std::string()); |
| 444 | |
| 433 DCHECK(window); | 445 DCHECK(window); |
|
dgozman
2015/03/10 19:36:34
remove DCHECK
| |
| 446 if (!window) | |
| 447 return; | |
| 448 | |
| 434 window->bindings_->AttachTo(agent_host); | 449 window->bindings_->AttachTo(agent_host); |
| 435 } | 450 } |
| 436 window->ScheduleShow(DevToolsToggleAction::Show()); | 451 window->ScheduleShow(DevToolsToggleAction::Show()); |
| 437 return window; | |
| 438 } | 452 } |
| 439 | 453 |
| 440 // static | 454 // static |
| 441 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow( | 455 void DevToolsWindow::ToggleDevToolsWindow( |
| 442 Browser* browser, | 456 Browser* browser, |
| 443 const DevToolsToggleAction& action) { | 457 const DevToolsToggleAction& action) { |
| 444 if (action.type() == DevToolsToggleAction::kToggle && | 458 if (action.type() == DevToolsToggleAction::kToggle && |
| 445 browser->is_devtools()) { | 459 browser->is_devtools()) { |
| 446 browser->tab_strip_model()->CloseAllTabs(); | 460 browser->tab_strip_model()->CloseAllTabs(); |
| 447 return NULL; | 461 return; |
| 448 } | 462 } |
| 449 | 463 |
| 450 return ToggleDevToolsWindow( | 464 ToggleDevToolsWindow( |
| 451 browser->tab_strip_model()->GetActiveWebContents(), | 465 browser->tab_strip_model()->GetActiveWebContents(), |
| 452 action.type() == DevToolsToggleAction::kInspect, | 466 action.type() == DevToolsToggleAction::kInspect, |
| 453 action, ""); | 467 action, ""); |
| 454 } | 468 } |
| 455 | 469 |
| 456 // static | 470 // static |
| 457 void DevToolsWindow::OpenExternalFrontend( | 471 void DevToolsWindow::OpenExternalFrontend( |
| 458 Profile* profile, | 472 Profile* profile, |
| 459 const std::string& frontend_url, | 473 const std::string& frontend_url, |
| 460 const scoped_refptr<content::DevToolsAgentHost>& agent_host, | 474 const scoped_refptr<content::DevToolsAgentHost>& agent_host, |
| 461 bool isWorker) { | 475 bool isWorker) { |
| 462 DevToolsWindow* window = FindDevToolsWindow(agent_host.get()); | 476 DevToolsWindow* window = FindDevToolsWindow(agent_host.get()); |
| 463 if (!window) { | 477 if (!window) { |
| 464 window = Create(profile, GURL(), nullptr, isWorker, | 478 window = Create(profile, GURL(), nullptr, isWorker, |
| 465 DevToolsUI::GetProxyURL(frontend_url).spec(), false, std::string()); | 479 DevToolsUI::GetProxyURL(frontend_url).spec(), false, std::string()); |
| 466 if (window) | 480 |
| 467 window->bindings_->AttachTo(agent_host); | 481 if (!window) |
| 482 return; | |
| 483 | |
| 484 window->bindings_->AttachTo(agent_host); | |
| 468 } | 485 } |
| 469 if (window) | 486 |
| 470 window->ScheduleShow(DevToolsToggleAction::Show()); | 487 window->ScheduleShow(DevToolsToggleAction::Show()); |
| 471 } | 488 } |
| 472 | 489 |
| 473 // static | 490 // static |
| 474 DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow( | 491 void DevToolsWindow::ToggleDevToolsWindow( |
| 475 content::WebContents* inspected_web_contents, | 492 content::WebContents* inspected_web_contents, |
| 476 bool force_open, | 493 bool force_open, |
| 477 const DevToolsToggleAction& action, | 494 const DevToolsToggleAction& action, |
| 478 const std::string& settings) { | 495 const std::string& settings) { |
| 479 scoped_refptr<DevToolsAgentHost> agent( | 496 scoped_refptr<DevToolsAgentHost> agent( |
| 480 DevToolsAgentHost::GetOrCreateFor(inspected_web_contents)); | 497 DevToolsAgentHost::GetOrCreateFor(inspected_web_contents)); |
| 481 DevToolsWindow* window = FindDevToolsWindow(agent.get()); | 498 DevToolsWindow* window = FindDevToolsWindow(agent.get()); |
| 482 bool do_open = force_open; | 499 bool do_open = force_open; |
| 483 if (!window) { | 500 if (!window) { |
| 484 Profile* profile = Profile::FromBrowserContext( | 501 Profile* profile = Profile::FromBrowserContext( |
| 485 inspected_web_contents->GetBrowserContext()); | 502 inspected_web_contents->GetBrowserContext()); |
| 486 content::RecordAction( | 503 content::RecordAction( |
| 487 base::UserMetricsAction("DevTools_InspectRenderer")); | 504 base::UserMetricsAction("DevTools_InspectRenderer")); |
| 488 window = Create(profile, GURL(), inspected_web_contents, | 505 window = Create(profile, GURL(), inspected_web_contents, |
| 489 false, std::string(), true, settings); | 506 false, std::string(), true, settings); |
| 507 | |
| 490 DCHECK(window); | 508 DCHECK(window); |
|
dgozman
2015/03/10 19:36:34
remove DCHECK
| |
| 509 if (!window) | |
| 510 return; | |
| 511 | |
| 491 window->bindings_->AttachTo(agent.get()); | 512 window->bindings_->AttachTo(agent.get()); |
| 492 do_open = true; | 513 do_open = true; |
| 493 } | 514 } |
| 494 | 515 |
| 495 // Update toolbar to reflect DevTools changes. | 516 // Update toolbar to reflect DevTools changes. |
| 496 window->UpdateBrowserToolbar(); | 517 window->UpdateBrowserToolbar(); |
| 497 | 518 |
| 498 // If window is docked and visible, we hide it on toggle. If window is | 519 // If window is docked and visible, we hide it on toggle. If window is |
| 499 // undocked, we show (activate) it. | 520 // undocked, we show (activate) it. |
| 500 if (!window->is_docked_ || do_open) | 521 if (!window->is_docked_ || do_open) |
| 501 window->ScheduleShow(action); | 522 window->ScheduleShow(action); |
| 502 else | 523 else |
| 503 window->CloseWindow(); | 524 window->CloseWindow(); |
| 504 | |
| 505 return window; | |
| 506 } | 525 } |
| 507 | 526 |
| 508 // static | 527 // static |
| 509 void DevToolsWindow::InspectElement( | 528 void DevToolsWindow::InspectElement( |
| 510 content::WebContents* inspected_web_contents, | 529 content::WebContents* inspected_web_contents, |
| 511 int x, | 530 int x, |
| 512 int y) { | 531 int y) { |
| 513 scoped_refptr<DevToolsAgentHost> agent( | 532 scoped_refptr<DevToolsAgentHost> agent( |
| 514 DevToolsAgentHost::GetOrCreateFor(inspected_web_contents)); | 533 DevToolsAgentHost::GetOrCreateFor(inspected_web_contents)); |
| 515 agent->InspectElement(x, y); | 534 agent->InspectElement(x, y); |
| 516 bool should_measure_time = FindDevToolsWindow(agent.get()) == NULL; | 535 bool should_measure_time = FindDevToolsWindow(agent.get()) == NULL; |
| 517 base::TimeTicks start_time = base::TimeTicks::Now(); | 536 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 518 // TODO(loislo): we should initiate DevTools window opening from within | 537 // TODO(loislo): we should initiate DevTools window opening from within |
| 519 // renderer. Otherwise, we still can hit a race condition here. | 538 // renderer. Otherwise, we still can hit a race condition here. |
| 520 DevToolsWindow* window = OpenDevToolsWindow(inspected_web_contents); | 539 OpenDevToolsWindow(inspected_web_contents); |
| 521 if (should_measure_time) | 540 |
| 541 DevToolsWindow* window = FindDevToolsWindow(agent.get()); | |
| 542 if (should_measure_time && window) | |
| 522 window->inspect_element_start_time_ = start_time; | 543 window->inspect_element_start_time_ = start_time; |
| 523 } | 544 } |
| 524 | 545 |
| 525 void DevToolsWindow::ScheduleShow(const DevToolsToggleAction& action) { | 546 void DevToolsWindow::ScheduleShow(const DevToolsToggleAction& action) { |
| 526 if (life_stage_ == kLoadCompleted) { | 547 if (life_stage_ == kLoadCompleted) { |
| 527 Show(action); | 548 Show(action); |
| 528 return; | 549 return; |
| 529 } | 550 } |
| 530 | 551 |
| 531 // Action will be done only after load completed. | 552 // Action will be done only after load completed. |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 715 | 736 |
| 716 // static | 737 // static |
| 717 DevToolsWindow* DevToolsWindow::Create( | 738 DevToolsWindow* DevToolsWindow::Create( |
| 718 Profile* profile, | 739 Profile* profile, |
| 719 const GURL& frontend_url, | 740 const GURL& frontend_url, |
| 720 content::WebContents* inspected_web_contents, | 741 content::WebContents* inspected_web_contents, |
| 721 bool shared_worker_frontend, | 742 bool shared_worker_frontend, |
| 722 const std::string& remote_frontend, | 743 const std::string& remote_frontend, |
| 723 bool can_dock, | 744 bool can_dock, |
| 724 const std::string& settings) { | 745 const std::string& settings) { |
| 746 // If development tools disabled by policy don't open the window. | |
| 747 if (AreDeveloperToolsDisabled(inspected_web_contents)) | |
|
dgozman
2015/03/10 19:36:34
I'd just inline the function here. Note that |prof
| |
| 748 return nullptr; | |
| 725 if (inspected_web_contents) { | 749 if (inspected_web_contents) { |
| 726 // Check for a place to dock. | 750 // Check for a place to dock. |
| 727 Browser* browser = NULL; | 751 Browser* browser = NULL; |
| 728 int tab; | 752 int tab; |
| 729 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents, | 753 if (!FindInspectedBrowserAndTabIndex(inspected_web_contents, |
| 730 &browser, &tab) || | 754 &browser, &tab) || |
| 731 browser->is_type_popup()) { | 755 browser->is_type_popup()) { |
| 732 can_dock = false; | 756 can_dock = false; |
| 733 } | 757 } |
| 734 } | 758 } |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1220 closure.Run(); | 1244 closure.Run(); |
| 1221 return; | 1245 return; |
| 1222 } | 1246 } |
| 1223 load_completed_callback_ = closure; | 1247 load_completed_callback_ = closure; |
| 1224 } | 1248 } |
| 1225 | 1249 |
| 1226 bool DevToolsWindow::ForwardKeyboardEvent( | 1250 bool DevToolsWindow::ForwardKeyboardEvent( |
| 1227 const content::NativeWebKeyboardEvent& event) { | 1251 const content::NativeWebKeyboardEvent& event) { |
| 1228 return event_forwarder_->ForwardEvent(event); | 1252 return event_forwarder_->ForwardEvent(event); |
| 1229 } | 1253 } |
| OLD | NEW |