summaryrefslogtreecommitdiffstats
path: root/webkit/glue/inspector_client_impl.cc
blob: 6e58a87bd6488da68820de7bbf7cbb3a94351a52 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 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 "config.h"

#include "base/compiler_specific.h"

MSVC_PUSH_WARNING_LEVEL(0);
#include "DOMWindow.h"
#include "FloatRect.h"
#include "InspectorController.h"
#include "Page.h"
#include "Settings.h"
#include <wtf/Vector.h>
MSVC_POP_WARNING();

#undef LOG
#include "base/logging.h"
#include "base/gfx/rect.h"
#include "base/string_util.h"
#include "webkit/api/public/WebRect.h"
#include "webkit/api/public/WebURL.h"
#include "webkit/api/public/WebURLRequest.h"
#include "webkit/glue/glue_util.h"
#include "webkit/glue/inspector_client_impl.h"
#include "webkit/glue/webdevtoolsagent_impl.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webview_impl.h"
#include "googleurl/src/gurl.h"
#include "net/base/net_util.h"

using namespace WebCore;

using WebKit::WebRect;
using WebKit::WebSize;
using WebKit::WebURLRequest;

static const float kDefaultInspectorXPos = 10;
static const float kDefaultInspectorYPos = 50;
static const float kDefaultInspectorHeight = 640;
static const float kDefaultInspectorWidth = 480;

InspectorClientImpl::InspectorClientImpl(WebViewImpl* webView)
    : inspected_web_view_(webView) {
  ASSERT(inspected_web_view_);
}

InspectorClientImpl::~InspectorClientImpl() {
}

void InspectorClientImpl::inspectorDestroyed() {
  // Our lifetime is bound to the WebViewImpl.
}

Page* InspectorClientImpl::createPage() {
  // This method should never be called in Chrome as inspector front-end lives
  // in a separate process.
  NOTREACHED();
  return NULL;
}

void InspectorClientImpl::showWindow() {
  DCHECK(inspected_web_view_->GetWebDevToolsAgentImpl());
  InspectorController* inspector =
      inspected_web_view_->page()->inspectorController();
  inspector->setWindowVisible(true);
}

void InspectorClientImpl::closeWindow() {
  if (inspected_web_view_->page())
    inspected_web_view_->page()->inspectorController()->setWindowVisible(false);
}

bool InspectorClientImpl::windowVisible() {
  DCHECK(inspected_web_view_->GetWebDevToolsAgentImpl());
  return false;
}

void InspectorClientImpl::attachWindow() {
  // TODO(jackson): Implement this
}

void InspectorClientImpl::detachWindow() {
  // TODO(jackson): Implement this
}

void InspectorClientImpl::setAttachedWindowHeight(unsigned int height) {
  // TODO(dglazkov): Implement this
  NOTIMPLEMENTED();
}

static void invalidateNodeBoundingRect(WebViewImpl* web_view) {
  // TODO(ojan): http://b/1143996 Is it important to just invalidate the rect
  // of the node region given that this is not on a critical codepath?
  // In order to do so, we'd have to take scrolling into account.
  const WebSize& size = web_view->size();
  WebRect damaged_rect(0, 0, size.width, size.height);
  if (web_view->client())
    web_view->client()->didInvalidateRect(damaged_rect);
}

void InspectorClientImpl::highlight(Node* node) {
  // InspectorController does the actually tracking of the highlighted node
  // and the drawing of the highlight. Here we just make sure to invalidate
  // the rects of the old and new nodes.
  hideHighlight();
}

void InspectorClientImpl::hideHighlight() {
  // TODO: Should be able to invalidate a smaller rect.
  invalidateNodeBoundingRect(inspected_web_view_);
}

void InspectorClientImpl::inspectedURLChanged(const String& newURL) {
  // TODO(jackson): Implement this
}

String InspectorClientImpl::localizedStringsURL() {
  NOTIMPLEMENTED();
  return String();
}

String InspectorClientImpl::hiddenPanels() {
  // Enumerate tabs that are currently disabled.
  return "scripts,profiles,databases";
}

void InspectorClientImpl::populateSetting(
    const String& key,
    InspectorController::Setting& setting) {
  LoadSettings();
  if (settings_->contains(key))
    setting = settings_->get(key);
}

void InspectorClientImpl::storeSetting(
    const String& key,
    const InspectorController::Setting& setting) {
  LoadSettings();
  settings_->set(key, setting);
  SaveSettings();
}

void InspectorClientImpl::removeSetting(const String& key) {
  LoadSettings();
  settings_->remove(key);
  SaveSettings();
}

void InspectorClientImpl::inspectorWindowObjectCleared() {
  NOTIMPLEMENTED();
}

void InspectorClientImpl::LoadSettings() {
  if (settings_)
    return;

  settings_.set(new SettingsMap);
  String data = webkit_glue::WebStringToString(
      inspected_web_view_->inspectorSettings());
  if (data.isEmpty())
    return;

  Vector<String> entries;
  data.split("\n", entries);
  for (Vector<String>::iterator it = entries.begin();
       it != entries.end(); ++it) {
    Vector<String> tokens;
    it->split(":", tokens);
    if (tokens.size() != 3)
      continue;

    String name = decodeURLEscapeSequences(tokens[0]);
    String type = tokens[1];
    InspectorController::Setting setting;
    bool ok = true;
    if (type == "string")
      setting.set(decodeURLEscapeSequences(tokens[2]));
    else if (type == "double")
      setting.set(tokens[2].toDouble(&ok));
    else if (type == "integer")
      setting.set(static_cast<long>(tokens[2].toInt(&ok)));
    else if (type == "boolean")
      setting.set(tokens[2] == "true");
    else
      continue;

    if (ok)
      settings_->set(name, setting);
  }
}

void InspectorClientImpl::SaveSettings() {
  String data;
  for (SettingsMap::iterator it = settings_->begin(); it != settings_->end();
       ++it) {
    String entry;
    InspectorController::Setting value = it->second;
    String name = encodeWithURLEscapeSequences(it->first);
    switch (value.type()) {
      case InspectorController::Setting::StringType:
        entry = String::format(
            "%s:string:%s",
            name.utf8().data(),
            encodeWithURLEscapeSequences(value.string()).utf8().data());
        break;
      case InspectorController::Setting::DoubleType:
        entry = String::format(
            "%s:double:%f",
            name.utf8().data(),
            value.doubleValue());
        break;
      case InspectorController::Setting::IntegerType:
        entry = String::format(
            "%s:integer:%ld",
            name.utf8().data(),
            value.integerValue());
        break;
      case InspectorController::Setting::BooleanType:
        entry = String::format("%s:boolean:%s",
                               name.utf8().data(),
                               value.booleanValue() ? "true" : "false");
        break;
      case InspectorController::Setting::StringVectorType:
        NOTIMPLEMENTED();
        break;
      default:
        NOTREACHED();
        break;
    }
    data.append(entry);
    data.append("\n");
  }
  inspected_web_view_->setInspectorSettings(
      webkit_glue::StringToWebString(data));
  if (inspected_web_view_->client())
    inspected_web_view_->client()->didUpdateInspectorSettings();
}