forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_bindings.cc
More file actions
139 lines (115 loc) · 5.24 KB
/
window_bindings.cc
File metadata and controls
139 lines (115 loc) · 5.24 KB
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 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "content/nw/src/api/window_bindings.h"
#include "base/values.h"
#include "content/common/child_thread.h"
#include "content/nw/src/api/bindings_common.h"
#include "content/renderer/render_view_impl.h"
#include "grit/nw_resources.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
namespace api {
WindowBindings::WindowBindings()
: v8::Extension("window_bindings.js",
GetStringResource(
IDR_NW_API_WINDOW_BINDINGS_JS).data(),
0, // num dependencies.
NULL, // dependencies array.
GetStringResource(
IDR_NW_API_WINDOW_BINDINGS_JS).size()) {
}
WindowBindings::~WindowBindings() {
}
v8::Handle<v8::FunctionTemplate>
WindowBindings::GetNativeFunction(v8::Handle<v8::String> name) {
if (name->Equals(v8::String::New("BindToShell")))
return v8::FunctionTemplate::New(BindToShell);
else if (name->Equals(v8::String::New("CallObjectMethod")))
return v8::FunctionTemplate::New(CallObjectMethod);
else if (name->Equals(v8::String::New("CallObjectMethodSync")))
return v8::FunctionTemplate::New(CallObjectMethodSync);
else if (name->Equals(v8::String::New("GetWindowObject")))
return v8::FunctionTemplate::New(GetWindowObject);
return v8::FunctionTemplate::New();
}
// static
v8::Handle<v8::Value>
WindowBindings::BindToShell(const v8::Arguments& args) {
int routing_id = args[0]->Int32Value();
int object_id = args[1]->Int32Value();
remote::AllocateObject(routing_id, object_id, "Window", v8::Object::New());
return v8::Undefined();
}
// static
v8::Handle<v8::Value>
WindowBindings::CallObjectMethod(const v8::Arguments& args) {
v8::Local<v8::Object> self = args[0]->ToObject();
int routing_id = self->Get(v8::String::New("routing_id"))->Int32Value();
int object_id = self->Get(v8::String::New("id"))->Int32Value();
std::string method = *v8::String::Utf8Value(args[1]);
return remote::CallObjectMethod(
routing_id, object_id, "Window", method, args[2]);
}
// static
v8::Handle<v8::Value>
WindowBindings::CallObjectMethodSync(const v8::Arguments& args) {
v8::HandleScope scope;
v8::Local<v8::Object> self = args[0]->ToObject();
int routing_id = self->Get(v8::String::New("routing_id"))->Int32Value();
int object_id = self->Get(v8::String::New("id"))->Int32Value();
std::string method = *v8::String::Utf8Value(args[1]);
if (method == "GetZoomLevel") {
content::RenderViewImpl* render_view = static_cast<content::RenderViewImpl*>(
content::ChildThread::current()->ResolveRoute(routing_id));
if (!render_view)
return v8::ThrowException(v8::Exception::Error(v8::String::New(
"Unable to get render view in GetZoomLevel")));
float zoom_level = render_view->GetWebView()->zoomLevel();
v8::Local<v8::Array> array = v8::Array::New();
array->Set(0, v8::Number::New(zoom_level));
return scope.Close(array);
}
if (method == "SetZoomLevel") {
content::RenderViewImpl* render_view = static_cast<content::RenderViewImpl*>(
content::ChildThread::current()->ResolveRoute(routing_id));
if (!render_view)
return v8::ThrowException(v8::Exception::Error(v8::String::New(
"Unable to get render view in SetZoomLevel")));
double zoom_level = args[2]->ToNumber()->Value();
render_view->OnSetZoomLevel(zoom_level);
return v8::Undefined();
}
return remote::CallObjectMethodSync(
routing_id, object_id, "Window", method, args[2]);
}
// static
v8::Handle<v8::Value>
WindowBindings::GetWindowObject(const v8::Arguments& args) {
int routing_id = args[0]->Int32Value();
// Dark magic to digg out the RenderView from its id.
content::RenderViewImpl* render_view = static_cast<content::RenderViewImpl*>(
content::ChildThread::current()->ResolveRoute(routing_id));
if (!render_view)
return v8::ThrowException(v8::Exception::Error(v8::String::New(
"Unable to get render view in GetWindowObject")));
// Return the window object.
return render_view->GetWebView()->mainFrame()->mainWorldScriptContext()->
Global();
}
} // namespace api