summaryrefslogtreecommitdiffstats
path: root/chrome/browser/debugger/debugger_view.cc
blob: 865635f3c8fd40e199a616db34cb8235b02610aa (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2006-2008 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/debugger_view.h"

#include "app/gfx/chrome_canvas.h"
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/json_writer.h"
#include "base/values.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/debugger/debugger_shell.h"
#include "chrome/browser/debugger/debugger_window.h"
#include "chrome/browser/debugger/debugger_wrapper.h"
#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/view_ids.h"
#include "chrome/browser/views/standard_layout.h"
#include "chrome/browser/views/tab_contents_container_view.h"
#include "chrome/common/url_constants.h"
#include "grit/debugger_resources.h"
#include "views/grid_layout.h"
#include "views/controls/scrollbar/native_scroll_bar.h"
#include "views/controls/scroll_view.h"
#include "views/controls/text_field.h"
#include "views/view.h"

DebuggerView::DebuggerView(DebuggerWindow* window)
    : window_(window), output_ready_(false) {
  web_container_ = new TabContentsContainerView();
  AddChildView(web_container_);
  AddAccelerator(views::Accelerator(VK_ESCAPE, false, false, false));
}

DebuggerView::~DebuggerView() {
}

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

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


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

void DebuggerView::Paint(ChromeCanvas* canvas) {
#ifndef NDEBUG
  SkPaint paint;
  canvas->FillRectInt(SK_ColorCYAN, x(), y(), width(), height());
#endif
}

void DebuggerView::SetOutputViewReady() {
  output_ready_ = true;
  for (std::vector<std::wstring>::iterator i = pending_output_.begin();
       i != pending_output_.end(); ++i) {
    Output(*i);
  }
  pending_output_.clear();

  for (std::vector<std::string>::const_iterator i = pending_events_.begin();
       i != pending_events_.end(); ++i) {
    ExecuteJavascript(*i);
  }
  pending_events_.clear();
}

void DebuggerView::Output(const std::string& out) {
  Output(UTF8ToWide(out));
}

void DebuggerView::Output(const std::wstring& out) {
  if (!output_ready_) {
    pending_output_.push_back(out);
    return;
  }

  DictionaryValue* body = new DictionaryValue;
  body->Set(L"text", Value::CreateStringValue(out));
  SendEventToPage(L"appendText", body);
}

void DebuggerView::OnInit() {
  // 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();

  GURL contents(std::string(chrome::kChromeUIScheme) +
                "://inspector/debugger.html");
  tab_contents_->controller().LoadURL(contents, GURL(),
                                      PageTransition::START_PAGE);
}

void DebuggerView::OnShow() {
  tab_contents_->Focus();
}

void DebuggerView::OnClose() {
  web_container_->SetTabContents(NULL);
  delete tab_contents_;
}

void DebuggerView::OpenURLFromTab(TabContents* source,
                               const GURL& url,
                               const GURL& referrer,
                               WindowOpenDisposition disposition,
                               PageTransition::Type transition) {
  BrowserList::GetLastActive()->OpenURL(url, referrer, disposition,
                                        transition);
}


void DebuggerView::SendEventToPage(const std::wstring& name,
                                   Value* body) {
  DictionaryValue msg;
  msg.SetString(L"type", L"event");
  msg.SetString(L"event", name);
  msg.Set(L"body", body);

  std::string json;
  JSONWriter::Write(&msg, false, &json);

  const std::string js =
    StringPrintf("DebuggerIPC.onMessageReceived(%s)", json.c_str());
  if (output_ready_) {
    ExecuteJavascript(js);
  } else {
    pending_events_.push_back(js);
  }
}

void DebuggerView::ExecuteJavascript(const std::string& js) {
  tab_contents_->render_view_host()->ExecuteJavascriptInWebFrame(L"",
      UTF8ToWide(js));
}

void DebuggerView::LoadingStateChanged(TabContents* source) {
  if (!source->is_loading())
    SetOutputViewReady();
}

bool DebuggerView::AcceleratorPressed(const views::Accelerator& accelerator) {
  DCHECK(accelerator.GetKeyCode() == VK_ESCAPE);
  window_->window()->Close();
  return true;
}