OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sky/engine/config.h" | 5 #include "sky/engine/config.h" |
6 #include "sky/engine/bindings/builtin.h" | 6 #include "sky/engine/bindings/builtin.h" |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "dart/runtime/include/dart_api.h" | 9 #include "dart/runtime/include/dart_api.h" |
10 #include "gen/sky/bindings/DartGlobal.h" | 10 #include "gen/sky/bindings/DartGlobal.h" |
11 #include "sky/engine/bindings/builtin_natives.h" | 11 #include "sky/engine/bindings/builtin_natives.h" |
12 #include "sky/engine/bindings/builtin_sky.h" | 12 #include "sky/engine/bindings/builtin_sky.h" |
13 #include "sky/engine/bindings/mojo_natives.h" | 13 #include "sky/engine/bindings/mojo_natives.h" |
14 #include "sky/engine/tonic/dart_builtin.h" | 14 #include "sky/engine/tonic/dart_builtin.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 namespace { | 17 namespace { |
18 | 18 |
| 19 const char* mojo_core_patch_resource_names_[] = { |
| 20 "/core/buffer_patch.dart", |
| 21 "/core/data_pipe_patch.dart", |
| 22 "/core/handle_patch.dart", |
| 23 "/core/handle_watcher_patch.dart", |
| 24 "/core/message_pipe_patch.dart", |
| 25 NULL, |
| 26 }; |
| 27 |
19 struct LibraryDescriptor { | 28 struct LibraryDescriptor { |
20 const char* url; | 29 const char* url; |
21 bool has_natives; | 30 bool has_natives; |
22 Dart_NativeEntrySymbol native_symbol; | 31 Dart_NativeEntrySymbol native_symbol; |
23 Dart_NativeEntryResolver native_resolver; | 32 Dart_NativeEntryResolver native_resolver; |
| 33 const char* patch_url; |
| 34 const char** patch_resources; |
24 }; | 35 }; |
25 | 36 |
26 const LibraryDescriptor kBuiltinLibraries[] = { | 37 const LibraryDescriptor kBuiltinLibraries[] = { |
27 /* { url_, has_natives_, native_symbol_, native_resolver_ } */ | 38 /* { url, has_natives, native_symbol, native_resolver, |
28 {"dart:sky_builtin", true, BuiltinNatives::NativeSymbol, BuiltinNatives::Nat
iveLookup}, | 39 patch_url, patch_paths } */ |
29 {"dart:sky", true, skySnapshotSymbolizer, skySnapshotResolver}, | 40 {"dart:sky_builtin", |
30 {"mojo:bindings", false, nullptr, nullptr}, | 41 true, |
31 {"mojo:core", true, MojoNativeSymbol, MojoNativeLookup}, | 42 BuiltinNatives::NativeSymbol, |
| 43 BuiltinNatives::NativeLookup, |
| 44 nullptr, |
| 45 nullptr}, |
| 46 {"dart:sky", |
| 47 true, |
| 48 skySnapshotSymbolizer, |
| 49 skySnapshotResolver, |
| 50 nullptr, |
| 51 nullptr}, |
| 52 {"mojo:bindings", false, nullptr, nullptr, nullptr, nullptr}, |
| 53 {"mojo:core", |
| 54 true, |
| 55 MojoNativeSymbol, |
| 56 MojoNativeLookup, |
| 57 "mojo:core-patch", |
| 58 mojo_core_patch_resource_names_}, |
32 }; | 59 }; |
33 | 60 |
34 } // namespace | 61 } // namespace |
35 | 62 |
36 void Builtin::SetNativeResolver(BuiltinLibraryId id) { | 63 struct ResourcesEntry { |
37 static_assert(arraysize(kBuiltinLibraries) == kInvalidLibrary, | 64 const char* path_; |
38 "Unexpected number of builtin libraries"); | 65 const char* resource_; |
39 DCHECK_GE(id, kBuiltinLibrary); | 66 int length_; |
40 DCHECK_LT(id, kInvalidLibrary); | 67 }; |
41 if (kBuiltinLibraries[id].has_natives) { | 68 |
42 Dart_Handle library = DartBuiltin::LookupLibrary(kBuiltinLibraries[id].url); | 69 extern ResourcesEntry __dart_embedder_patch_resources_[]; |
43 // Setup the native resolver for built in library functions. | 70 |
44 DART_CHECK_VALID( | 71 // Returns -1 if resource could not be found. |
45 Dart_SetNativeResolver(library, | 72 int Builtin::FindResource(const char* path, const uint8_t** resource) { |
46 kBuiltinLibraries[id].native_resolver, | 73 ResourcesEntry* table = &__dart_embedder_patch_resources_[0]; |
47 kBuiltinLibraries[id].native_symbol)); | 74 for (int i = 0; table[i].path_ != NULL; i++) { |
| 75 const ResourcesEntry& entry = table[i]; |
| 76 if (strcmp(path, entry.path_) == 0) { |
| 77 *resource = reinterpret_cast<const uint8_t*>(entry.resource_); |
| 78 DCHECK(entry.length_ > 0); |
| 79 return entry.length_; |
| 80 } |
| 81 } |
| 82 *resource = NULL; |
| 83 return -1; |
| 84 } |
| 85 |
| 86 Dart_Handle Builtin::GetStringResource(const char* resource_name) { |
| 87 const uint8_t* resource_string; |
| 88 int resource_length = FindResource(resource_name, &resource_string); |
| 89 if (resource_length > 0) { |
| 90 return Dart_NewStringFromUTF8(resource_string, resource_length); |
| 91 } |
| 92 return Dart_Null(); |
| 93 } |
| 94 |
| 95 // Patch all the specified patch files in the array 'patch_resources' into the |
| 96 // library specified in 'library'. |
| 97 void Builtin::LoadPatchFiles(Dart_Handle library, |
| 98 const char* patch_uri, |
| 99 const char** patch_resources) { |
| 100 for (intptr_t j = 0; patch_resources[j] != NULL; j++) { |
| 101 Dart_Handle patch_src = GetStringResource(patch_resources[j]); |
| 102 if (Dart_IsNull(patch_src)) { |
| 103 LOG(ERROR) << "Could not load resource " << patch_resources[j]; |
| 104 return; |
| 105 } |
| 106 // Prepend the patch library URI to form a unique script URI for the patch. |
| 107 intptr_t len = snprintf(NULL, 0, "%s%s", patch_uri, patch_resources[j]); |
| 108 char* patch_filename = reinterpret_cast<char*>(malloc(len + 1)); |
| 109 snprintf(patch_filename, len + 1, "%s%s", patch_uri, patch_resources[j]); |
| 110 Dart_Handle patch_file_uri = Dart_NewStringFromCString(patch_filename); |
| 111 DART_CHECK_VALID(patch_file_uri); |
| 112 free(patch_filename); |
| 113 DART_CHECK_VALID(Dart_LibraryLoadPatch(library, patch_file_uri, patch_src)); |
48 } | 114 } |
49 } | 115 } |
50 | 116 |
51 Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { | 117 Dart_Handle Builtin::GetLibrary(BuiltinLibraryId id) { |
52 static_assert(arraysize(kBuiltinLibraries) == kInvalidLibrary, | 118 static_assert(arraysize(kBuiltinLibraries) == kInvalidLibrary, |
53 "Unexpected number of builtin libraries"); | 119 "Unexpected number of builtin libraries"); |
54 DCHECK_GE(id, kBuiltinLibrary); | 120 DCHECK_GE(id, kBuiltinLibrary); |
55 DCHECK_LT(id, kInvalidLibrary); | 121 DCHECK_LT(id, kInvalidLibrary); |
56 Dart_Handle library = DartBuiltin::LookupLibrary(kBuiltinLibraries[id].url); | 122 Dart_Handle library = DartBuiltin::LookupLibrary(kBuiltinLibraries[id].url); |
57 DART_CHECK_VALID(library); | 123 DART_CHECK_VALID(library); |
58 return library; | 124 return library; |
59 } | 125 } |
60 | 126 |
| 127 void Builtin::PrepareLibrary(BuiltinLibraryId id) { |
| 128 Dart_Handle library = GetLibrary(id); |
| 129 DCHECK(!Dart_IsError(library)); |
| 130 if (kBuiltinLibraries[id].has_natives) { |
| 131 // Setup the native resolver for built in library functions. |
| 132 DART_CHECK_VALID( |
| 133 Dart_SetNativeResolver(library, |
| 134 kBuiltinLibraries[id].native_resolver, |
| 135 kBuiltinLibraries[id].native_symbol)); |
| 136 } |
| 137 if (kBuiltinLibraries[id].patch_url != nullptr) { |
| 138 DCHECK(kBuiltinLibraries[id].patch_resources != nullptr); |
| 139 LoadPatchFiles(library, kBuiltinLibraries[id].patch_url, |
| 140 kBuiltinLibraries[id].patch_resources); |
| 141 } |
| 142 } |
| 143 |
61 } // namespace blink | 144 } // namespace blink |
OLD | NEW |