summaryrefslogtreecommitdiffstats
path: root/content/shell/layout_test_controller_bindings.cc
blob: 20131adc6e2d3763961e9b7c3da6e37811b15da1 (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
// Copyright (c) 2012 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 "content/shell/layout_test_controller_bindings.h"

#include "base/string_piece.h"
#include "content/public/renderer/render_view.h"
#include "content/shell/shell_messages.h"
#include "grit/shell_resources.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"

using WebKit::WebFrame;
using WebKit::WebView;

namespace content {

namespace {

base::StringPiece GetStringResource(int resource_id) {
  return ResourceBundle::GetSharedInstance().GetRawDataResource(
      resource_id, ui::SCALE_FACTOR_NONE);
}

RenderView* GetCurrentRenderView() {
  WebFrame* frame = WebFrame::frameForCurrentContext();
  DCHECK(frame);
  if (!frame)
    return NULL;

  WebView* view = frame->view();
  if (!view)
    return NULL;  // can happen during closing.

  RenderView* render_view = RenderView::FromWebView(view);
  DCHECK(render_view);
  return render_view;
}

v8::Handle<v8::Value> NotifyDone(const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_NotifyDone(view->GetRoutingID()));
  return v8::Undefined();
}

v8::Handle<v8::Value> SetDumpAsText(const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_DumpAsText(view->GetRoutingID()));
  return v8::Undefined();
}

v8::Handle<v8::Value> SetDumpChildFramesAsText(const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_DumpChildFramesAsText(view->GetRoutingID()));
  return v8::Undefined();
}

v8::Handle<v8::Value> SetPrinting(const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_SetPrinting(view->GetRoutingID()));
  return v8::Undefined();
}

v8::Handle<v8::Value> SetShouldStayOnPageAfterHandlingBeforeUnload(
    const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  if (args.Length() != 1 || args[0]->IsBoolean())
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload(
      view->GetRoutingID(), args[0]->BooleanValue()));
  return v8::Undefined();
}

v8::Handle<v8::Value> SetWaitUntilDone(const v8::Arguments& args) {
  RenderView* view = GetCurrentRenderView();
  if (!view)
    return v8::Undefined();

  view->Send(new ShellViewHostMsg_WaitUntilDone(view->GetRoutingID()));
  return v8::Undefined();
}

}  // namespace

LayoutTestControllerBindings::LayoutTestControllerBindings()
    : v8::Extension("layout_test_controller.js",
                    GetStringResource(
                        IDR_CONTENT_SHELL_LAYOUT_TEST_CONTROLLER_JS).data(),
                    0,     // num dependencies.
                    NULL,  // dependencies array.
                    GetStringResource(
                        IDR_CONTENT_SHELL_LAYOUT_TEST_CONTROLLER_JS).size()) {
}

LayoutTestControllerBindings::~LayoutTestControllerBindings() {
}

v8::Handle<v8::FunctionTemplate>
LayoutTestControllerBindings::GetNativeFunction(v8::Handle<v8::String> name) {
  if (name->Equals(v8::String::New("NotifyDone")))
    return v8::FunctionTemplate::New(NotifyDone);
  if (name->Equals(v8::String::New("SetDumpAsText")))
    return v8::FunctionTemplate::New(SetDumpAsText);
  if (name->Equals(v8::String::New("SetDumpChildFramesAsText")))
    return v8::FunctionTemplate::New(SetDumpChildFramesAsText);
  if (name->Equals(v8::String::New("SetPrinting")))
    return v8::FunctionTemplate::New(SetPrinting);
  if (name->Equals(v8::String::New(
          "SetShouldStayOnPageAfterHandlingBeforeUnload"))) {
    return v8::FunctionTemplate::New(
        SetShouldStayOnPageAfterHandlingBeforeUnload);
  }
  if (name->Equals(v8::String::New("SetWaitUntilDone")))
    return v8::FunctionTemplate::New(SetWaitUntilDone);

  NOTREACHED();
  return v8::FunctionTemplate::New();
}

}  // namespace content