OLD | NEW |
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 <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/i18n/icu_util.h" | 10 #include "base/i18n/icu_util.h" |
9 #include "dart/runtime/include/dart_api.h" | 11 #include "dart/runtime/include/dart_api.h" |
10 #include "mojo/dart/embedder/builtin.h" | 12 #include "mojo/dart/embedder/builtin.h" |
11 #include "mojo/dart/embedder/mojo_natives.h" | 13 #include "mojo/dart/embedder/mojo_natives.h" |
12 | 14 |
13 namespace mojo { | 15 namespace mojo { |
14 namespace dart { | 16 namespace dart { |
15 | 17 |
| 18 struct ResourcesEntry { |
| 19 const char* path_; |
| 20 const char* resource_; |
| 21 int length_; |
| 22 }; |
| 23 |
| 24 extern ResourcesEntry __dart_embedder_patch_resources_[]; |
| 25 |
| 26 // Returns -1 if resource could not be found. |
| 27 static int FindResource(const char* path, const char** resource) { |
| 28 ResourcesEntry* table = &__dart_embedder_patch_resources_[0]; |
| 29 for (int i = 0; table[i].path_ != NULL; i++) { |
| 30 const ResourcesEntry& entry = table[i]; |
| 31 if (strcmp(path, entry.path_) == 0) { |
| 32 *resource = entry.resource_; |
| 33 DCHECK(entry.length_ > 0); |
| 34 return entry.length_; |
| 35 } |
| 36 } |
| 37 *resource = NULL; |
| 38 return -1; |
| 39 } |
| 40 |
| 41 const char* __mojo_core_patch_resources[] = { |
| 42 "/core/buffer_patch.dart", |
| 43 "/core/data_pipe_patch.dart", |
| 44 "/core/handle_patch.dart", |
| 45 "/core/handle_watcher_patch.dart", |
| 46 "/core/message_pipe_patch.dart", |
| 47 NULL, |
| 48 }; |
| 49 |
16 Builtin::builtin_lib_props Builtin::builtin_libraries_[] = { | 50 Builtin::builtin_lib_props Builtin::builtin_libraries_[] = { |
17 /* { url_, has_natives_, native_symbol_, native_resolver_ } */ | 51 /* { url_, has_natives_, native_symbol_, native_resolver_, |
18 {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup}, | 52 patch_url_, patch_paths_ } */ |
19 {"dart:mojo_bindings", false, nullptr, nullptr}, | 53 {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup, |
20 {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup}, | 54 nullptr, nullptr }, |
| 55 {"dart:mojo_bindings", false, nullptr, nullptr, |
| 56 nullptr, nullptr }, |
| 57 {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup, |
| 58 "dart:mojo_core-patch", __mojo_core_patch_resources }, |
21 }; | 59 }; |
22 | 60 |
23 uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; | 61 uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; |
24 | 62 |
25 Dart_Handle Builtin::NewError(const char* format, ...) { | 63 Dart_Handle Builtin::NewError(const char* format, ...) { |
26 va_list args; | 64 va_list args; |
27 va_start(args, format); | 65 va_start(args, format); |
28 intptr_t len = vsnprintf(NULL, 0, format, args); | 66 intptr_t len = vsnprintf(nullptr, 0, format, args); |
29 va_end(args); | 67 va_end(args); |
30 | 68 |
31 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); | 69 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); |
32 va_list args2; | 70 va_list args2; |
33 va_start(args2, format); | 71 va_start(args2, format); |
34 vsnprintf(buffer, (len + 1), format, args2); | 72 vsnprintf(buffer, (len + 1), format, args2); |
35 va_end(args2); | 73 va_end(args2); |
36 | 74 |
37 return Dart_NewApiError(buffer); | 75 return Dart_NewApiError(buffer); |
38 } | 76 } |
39 | 77 |
40 void Builtin::SetNativeResolver(BuiltinLibraryId id) { | 78 void Builtin::SetNativeResolver(BuiltinLibraryId id) { |
41 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == | 79 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == |
42 kInvalidLibrary, "Unexpected number of builtin libraries"); | 80 kInvalidLibrary, "Unexpected number of builtin libraries"); |
43 DCHECK_GE(id, kBuiltinLibrary); | 81 DCHECK_GE(id, kBuiltinLibrary); |
44 DCHECK_LT(id, kInvalidLibrary); | 82 DCHECK_LT(id, kInvalidLibrary); |
45 if (builtin_libraries_[id].has_natives_) { | 83 if (builtin_libraries_[id].has_natives_) { |
46 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); | 84 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); |
47 Dart_Handle library = Dart_LookupLibrary(url); | 85 Dart_Handle library = Dart_LookupLibrary(url); |
48 DCHECK(!Dart_IsError(library)); | 86 DCHECK(!Dart_IsError(library)); |
49 // Setup the native resolver for built in library functions. | 87 // Setup the native resolver for built in library functions. |
50 DART_CHECK_VALID( | 88 DART_CHECK_VALID( |
51 Dart_SetNativeResolver(library, | 89 Dart_SetNativeResolver(library, |
52 builtin_libraries_[id].native_resolver_, | 90 builtin_libraries_[id].native_resolver_, |
53 builtin_libraries_[id].native_symbol_)); | 91 builtin_libraries_[id].native_symbol_)); |
54 } | 92 } |
55 } | 93 } |
56 | 94 |
| 95 Dart_Handle Builtin::GetStringResource(const char* resource_name) { |
| 96 const char* resource_string; |
| 97 int resource_length = FindResource(resource_name, &resource_string); |
| 98 if (resource_length > 0) { |
| 99 return Dart_NewStringFromCString(resource_string); |
| 100 } |
| 101 return NewError("Could not find resource %s", resource_name); |
| 102 } |
| 103 |
| 104 // Patch all the specified patch files in the array 'patch_resources' into the |
| 105 // library specified in 'library'. |
| 106 void Builtin::LoadPatchFiles(Dart_Handle library, |
| 107 const char* patch_uri, |
| 108 const char** patch_resources) { |
| 109 for (intptr_t j = 0; patch_resources[j] != NULL; j++) { |
| 110 Dart_Handle patch_src = GetStringResource(patch_resources[j]); |
| 111 DART_CHECK_VALID(patch_src); |
| 112 // Prepend the patch library URI to form a unique script URI for the patch. |
| 113 intptr_t len = snprintf(NULL, 0, "%s%s", patch_uri, patch_resources[j]); |
| 114 char* patch_filename = reinterpret_cast<char*>(malloc(len + 1)); |
| 115 snprintf(patch_filename, len + 1, "%s%s", patch_uri, patch_resources[j]); |
| 116 Dart_Handle patch_file_uri = Dart_NewStringFromCString(patch_filename); |
| 117 DART_CHECK_VALID(patch_file_uri); |
| 118 free(patch_filename); |
| 119 DART_CHECK_VALID(Dart_LibraryLoadPatch(library, patch_file_uri, patch_src)); |
| 120 } |
| 121 } |
| 122 |
57 Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { | 123 Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { |
58 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == | 124 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == |
59 kInvalidLibrary, "Unexpected number of builtin libraries"); | 125 kInvalidLibrary, "Unexpected number of builtin libraries"); |
60 DCHECK_GE(id, kBuiltinLibrary); | 126 DCHECK_GE(id, kBuiltinLibrary); |
61 DCHECK_LT(id, kInvalidLibrary); | 127 DCHECK_LT(id, kInvalidLibrary); |
62 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); | 128 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); |
63 Dart_Handle library = Dart_LookupLibrary(url); | 129 Dart_Handle library = Dart_LookupLibrary(url); |
64 DART_CHECK_VALID(library); | 130 DART_CHECK_VALID(library); |
| 131 SetNativeResolver(id); |
| 132 if (builtin_libraries_[id].patch_url_ != nullptr) { |
| 133 DCHECK(builtin_libraries_[id].patch_resources_ != nullptr); |
| 134 LoadPatchFiles(library, |
| 135 builtin_libraries_[id].patch_url_, |
| 136 builtin_libraries_[id].patch_resources_); |
| 137 } |
65 return library; | 138 return library; |
66 } | 139 } |
67 | 140 |
68 } // namespace dart | 141 } // namespace dart |
69 } // namespace mojo | 142 } // namespace mojo |
OLD | NEW |