| 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 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "crypto/random.h" | 15 #include "crypto/random.h" |
| 16 #include "dart/runtime/include/dart_api.h" | 16 #include "dart/runtime/include/dart_api.h" |
| 17 #include "mojo/dart/embedder/builtin.h" | 17 #include "mojo/dart/embedder/builtin.h" |
| 18 #include "mojo/dart/embedder/mojo_natives.h" | 18 #include "mojo/dart/embedder/mojo_natives.h" |
| 19 | 19 |
| 20 #if defined(OS_ANDROID) |
| 21 #include <android/log.h> |
| 22 #endif |
| 23 |
| 20 namespace mojo { | 24 namespace mojo { |
| 21 namespace dart { | 25 namespace dart { |
| 22 | 26 |
| 23 // Lists the native functions implementing basic functionality in | 27 // Lists the native functions implementing basic functionality in |
| 24 // the Mojo embedder dart, such as printing, and file I/O. | 28 // the Mojo embedder dart, such as printing, and file I/O. |
| 25 #define BUILTIN_NATIVE_LIST(V) \ | 29 #define BUILTIN_NATIVE_LIST(V) \ |
| 26 V(Crypto_GetRandomBytes, 1) \ | 30 V(Crypto_GetRandomBytes, 1) \ |
| 27 V(Logger_PrintString, 1) \ | 31 V(Logger_PrintString, 1) \ |
| 28 V(Builtin_ReadSync, 1) \ | 32 V(Builtin_ReadSync, 1) \ |
| 29 V(Builtin_EnumerateFiles, 1) \ | 33 V(Builtin_EnumerateFiles, 1) \ |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Implementation of native functions which are used for some | 77 // Implementation of native functions which are used for some |
| 74 // test/debug functionality in standalone dart mode. | 78 // test/debug functionality in standalone dart mode. |
| 75 void Logger_PrintString(Dart_NativeArguments args) { | 79 void Logger_PrintString(Dart_NativeArguments args) { |
| 76 intptr_t length = 0; | 80 intptr_t length = 0; |
| 77 uint8_t* chars = nullptr; | 81 uint8_t* chars = nullptr; |
| 78 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 82 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 79 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); | 83 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
| 80 if (Dart_IsError(result)) { | 84 if (Dart_IsError(result)) { |
| 81 Dart_PropagateError(result); | 85 Dart_PropagateError(result); |
| 82 } else { | 86 } else { |
| 83 // TODO(dart): Hook up to developer console (if/when that's a thing). | 87 // TODO(dart): Hook up to developer console (if/when that's a thing). |
| 84 #if defined(OS_ANDROID) | |
| 85 std::string message(reinterpret_cast<char*>(chars), length); | |
| 86 LOG(INFO) << "CONSOLE: " << message; | |
| 87 #else | |
| 88 // Uses fwrite to support printing NUL bytes. | 88 // Uses fwrite to support printing NUL bytes. |
| 89 fwrite(chars, 1, length, stdout); | 89 fwrite(chars, 1, length, stdout); |
| 90 fputs("\n", stdout); | 90 fputs("\n", stdout); |
| 91 #if defined(OS_ANDROID) |
| 92 // In addition to writing to the stdout, write to the logcat so that the |
| 93 // message is discoverable when running on an unrooted device. Use the |
| 94 // "chromium" tag to match native printouts produced by base LOG macros, so |
| 95 // that the same rule will pick them up in scripts that stream relevant |
| 96 // logcat output to host terminal. |
| 97 __android_log_print(ANDROID_LOG_INFO, "chromium", "%.*s", length, chars); |
| 91 #endif | 98 #endif |
| 92 } | 99 } |
| 93 fflush(stdout); | 100 fflush(stdout); |
| 94 } | 101 } |
| 95 | 102 |
| 96 void Builtin_ReadSync(Dart_NativeArguments args) { | 103 void Builtin_ReadSync(Dart_NativeArguments args) { |
| 97 intptr_t chars_length = 0; | 104 intptr_t chars_length = 0; |
| 98 uint8_t* chars = nullptr; | 105 uint8_t* chars = nullptr; |
| 99 Dart_Handle file_uri = Dart_GetNativeArgument(args, 0); | 106 Dart_Handle file_uri = Dart_GetNativeArgument(args, 0); |
| 100 if (!Dart_IsString(file_uri)) { | 107 if (!Dart_IsString(file_uri)) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 Dart_Handle error = Dart_NewStringFromCString( | 331 Dart_Handle error = Dart_NewStringFromCString( |
| 325 "Failed to allocate storage."); | 332 "Failed to allocate storage."); |
| 326 Dart_ThrowException(error); | 333 Dart_ThrowException(error); |
| 327 } | 334 } |
| 328 Dart_ListSetAsBytes(result, 0, buffer.get(), count); | 335 Dart_ListSetAsBytes(result, 0, buffer.get(), count); |
| 329 Dart_SetReturnValue(args, result); | 336 Dart_SetReturnValue(args, result); |
| 330 } | 337 } |
| 331 | 338 |
| 332 } // namespace bin | 339 } // namespace bin |
| 333 } // namespace dart | 340 } // namespace dart |
| OLD | NEW |