OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/apps/js/mojo_module.h" | |
6 | |
7 #include "gin/arguments.h" | |
8 #include "gin/converter.h" | |
9 #include "gin/object_template_builder.h" | |
10 #include "gin/per_isolate_data.h" | |
11 #include "mojo/apps/js/js_app.h" | |
12 #include "mojo/apps/js/mojo_module.h" | |
13 #include "mojo/bindings/js/handle.h" | |
14 #include "mojo/common/data_pipe_utils.h" | |
15 | |
16 namespace mojo { | |
17 namespace apps { | |
18 | |
19 namespace { | |
20 | |
21 // TODO(hansmuller): define a new gin nullable string type for the return value. | |
22 std::string CopyToString(const gin::Arguments& args, Handle source) { | |
23 // TODO(hansmuller); if !pipe.is_valid() throw an error. | |
24 DataPipeConsumerHandle handle(source.value()); | |
25 ScopedDataPipeConsumerHandle scoped_handle(handle); | |
26 std::string result; | |
27 if (mojo::common::BlockingCopyToString(scoped_handle.Pass(), &result)) | |
28 return result; | |
29 | |
30 args.isolate()->ThrowException(v8::Exception::Error( | |
31 gin::StringToV8(args.isolate(), "unable to read data pipe"))); | |
32 return ""; | |
33 } | |
34 | |
35 gin::WrapperInfo g_wrapper_info = {gin::kEmbedderNativeGin}; | |
36 | |
37 } // namespace | |
38 | |
39 const char Mojo::kModuleName[] = "mojo"; | |
Aaron Boodman
2014/09/11 05:24:53
This is not used.
hansmuller
2014/09/11 16:44:44
It's used by the mojo AddBuiltinModule() call, whi
| |
40 | |
41 v8::Local<v8::Value> Mojo::GetModule(JSApp* js_app, v8::Isolate* isolate) { | |
42 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | |
43 v8::Local<v8::ObjectTemplate> templ = | |
44 data->GetObjectTemplate(&g_wrapper_info); | |
45 | |
46 // The lifetime of the the js_app is managed by the content_handler_app_. | |
47 if (templ.IsEmpty()) { | |
48 templ = gin::ObjectTemplateBuilder(isolate) | |
49 .SetMethod("connectToService", | |
50 base::Bind(&JSApp::ConnectToService, | |
51 base::Unretained(js_app))) | |
52 .SetMethod("copyToString", CopyToString) | |
53 .SetMethod("quit", | |
54 base::Bind(&JSApp::Quit, base::Unretained(js_app))) | |
55 .Build(); | |
56 data->SetObjectTemplate(&g_wrapper_info, templ); | |
57 } | |
58 | |
59 return templ->NewInstance(); | |
60 } | |
61 | |
62 } // namespace apps | |
63 } // namespace mojo | |
OLD | NEW |