| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // join while handles are still open, until the C++ side closes them. | 263 // join while handles are still open, until the C++ side closes them. |
| 264 base::Thread dart_thread("dart"); | 264 base::Thread dart_thread("dart"); |
| 265 cpp_side->set_run_loop(&run_loop_); | 265 cpp_side->set_run_loop(&run_loop_); |
| 266 return RunWithDartOnThread(&dart_thread, test, cpp_side); | 266 return RunWithDartOnThread(&dart_thread, test, cpp_side); |
| 267 } | 267 } |
| 268 | 268 |
| 269 private: | 269 private: |
| 270 base::MessageLoop loop; | 270 base::MessageLoop loop; |
| 271 base::RunLoop run_loop_; | 271 base::RunLoop run_loop_; |
| 272 | 272 |
| 273 static void UnhandledExceptionCallback(bool* exception, Dart_Handle error) { | 273 static void UnhandledExceptionCallback(bool* exception, |
| 274 int64_t* closed_handles, |
| 275 Dart_Handle error, |
| 276 int64_t count) { |
| 274 *exception = true; | 277 *exception = true; |
| 278 *closed_handles = count; |
| 275 } | 279 } |
| 276 | 280 |
| 277 static bool GenerateEntropy(uint8_t* buffer, intptr_t length) { | 281 static bool GenerateEntropy(uint8_t* buffer, intptr_t length) { |
| 278 base::RandBytes(static_cast<void*>(buffer), length); | 282 base::RandBytes(static_cast<void*>(buffer), length); |
| 279 return true; | 283 return true; |
| 280 } | 284 } |
| 281 | 285 |
| 282 static void InitializeDartConfig(DartControllerConfig* config, | 286 static void InitializeDartConfig(DartControllerConfig* config, |
| 283 const std::string& test, | 287 const std::string& test, |
| 284 MojoHandle handle, | 288 MojoHandle handle, |
| 285 const char** arguments, | 289 const char** arguments, |
| 286 int arguments_count, | 290 int arguments_count, |
| 287 bool* unhandled_exception, | 291 bool* unhandled_exception, |
| 292 int64_t* closed_handles, |
| 288 char** error) { | 293 char** error) { |
| 289 base::FilePath path; | 294 base::FilePath path; |
| 290 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 295 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 291 path = path.AppendASCII("mojo") | 296 path = path.AppendASCII("mojo") |
| 292 .AppendASCII("dart") | 297 .AppendASCII("dart") |
| 293 .AppendASCII("embedder") | 298 .AppendASCII("embedder") |
| 294 .AppendASCII("test") | 299 .AppendASCII("test") |
| 295 .AppendASCII(test); | 300 .AppendASCII(test); |
| 296 | 301 |
| 297 // Setup the package root. | 302 // Setup the package root. |
| 298 base::FilePath package_root; | 303 base::FilePath package_root; |
| 299 PathService::Get(base::DIR_EXE, &package_root); | 304 PathService::Get(base::DIR_EXE, &package_root); |
| 300 package_root = package_root.AppendASCII("gen") | 305 package_root = package_root.AppendASCII("gen") |
| 301 .AppendASCII("dart-pkg") | 306 .AppendASCII("dart-pkg") |
| 302 .AppendASCII("packages"); | 307 .AppendASCII("packages"); |
| 303 | 308 |
| 304 | 309 |
| 305 config->strict_compilation = true; | 310 config->strict_compilation = true; |
| 306 config->script_uri = path.AsUTF8Unsafe(); | 311 config->script_uri = path.AsUTF8Unsafe(); |
| 307 config->package_root = package_root.AsUTF8Unsafe(); | 312 config->package_root = package_root.AsUTF8Unsafe(); |
| 308 config->callbacks.exception = | 313 config->callbacks.exception = base::Bind( |
| 309 base::Bind(&UnhandledExceptionCallback, unhandled_exception); | 314 &UnhandledExceptionCallback, unhandled_exception, closed_handles); |
| 310 config->entropy = GenerateEntropy; | 315 config->entropy = GenerateEntropy; |
| 311 config->handle = handle; | 316 config->handle = handle; |
| 312 config->SetVmFlags(arguments, arguments_count); | 317 config->SetVmFlags(arguments, arguments_count); |
| 313 config->error = error; | 318 config->error = error; |
| 314 } | 319 } |
| 315 | 320 |
| 316 static void RunDartSide(const DartControllerConfig& config) { | 321 static void RunDartSide(const DartControllerConfig& config) { |
| 317 DartController::RunSingleDartScript(config); | 322 DartController::RunSingleDartScript(config); |
| 318 } | 323 } |
| 319 | 324 |
| 320 bool RunWithDartOnThread(base::Thread* dart_thread, | 325 bool RunWithDartOnThread(base::Thread* dart_thread, |
| 321 const std::string& test, | 326 const std::string& test, |
| 322 CppSideConnection* cpp_side) { | 327 CppSideConnection* cpp_side) { |
| 323 dart_to_cpp::DartSidePtr dart_side_ptr; | 328 dart_to_cpp::DartSidePtr dart_side_ptr; |
| 324 auto dart_side_request = GetProxy(&dart_side_ptr); | 329 auto dart_side_request = GetProxy(&dart_side_ptr); |
| 325 | 330 |
| 326 dart_to_cpp::CppSidePtr cpp_side_ptr; | 331 dart_to_cpp::CppSidePtr cpp_side_ptr; |
| 327 cpp_side->Bind(GetProxy(&cpp_side_ptr)); | 332 cpp_side->Bind(GetProxy(&cpp_side_ptr)); |
| 328 dart_side_ptr->SetClient(cpp_side_ptr.Pass()); | 333 dart_side_ptr->SetClient(cpp_side_ptr.Pass()); |
| 329 | 334 |
| 330 dart_side_ptr.internal_state()->router_for_testing()->EnableTestingMode(); | 335 dart_side_ptr.internal_state()->router_for_testing()->EnableTestingMode(); |
| 331 | 336 |
| 332 cpp_side->set_dart_side(dart_side_ptr.get()); | 337 cpp_side->set_dart_side(dart_side_ptr.get()); |
| 333 | 338 |
| 334 DartControllerConfig config; | 339 DartControllerConfig config; |
| 335 char* error; | 340 char* error; |
| 336 bool unhandled_exception = false; | 341 bool unhandled_exception = false; |
| 342 int64_t closed_handles; |
| 337 InitializeDartConfig( | 343 InitializeDartConfig( |
| 338 &config, | 344 &config, test, dart_side_request.PassMessagePipe().release().value(), |
| 339 test, | 345 nullptr, 0, &unhandled_exception, &closed_handles, &error); |
| 340 dart_side_request.PassMessagePipe().release().value(), | |
| 341 nullptr, | |
| 342 0, | |
| 343 &unhandled_exception, | |
| 344 &error); | |
| 345 | 346 |
| 346 dart_thread->Start(); | 347 dart_thread->Start(); |
| 347 dart_thread->message_loop()->PostTask(FROM_HERE, | 348 dart_thread->message_loop()->PostTask(FROM_HERE, |
| 348 base::Bind(&RunDartSide, base::ConstRef(config))); | 349 base::Bind(&RunDartSide, base::ConstRef(config))); |
| 349 | 350 |
| 350 run_loop_.Run(); | 351 run_loop_.Run(); |
| 351 return unhandled_exception; | 352 return unhandled_exception; |
| 352 } | 353 } |
| 353 | 354 |
| 354 DISALLOW_COPY_AND_ASSIGN(DartToCppTest); | 355 DISALLOW_COPY_AND_ASSIGN(DartToCppTest); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 365 TEST_F(DartToCppTest, Echo) { | 366 TEST_F(DartToCppTest, Echo) { |
| 366 EchoCppSideConnection cpp_side_connection; | 367 EchoCppSideConnection cpp_side_connection; |
| 367 bool unhandled_exception = | 368 bool unhandled_exception = |
| 368 RunTest("dart_to_cpp_tests.dart", &cpp_side_connection); | 369 RunTest("dart_to_cpp_tests.dart", &cpp_side_connection); |
| 369 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 370 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 370 EXPECT_FALSE(unhandled_exception); | 371 EXPECT_FALSE(unhandled_exception); |
| 371 } | 372 } |
| 372 | 373 |
| 373 } // namespace dart | 374 } // namespace dart |
| 374 } // namespace mojo | 375 } // namespace mojo |
| OLD | NEW |