summaryrefslogtreecommitdiffstats
path: root/chrome/browser/debugger/devtools_view.cc
blob: d391139c88d1e40c813b8781246828fac1045937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/debugger/devtools_view.h"

#include <string>

#include "base/command_line.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/debugger/devtools_client_host.h"
#include "chrome/browser/debugger/devtools_manager.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/views/tab_contents_container_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/property_bag.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"

DevToolsView::DevToolsView() : tab_contents_(NULL) {
  web_container_ = new TabContentsContainerView();
  AddChildView(web_container_);
}

DevToolsView::~DevToolsView() {
}

std::string DevToolsView::GetClassName() const {
  return "DevToolsView";
}

gfx::Size DevToolsView::GetPreferredSize() {
  return gfx::Size(700, 400);
}

void DevToolsView::Layout() {
  web_container_->SetBounds(0, 0, width(), height());
}

void DevToolsView::ViewHierarchyChanged(bool is_add,
                                     views::View* parent,
                                     views::View* child) {
  if (is_add && child == this) {
    DCHECK(GetWidget());
    Init();
  }
}

void DevToolsView::Init() {
  // We can't create the TabContents until we've actually been put into a real
  // view hierarchy somewhere.
  Profile* profile = BrowserList::GetLastActive()->profile();

  tab_contents_ = new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL);
  tab_contents_->set_delegate(this);
  web_container_->SetTabContents(tab_contents_);
  tab_contents_->render_view_host()->AllowDOMUIBindings();

  // chrome://devtools/devtools.html
  GURL contents(std::string(chrome::kChromeUIDevToolsURL) + "devtools.html");

  // this will call CreateRenderView to create renderer process
  tab_contents_->controller().LoadURL(contents, GURL(),
                                      PageTransition::START_PAGE);

  // If each DevTools front end has its own renderer process allow to inspect
  // DevTools windows.
  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab)) {
    views::Accelerator accelerator('J', true /* shift_down */,
                                   true /* ctrl_down */, false /* alt_down */);
    views::FocusManager* focus_manager = GetFocusManager();
    DCHECK(focus_manager);
    focus_manager->RegisterAccelerator(accelerator, this);
  }
}

bool DevToolsView::AcceleratorPressed(const views::Accelerator& accelerator) {
  // TODO(yurys): get rid of this hack and pull the accelerator from the
  // resources
  views::Accelerator la('J', true /* shift_down */, true /* ctrl_down */,
                        false /* alt_down */);
  if (!tab_contents_) {
    return false;
  }
  if (!(la == accelerator)) {
    return false;
  }
  DevToolsManager* manager = g_browser_process->devtools_manager();
  manager->OpenDevToolsWindow(tab_contents_->render_view_host());
  return true;
}

void DevToolsView::OnWindowClosing() {
  DCHECK(tab_contents_) << "OnWindowClosing is called twice";
  if (tab_contents_) {
    // Detach last (and only) tab.
    web_container_->SetTabContents(NULL);

    // Destroy the tab and navigation controller.
    delete tab_contents_;
    tab_contents_ = NULL;
  }
}

void DevToolsView::SendMessageToClient(const IPC::Message& message) {
  if (tab_contents_) {
    RenderViewHost* target_host = tab_contents_->render_view_host();
    IPC::Message* m =  new IPC::Message(message);
    m->set_routing_id(target_host->routing_id());
    target_host->Send(m);
  }
}

bool DevToolsView::HasRenderViewHost(const RenderViewHost& rvh) const {
  if (tab_contents_) {
    return (&rvh == tab_contents_->render_view_host());
  }
  return false;
}

void DevToolsView::OpenURLFromTab(TabContents* source,
                               const GURL& url, const GURL& referrer,
                               WindowOpenDisposition disposition,
                               PageTransition::Type transition) {
  NOTREACHED();
}