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 "sky/viewer/internals.h" | |
6 | |
7 #include "mojo/public/cpp/application/connect.h" | |
8 #include "mojo/public/cpp/bindings/array.h" | |
9 #include "sky/engine/tonic/dart_builtin.h" | |
10 #include "sky/engine/tonic/dart_converter.h" | |
11 #include "sky/engine/tonic/dart_error.h" | |
12 #include "sky/viewer/document_view.h" | |
13 #include "sky/viewer/runtime_flags.h" | |
14 #include <limits> | |
15 | |
16 using namespace blink; | |
17 | |
18 namespace sky { | |
19 namespace { | |
20 | |
21 int kInternalsKey = 0; | |
22 | |
23 Internals* GetInternals() { | |
24 DartState* state = DartState::Current(); | |
25 return static_cast<Internals*>(state->GetUserData(&kInternalsKey)); | |
26 } | |
27 | |
28 void NotifyTestComplete(Dart_NativeArguments args) { | |
29 Dart_Handle test_result = Dart_GetNativeArgument(args, 0); | |
30 GetInternals()->NotifyTestComplete(StdStringFromDart(test_result)); | |
31 } | |
32 | |
33 void TakeRootBundleHandle(Dart_NativeArguments args) { | |
34 Dart_SetIntegerReturnValue(args, 0); | |
35 } | |
36 | |
37 void TakeShellProxyHandle(Dart_NativeArguments args) { | |
38 Dart_SetIntegerReturnValue(args, | |
39 GetInternals()->TakeShellProxyHandle().value()); | |
40 } | |
41 | |
42 void TakeServicesProvidedByEmbedder(Dart_NativeArguments args) { | |
43 Dart_SetIntegerReturnValue( | |
44 args, GetInternals()->TakeServicesProvidedByEmbedder().value()); | |
45 } | |
46 | |
47 void TakeServicesProvidedToEmbedder(Dart_NativeArguments args) { | |
48 Dart_SetIntegerReturnValue( | |
49 args, GetInternals()->TakeServicesProvidedToEmbedder().value()); | |
50 } | |
51 | |
52 void TakeServiceRegistry(Dart_NativeArguments args) { | |
53 Dart_SetIntegerReturnValue( | |
54 args, GetInternals()->TakeServiceRegistry().value()); | |
55 } | |
56 | |
57 const DartBuiltin::Natives kNativeFunctions[] = { | |
58 {"notifyTestComplete", NotifyTestComplete, 1}, | |
59 {"takeRootBundleHandle", TakeRootBundleHandle, 0}, | |
60 {"takeServiceRegistry", TakeServiceRegistry, 0}, | |
61 {"takeServicesProvidedByEmbedder", TakeServicesProvidedByEmbedder, 0}, | |
62 {"takeServicesProvidedToEmbedder", TakeServicesProvidedToEmbedder, 0}, | |
63 {"takeShellProxyHandle", TakeShellProxyHandle, 0}, | |
64 }; | |
65 | |
66 const DartBuiltin& GetBuiltin() { | |
67 static DartBuiltin& builtin = *new DartBuiltin(kNativeFunctions, | |
68 arraysize(kNativeFunctions)); | |
69 return builtin; | |
70 } | |
71 | |
72 Dart_NativeFunction Resolver(Dart_Handle name, | |
73 int argument_count, | |
74 bool* auto_setup_scope) { | |
75 return GetBuiltin().Resolver(name, argument_count, auto_setup_scope); | |
76 } | |
77 | |
78 const uint8_t* Symbolizer(Dart_NativeFunction native_function) { | |
79 return GetBuiltin().Symbolizer(native_function); | |
80 } | |
81 | |
82 const char kLibraryName[] = "dart:sky.internals"; | |
83 | |
84 } // namespace | |
85 | |
86 void Internals::Create(Dart_Isolate isolate, DocumentView* document_view) { | |
87 DartState* state = DartState::From(isolate); | |
88 state->SetUserData(&kInternalsKey, new Internals(document_view)); | |
89 Dart_Handle library = | |
90 Dart_LookupLibrary(Dart_NewStringFromCString(kLibraryName)); | |
91 CHECK(!LogIfError(library)); | |
92 CHECK(!LogIfError(Dart_SetNativeResolver(library, Resolver, Symbolizer))); | |
93 } | |
94 | |
95 Internals::Internals(DocumentView* document_view) | |
96 : document_view_(document_view->GetWeakPtr()), | |
97 shell_binding_(this) { | |
98 test_harness_ = document_view_->TakeTestHarness(); | |
99 } | |
100 | |
101 Internals::~Internals() { | |
102 } | |
103 | |
104 void Internals::NotifyTestComplete(const std::string& test_result) { | |
105 if (!RuntimeFlags::Get().testing()) | |
106 return; | |
107 std::vector<unsigned char> pixels; | |
108 document_view_->GetPixelsForTesting(&pixels); | |
109 if (test_harness_) { | |
110 test_harness_->OnTestComplete(test_result, | |
111 mojo::Array<uint8_t>::From(pixels)); | |
112 } | |
113 } | |
114 | |
115 mojo::Handle Internals::TakeServicesProvidedToEmbedder() { | |
116 if (!document_view_) | |
117 return mojo::Handle(); | |
118 return document_view_->TakeServicesProvidedToEmbedder().release(); | |
119 } | |
120 | |
121 mojo::Handle Internals::TakeServicesProvidedByEmbedder() { | |
122 if (!document_view_) | |
123 return mojo::Handle(); | |
124 return document_view_->TakeServicesProvidedByEmbedder().release(); | |
125 } | |
126 | |
127 mojo::Handle Internals::TakeServiceRegistry() { | |
128 if (!document_view_) | |
129 return mojo::Handle(); | |
130 return document_view_->TakeServiceRegistry().release(); | |
131 } | |
132 | |
133 // Returns a MessagePipe handle that's connected to this Shell. The caller | |
134 // owns the handle and is expected to use it to create the JS Application for | |
135 // the DocumentView. | |
136 mojo::Handle Internals::TakeShellProxyHandle() { | |
137 mojo::ShellPtr shell; | |
138 if (!shell_binding_.is_bound()) | |
139 shell_binding_.Bind(GetProxy(&shell)); | |
140 return shell.PassInterface().PassHandle().release(); | |
141 } | |
142 | |
143 void Internals::ConnectToApplication( | |
144 const mojo::String& application_url, | |
145 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
146 mojo::ServiceProviderPtr exposed_services) { | |
147 if (document_view_) { | |
148 document_view_->shell()->ConnectToApplication( | |
149 application_url, services.Pass(), exposed_services.Pass()); | |
150 } | |
151 } | |
152 | |
153 } // namespace sky | |
OLD | NEW |