15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/c/pp_errors.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ppapi/c/private/ppb_testing_private.h"
79ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "ppapi/native_client/tests/ppapi_test_lib/get_browser_interface.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "ppapi/native_client/tests/ppapi_test_lib/testable_callback.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestableCallback::TestableCallback(PP_Instance instance, bool force_async)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : have_result_(false),
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result_(PP_OK_COMPLETIONPENDING),
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      force_async_(force_async),
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      post_quit_task_(false),
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      run_count_(0),
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      instance_(instance) {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int32_t TestableCallback::WaitForResult() {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!have_result_) {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result_ = PP_OK_COMPLETIONPENDING;  // Reset
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    post_quit_task_ = true;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This waits until PPBTestingDev()->QuitMessageLoop() is called
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // by the "Handler" which represents the actual callback code.
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    PPBTestingPrivate()->RunMessageLoop(instance_);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  have_result_ = false;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result_;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PP_CompletionCallback TestableCallback::GetCallback() {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int32_t flags = force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_CompletionCallback cc =
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_MakeCompletionCallback(&TestableCallback::Handler, this);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cc.flags = flags;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return cc;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static, so we can take it's address
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is the actual callback, all it does is record
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the result and wake up whoever is block on
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "WaitForResult"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestableCallback::Handler(void* user_data, int32_t result) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestableCallback* callback = static_cast<TestableCallback*>(user_data);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  callback->result_ = result;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  callback->have_result_ = true;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ++callback->run_count_;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (callback->post_quit_task_) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    callback->post_quit_task_ = false;
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    PPBTestingPrivate()->QuitMessageLoop(callback->instance_);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
54