1 // Copyright (c) 2011 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 "ppapi/c/pp_errors.h" 6 #include "ppapi/c/private/ppb_testing_private.h" 7 #include "ppapi/native_client/tests/ppapi_test_lib/get_browser_interface.h" 8 #include "ppapi/native_client/tests/ppapi_test_lib/testable_callback.h" 9 10 TestableCallback::TestableCallback(PP_Instance instance, bool force_async) 11 : have_result_(false), 12 result_(PP_OK_COMPLETIONPENDING), 13 force_async_(force_async), 14 post_quit_task_(false), 15 run_count_(0), 16 instance_(instance) { 17 } 18 19 int32_t TestableCallback::WaitForResult() { 20 if (!have_result_) { 21 result_ = PP_OK_COMPLETIONPENDING; // Reset 22 post_quit_task_ = true; 23 24 // This waits until PPBTestingDev()->QuitMessageLoop() is called 25 // by the "Handler" which represents the actual callback code. 26 PPBTestingPrivate()->RunMessageLoop(instance_); 27 } 28 have_result_ = false; 29 return result_; 30 } 31 32 PP_CompletionCallback TestableCallback::GetCallback() { 33 int32_t flags = force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; 34 PP_CompletionCallback cc = 35 PP_MakeCompletionCallback(&TestableCallback::Handler, this); 36 cc.flags = flags; 37 return cc; 38 } 39 40 // static, so we can take it's address 41 // This is the actual callback, all it does is record 42 // the result and wake up whoever is block on 43 // "WaitForResult" 44 void TestableCallback::Handler(void* user_data, int32_t result) { 45 TestableCallback* callback = static_cast<TestableCallback*>(user_data); 46 callback->result_ = result; 47 callback->have_result_ = true; 48 ++callback->run_count_; 49 if (callback->post_quit_task_) { 50 callback->post_quit_task_ = false; 51 PPBTestingPrivate()->QuitMessageLoop(callback->instance_); 52 } 53 } 54