Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: services/js/js_app.cc

Issue 780213002: Put code in //services/js in namespace js (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/js/js_app.h" 5 #include "services/js/js_app.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "gin/converter.h" 9 #include "gin/converter.h"
10 #include "gin/modules/module_registry.h" 10 #include "gin/modules/module_registry.h"
11 #include "gin/try_catch.h" 11 #include "gin/try_catch.h"
12 #include "mojo/common/data_pipe_utils.h" 12 #include "mojo/common/data_pipe_utils.h"
13 #include "mojo/edk/js/core.h" 13 #include "mojo/edk/js/core.h"
14 #include "mojo/edk/js/handle.h" 14 #include "mojo/edk/js/handle.h"
15 #include "mojo/edk/js/support.h" 15 #include "mojo/edk/js/support.h"
16 #include "mojo/public/cpp/bindings/interface_request.h" 16 #include "mojo/public/cpp/bindings/interface_request.h"
17 #include "services/js/js_app_bridge_module.h" 17 #include "services/js/js_app_bridge_module.h"
18 #include "services/js/js_app_message_loop_observers.h" 18 #include "services/js/js_app_message_loop_observers.h"
19 #include "services/js/js_app_shell.h" 19 #include "services/js/js_app_shell.h"
20 20
21 namespace mojo {
22 namespace js { 21 namespace js {
23 22
24 namespace { 23 namespace {
25 24
26 // If true then result is the value of value.property_name. 25 // If true then result is the value of value.property_name.
27 bool GetValueProperty(v8::Isolate* isolate, 26 bool GetValueProperty(v8::Isolate* isolate,
28 v8::Handle<v8::Value> value, 27 v8::Handle<v8::Value> value,
29 const std::string& property_name, 28 const std::string& property_name,
30 v8::Handle<v8::Value>* result) { 29 v8::Handle<v8::Value>* result) {
31 if (!value->IsObject()) 30 if (!value->IsObject())
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 v8::Local<v8::Value> connection_value = 72 v8::Local<v8::Value> connection_value =
74 get_connection_method->Call(service_provider, 0, nullptr); 73 get_connection_method->Call(service_provider, 0, nullptr);
75 return GetValueMojoHandle( 74 return GetValueMojoHandle(
76 isolate, connection_value, "messagePipeHandle", result); 75 isolate, connection_value, "messagePipeHandle", result);
77 } 76 }
78 77
79 } // namespace 78 } // namespace
80 79
81 const char JSApp::kMainModuleName[] = "main"; 80 const char JSApp::kMainModuleName[] = "main";
82 81
83 JSApp::JSApp(ShellPtr shell, URLResponsePtr response) : shell_(shell.Pass()) { 82 JSApp::JSApp(mojo::ShellPtr shell, mojo::URLResponsePtr response)
83 : shell_(shell.Pass()) {
84 v8::Isolate* isolate = isolate_holder_.isolate(); 84 v8::Isolate* isolate = isolate_holder_.isolate();
85 message_loop_observers_.reset(new JSAppMessageLoopObservers(isolate)); 85 message_loop_observers_.reset(new JSAppMessageLoopObservers(isolate));
86 86
87 DCHECK(!response.is_null()); 87 DCHECK(!response.is_null());
88 std::string url(response->url); 88 std::string url(response->url);
89 std::string source; 89 std::string source;
90 CHECK(common::BlockingCopyToString(response->body.Pass(), &source)); 90 CHECK(mojo::common::BlockingCopyToString(response->body.Pass(), &source));
91 91
92 runner_delegate_.AddBuiltinModule(AppBridge::kModuleName, 92 runner_delegate_.AddBuiltinModule(AppBridge::kModuleName,
93 base::Bind(&AppBridge::GetModule, base::Unretained(this))); 93 base::Bind(&AppBridge::GetModule, base::Unretained(this)));
94 94
95 shell_runner_.reset(new gin::ShellRunner(&runner_delegate_, isolate)); 95 shell_runner_.reset(new gin::ShellRunner(&runner_delegate_, isolate));
96 gin::Runner::Scope scope(shell_runner_.get()); 96 gin::Runner::Scope scope(shell_runner_.get());
97 shell_runner_->Run(source.c_str(), kMainModuleName); 97 shell_runner_->Run(source.c_str(), kMainModuleName);
98 98
99 gin::ModuleRegistry* registry = 99 gin::ModuleRegistry* registry =
100 gin::ModuleRegistry::From(shell_runner_->GetContextHolder()->context()); 100 gin::ModuleRegistry::From(shell_runner_->GetContextHolder()->context());
(...skipping 27 matching lines...) Expand all
128 } 128 }
129 129
130 void JSApp::ConnectToApplication(const std::string& application_url, 130 void JSApp::ConnectToApplication(const std::string& application_url,
131 v8::Handle<v8::Value> service_provider) { 131 v8::Handle<v8::Value> service_provider) {
132 gin::Runner::Scope scope(shell_runner_.get()); 132 gin::Runner::Scope scope(shell_runner_.get());
133 v8::Isolate* isolate = isolate_holder_.isolate(); 133 v8::Isolate* isolate = isolate_holder_.isolate();
134 134
135 mojo::Handle handle; 135 mojo::Handle handle;
136 if (GetConnectionMessagePipeHandle(isolate, service_provider, &handle) || 136 if (GetConnectionMessagePipeHandle(isolate, service_provider, &handle) ||
137 gin::ConvertFromV8(isolate, service_provider, &handle)) { 137 gin::ConvertFromV8(isolate, service_provider, &handle)) {
138 MessagePipeHandle message_pipe_handle(handle.value()); 138 mojo::MessagePipeHandle message_pipe_handle(handle.value());
139 ScopedMessagePipeHandle scoped_handle(message_pipe_handle); 139 mojo::ScopedMessagePipeHandle scoped_handle(message_pipe_handle);
140 shell_->ConnectToApplication( 140 shell_->ConnectToApplication(
141 application_url, MakeRequest<ServiceProvider>(scoped_handle.Pass())); 141 application_url,
142 mojo::MakeRequest<mojo::ServiceProvider>(scoped_handle.Pass()));
142 } 143 }
143 } 144 }
144 145
145 void JSApp::CallAppInstanceMethod( 146 void JSApp::CallAppInstanceMethod(
146 const std::string& name, int argc, v8::Handle<v8::Value> argv[]) { 147 const std::string& name, int argc, v8::Handle<v8::Value> argv[]) {
147 v8::Isolate* isolate = isolate_holder_.isolate(); 148 v8::Isolate* isolate = isolate_holder_.isolate();
148 v8::Local<v8::Object> app = 149 v8::Local<v8::Object> app =
149 v8::Local<v8::Object>::New(isolate, app_instance_); 150 v8::Local<v8::Object>::New(isolate, app_instance_);
150 151
151 v8::Handle<v8::Function> app_method; 152 v8::Handle<v8::Function> app_method;
152 GetValueMethod(isolate, app, name, &app_method); 153 GetValueMethod(isolate, app, name, &app_method);
153 shell_runner_->Call(app_method, app, argc, argv); 154 shell_runner_->Call(app_method, app, argc, argv);
154 } 155 }
155 156
156 void JSApp::Initialize(Array<String> app_args) { 157 void JSApp::Initialize(mojo::Array<mojo::String> app_args) {
157 gin::Runner::Scope scope(shell_runner_.get()); 158 gin::Runner::Scope scope(shell_runner_.get());
158 v8::Isolate* isolate = isolate_holder_.isolate(); 159 v8::Isolate* isolate = isolate_holder_.isolate();
159 v8::Handle<v8::Value> argv[] = { 160 v8::Handle<v8::Value> argv[] = {
160 gin::ConvertToV8(isolate, app_args.To<std::vector<std::string>>()), 161 gin::ConvertToV8(isolate, app_args.To<std::vector<std::string>>()),
161 }; 162 };
162 CallAppInstanceMethod("initialize", 1, argv); 163 CallAppInstanceMethod("initialize", 1, argv);
163 } 164 }
164 165
165 void JSApp::AcceptConnection(const String& requestor_url, 166 void JSApp::AcceptConnection(const mojo::String& requestor_url,
166 ServiceProviderPtr provider) { 167 mojo::ServiceProviderPtr provider) {
167 gin::Runner::Scope scope(shell_runner_.get()); 168 gin::Runner::Scope scope(shell_runner_.get());
168 v8::Isolate* isolate = isolate_holder_.isolate(); 169 v8::Isolate* isolate = isolate_holder_.isolate();
169 v8::Handle<v8::Value> argv[] = { 170 v8::Handle<v8::Value> argv[] = {
170 gin::ConvertToV8(isolate, requestor_url.To<std::string>()), 171 gin::ConvertToV8(isolate, requestor_url.To<std::string>()),
171 gin::ConvertToV8(isolate, provider.PassMessagePipe().release()), 172 gin::ConvertToV8(isolate, provider.PassMessagePipe().release()),
172 }; 173 };
173 CallAppInstanceMethod("acceptConnection_", arraysize(argv), argv); 174 CallAppInstanceMethod("acceptConnection_", arraysize(argv), argv);
174 } 175 }
175 176
176 void JSApp::Quit() { 177 void JSApp::Quit() {
177 base::MessageLoop::current()->PostTask( 178 base::MessageLoop::current()->PostTask(
178 FROM_HERE, base::Bind(&JSApp::QuitInternal, base::Unretained(this))); 179 FROM_HERE, base::Bind(&JSApp::QuitInternal, base::Unretained(this)));
179 } 180 }
180 181
181 void JSApp::QuitInternal() { 182 void JSApp::QuitInternal() {
182 shell_runner_.reset(); 183 shell_runner_.reset();
183 base::MessageLoop::current()->QuitWhenIdle(); 184 base::MessageLoop::current()->QuitWhenIdle();
184 } 185 }
185 186
186 } // namespace js 187 } // namespace js
187 } // namespace mojo
188 188
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698