| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_DELAYED_CALLBACK_H_ | |
| 8 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_DELAYED_CALLBACK_H_ | |
| 9 | |
| 10 #include "native_client/src/include/portability.h" | |
| 11 | |
| 12 #include "native_client/src/include/nacl_macros.h" | |
| 13 #include "native_client/src/shared/platform/nacl_check.h" | |
| 14 #include "ppapi/c/pp_errors.h" | |
| 15 #include "ppapi/cpp/completion_callback.h" | |
| 16 | |
| 17 namespace plugin { | |
| 18 | |
| 19 // Delay a "Run(PP_OK)" of a pp::CompletionCallback after exactly N time ticks | |
| 20 // have passed. User should not attempt to count beyond N ticks. | |
| 21 // It is expected that this is only counted up on "good" code paths | |
| 22 // (when there are no errors), so that passing along a status code of | |
| 23 // PP_OK makes sense. | |
| 24 class DelayedCallback { | |
| 25 public: | |
| 26 DelayedCallback(pp::CompletionCallback continuation, | |
| 27 uint32_t initial_requirement) | |
| 28 : required_ticks_(initial_requirement), | |
| 29 continuation_(continuation), | |
| 30 started_(false) { | |
| 31 } | |
| 32 | |
| 33 ~DelayedCallback() { } | |
| 34 | |
| 35 // Advance time, and run the callback if it is finally time. | |
| 36 // This must be run on the main thread, since pp callbacks | |
| 37 // must be run on the main thread anyway. We also want to | |
| 38 // avoid race condtions. If we want to relax the requirement, | |
| 39 // we could add locks and use CallOnMainThread. | |
| 40 void RunIfTime() { | |
| 41 CHECK(required_ticks_ > 0); | |
| 42 started_ = true; | |
| 43 --required_ticks_; | |
| 44 if (0 == required_ticks_) { | |
| 45 continuation_.Run(PP_OK); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 // Add another requirement before anything has run. | |
| 50 void IncrRequirements(uint32_t additional_requirements) { | |
| 51 CHECK(started_ == false); | |
| 52 required_ticks_ += additional_requirements; | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 NACL_DISALLOW_COPY_AND_ASSIGN(DelayedCallback); | |
| 57 | |
| 58 // How many time ticks are left before we run the callback. | |
| 59 uint32_t required_ticks_; | |
| 60 | |
| 61 // The continuation to invoke when ticks passed. | |
| 62 pp::CompletionCallback continuation_; | |
| 63 | |
| 64 // Some sanity checking to catch when people misuse the library. | |
| 65 bool started_; | |
| 66 }; | |
| 67 | |
| 68 } // namespace plugin | |
| 69 | |
| 70 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_DELAYED_CALLBACK_H_ | |
| OLD | NEW |