OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/shell/layout_test_controller_bindings.h" | |
6 | |
7 #include "base/string_piece.h" | |
8 #include "content/public/renderer/render_view.h" | |
9 #include "content/shell/shell_messages.h" | |
10 #include "grit/shell_resources.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
13 #include "ui/base/layout.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 | |
16 using WebKit::WebFrame; | |
17 using WebKit::WebView; | |
18 | |
19 namespace content { | |
20 | |
21 namespace { | |
22 | |
23 base::StringPiece GetStringResource(int resource_id) { | |
24 return ResourceBundle::GetSharedInstance().GetRawDataResource( | |
25 resource_id, ui::SCALE_FACTOR_NONE); | |
26 } | |
27 | |
28 RenderView* GetCurrentRenderView() { | |
29 WebFrame* frame = WebFrame::frameForCurrentContext(); | |
30 DCHECK(frame); | |
31 if (!frame) | |
32 return NULL; | |
33 | |
34 WebView* view = frame->view(); | |
35 if (!view) | |
36 return NULL; // can happen during closing. | |
37 | |
38 RenderView* render_view = RenderView::FromWebView(view); | |
39 DCHECK(render_view); | |
40 return render_view; | |
41 } | |
42 | |
43 v8::Handle<v8::Value> NotifyDone(const v8::Arguments& args) { | |
44 RenderView* view = GetCurrentRenderView(); | |
45 if (!view) | |
46 return v8::Undefined(); | |
47 | |
48 view->Send(new ShellViewHostMsg_NotifyDone(view->GetRoutingID())); | |
49 return v8::Undefined(); | |
50 } | |
51 | |
52 v8::Handle<v8::Value> SetDumpAsText(const v8::Arguments& args) { | |
53 RenderView* view = GetCurrentRenderView(); | |
54 if (!view) | |
55 return v8::Undefined(); | |
56 | |
57 view->Send(new ShellViewHostMsg_DumpAsText(view->GetRoutingID())); | |
58 return v8::Undefined(); | |
59 } | |
60 | |
61 v8::Handle<v8::Value> SetDumpChildFramesAsText(const v8::Arguments& args) { | |
62 RenderView* view = GetCurrentRenderView(); | |
63 if (!view) | |
64 return v8::Undefined(); | |
65 | |
66 view->Send(new ShellViewHostMsg_DumpChildFramesAsText(view->GetRoutingID())); | |
67 return v8::Undefined(); | |
68 } | |
69 | |
70 v8::Handle<v8::Value> SetPrinting(const v8::Arguments& args) { | |
71 RenderView* view = GetCurrentRenderView(); | |
72 if (!view) | |
73 return v8::Undefined(); | |
74 | |
75 view->Send(new ShellViewHostMsg_SetPrinting(view->GetRoutingID())); | |
76 return v8::Undefined(); | |
77 } | |
78 | |
79 v8::Handle<v8::Value> SetShouldStayOnPageAfterHandlingBeforeUnload( | |
80 const v8::Arguments& args) { | |
81 RenderView* view = GetCurrentRenderView(); | |
82 if (!view) | |
83 return v8::Undefined(); | |
84 | |
85 if (args.Length() != 1 || !args[0]->IsBoolean()) | |
86 return v8::Undefined(); | |
87 | |
88 view->Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload( | |
89 view->GetRoutingID(), args[0]->BooleanValue())); | |
90 return v8::Undefined(); | |
91 } | |
92 | |
93 v8::Handle<v8::Value> SetWaitUntilDone(const v8::Arguments& args) { | |
94 RenderView* view = GetCurrentRenderView(); | |
95 if (!view) | |
96 return v8::Undefined(); | |
97 | |
98 view->Send(new ShellViewHostMsg_WaitUntilDone(view->GetRoutingID())); | |
99 return v8::Undefined(); | |
100 } | |
101 | |
102 v8::Handle<v8::Value> NotImplemented(const v8::Arguments& args) { | |
103 RenderView* view = GetCurrentRenderView(); | |
104 if (!view) | |
105 return v8::Undefined(); | |
106 | |
107 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) | |
108 return v8::Undefined(); | |
109 | |
110 view->Send(new ShellViewHostMsg_NotImplemented( | |
111 view->GetRoutingID(), | |
112 *v8::String::AsciiValue(args[0]), | |
113 *v8::String::AsciiValue(args[1]))); | |
114 return v8::Undefined(); | |
115 } | |
116 | |
117 } // namespace | |
118 | |
119 LayoutTestControllerBindings::LayoutTestControllerBindings() | |
120 : v8::Extension("layout_test_controller.js", | |
121 GetStringResource( | |
122 IDR_CONTENT_SHELL_LAYOUT_TEST_CONTROLLER_JS).data(), | |
123 0, // num dependencies. | |
124 NULL, // dependencies array. | |
125 GetStringResource( | |
126 IDR_CONTENT_SHELL_LAYOUT_TEST_CONTROLLER_JS).size()) { | |
127 } | |
128 | |
129 LayoutTestControllerBindings::~LayoutTestControllerBindings() { | |
130 } | |
131 | |
132 v8::Handle<v8::FunctionTemplate> | |
133 LayoutTestControllerBindings::GetNativeFunction(v8::Handle<v8::String> name) { | |
134 if (name->Equals(v8::String::New("NotifyDone"))) | |
135 return v8::FunctionTemplate::New(NotifyDone); | |
136 if (name->Equals(v8::String::New("SetDumpAsText"))) | |
137 return v8::FunctionTemplate::New(SetDumpAsText); | |
138 if (name->Equals(v8::String::New("SetDumpChildFramesAsText"))) | |
139 return v8::FunctionTemplate::New(SetDumpChildFramesAsText); | |
140 if (name->Equals(v8::String::New("SetPrinting"))) | |
141 return v8::FunctionTemplate::New(SetPrinting); | |
142 if (name->Equals(v8::String::New( | |
143 "SetShouldStayOnPageAfterHandlingBeforeUnload"))) { | |
144 return v8::FunctionTemplate::New( | |
145 SetShouldStayOnPageAfterHandlingBeforeUnload); | |
146 } | |
147 if (name->Equals(v8::String::New("SetWaitUntilDone"))) | |
148 return v8::FunctionTemplate::New(SetWaitUntilDone); | |
149 if (name->Equals(v8::String::New("NotImplemented"))) | |
150 return v8::FunctionTemplate::New(NotImplemented); | |
151 | |
152 NOTREACHED(); | |
153 return v8::FunctionTemplate::New(); | |
154 } | |
155 | |
156 } // namespace content | |
OLD | NEW |