OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef PPAPI_CPP_COMPLETION_CALLBACK_H_ | 5 #ifndef PPAPI_CPP_COMPLETION_CALLBACK_H_ |
6 #define PPAPI_CPP_COMPLETION_CALLBACK_H_ | 6 #define PPAPI_CPP_COMPLETION_CALLBACK_H_ |
7 | 7 |
8 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
9 #include "ppapi/cpp/logging.h" | 9 #include "ppapi/cpp/logging.h" |
10 #include "ppapi/cpp/non_thread_safe_ref_count.h" | 10 #include "ppapi/cpp/non_thread_safe_ref_count.h" |
11 | 11 |
12 namespace pp { | 12 namespace pp { |
13 | 13 |
14 // A CompletionCallback provides a wrapper around PP_CompletionCallback. | 14 // A CompletionCallback provides a wrapper around PP_CompletionCallback. |
15 class CompletionCallback { | 15 class CompletionCallback { |
16 public: | 16 public: |
17 // Use this special constructor to create a 'blocking' CompletionCallback | 17 // Use this special constructor to create a 'blocking' CompletionCallback |
18 // that may be passed to a method to indicate that the calling thread should | 18 // that may be passed to a method to indicate that the calling thread should |
19 // be blocked until the asynchronous operation corresponding to the method | 19 // be blocked until the asynchronous operation corresponding to the method |
20 // completes. | 20 // completes. |
21 struct Block {}; | 21 struct Block {}; |
22 CompletionCallback(Block) { | 22 CompletionCallback(Block) { |
23 cc_ = PP_BlockUntilComplete(); | 23 cc_ = PP_BlockUntilComplete(); |
24 } | 24 } |
25 | 25 |
26 CompletionCallback(PP_CompletionCallback_Func func, void* user_data) { | 26 CompletionCallback(PP_CompletionCallback_Func func, void* user_data) { |
27 cc_ = PP_MakeCompletionCallback(func, user_data); | 27 cc_ = PP_MakeCompletionCallback(func, user_data); |
28 } | 28 } |
29 | 29 |
| 30 void set_flags(int32_t flags) { cc_.flags = flags; } |
| 31 |
30 // Call this method to explicitly run the CompletionCallback. Normally, the | 32 // Call this method to explicitly run the CompletionCallback. Normally, the |
31 // system runs a CompletionCallback after an asynchronous operation | 33 // system runs a CompletionCallback after an asynchronous operation |
32 // completes, but programs may wish to run the CompletionCallback manually | 34 // completes, but programs may wish to run the CompletionCallback manually |
33 // in order to reuse the same code paths. | 35 // in order to reuse the same code paths. |
34 void Run(int32_t result) { | 36 void Run(int32_t result) { |
35 PP_DCHECK(cc_.func); | 37 PP_DCHECK(cc_.func); |
36 PP_RunCompletionCallback(&cc_, result); | 38 PP_RunCompletionCallback(&cc_, result); |
37 } | 39 } |
38 | 40 |
39 const PP_CompletionCallback& pp_completion_callback() const { return cc_; } | 41 const PP_CompletionCallback& pp_completion_callback() const { return cc_; } |
(...skipping 22 matching lines...) Expand all Loading... |
62 // EXAMPLE USAGE: | 64 // EXAMPLE USAGE: |
63 // | 65 // |
64 // class MyHandler { | 66 // class MyHandler { |
65 // public: | 67 // public: |
66 // MyHandler() : factory_(this), offset_(0) { | 68 // MyHandler() : factory_(this), offset_(0) { |
67 // } | 69 // } |
68 // | 70 // |
69 // void ProcessFile(const FileRef& file) { | 71 // void ProcessFile(const FileRef& file) { |
70 // CompletionCallback cc = factory_.NewCallback(&MyHandler::DidOpen); | 72 // CompletionCallback cc = factory_.NewCallback(&MyHandler::DidOpen); |
71 // int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc); | 73 // int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc); |
72 // if (rv != PP_OK_COMPLETIONPENDING) | 74 // CHECK(rv == PP_OK_COMPLETIONPENDING); |
73 // cc.Run(rv); | |
74 // } | 75 // } |
75 // | 76 // |
76 // private: | 77 // private: |
77 // CompletionCallback NewCallback() { | 78 // CompletionCallback NewCallback() { |
78 // return factory_.NewCallback(&MyHandler::DidCompleteIO); | 79 // return factory_.NewCallback(&MyHandler::DidCompleteIO); |
79 // } | 80 // } |
80 // | 81 // |
81 // void DidOpen(int32_t result) { | 82 // void DidOpen(int32_t result) { |
82 // if (result == PP_OK) { | 83 // if (result == PP_OK) { |
83 // // The file is open, and we can begin reading. | 84 // // The file is open, and we can begin reading. |
(...skipping 10 matching lines...) Expand all Loading... |
94 // ProcessBytes(buf_, result); | 95 // ProcessBytes(buf_, result); |
95 // offset_ += result; | 96 // offset_ += result; |
96 // ReadMore(); | 97 // ReadMore(); |
97 // } else { | 98 // } else { |
98 // // Done reading (possibly with an error given by 'result'). | 99 // // Done reading (possibly with an error given by 'result'). |
99 // } | 100 // } |
100 // } | 101 // } |
101 // | 102 // |
102 // void ReadMore() { | 103 // void ReadMore() { |
103 // CompletionCallback cc = factory_.NewCallback(&MyHandler::DidRead); | 104 // CompletionCallback cc = factory_.NewCallback(&MyHandler::DidRead); |
| 105 // cc.set_flags(PP_COMPLETIONCALLBACK_FLAG_NOFORCEASYNC); |
104 // int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_), | 106 // int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_), |
105 // cc.pp_completion_callback()); | 107 // cc.pp_completion_callback()); |
106 // if (rv != PP_OK_COMPLETIONPENDING) | 108 // if (rv != PP_OK_COMPLETIONPENDING) |
107 // cc.Run(rv); | 109 // cc.Run(rv); |
108 // } | 110 // } |
109 // | 111 // |
110 // void ProcessBytes(const char* bytes, int32_t length) { | 112 // void ProcessBytes(const char* bytes, int32_t length) { |
111 // // Do work ... | 113 // // Do work ... |
112 // } | 114 // } |
113 // | 115 // |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 CompletionCallbackFactory(const CompletionCallbackFactory&); | 307 CompletionCallbackFactory(const CompletionCallbackFactory&); |
306 CompletionCallbackFactory& operator=(const CompletionCallbackFactory&); | 308 CompletionCallbackFactory& operator=(const CompletionCallbackFactory&); |
307 | 309 |
308 T* object_; | 310 T* object_; |
309 BackPointer* back_pointer_; | 311 BackPointer* back_pointer_; |
310 }; | 312 }; |
311 | 313 |
312 } // namespace pp | 314 } // namespace pp |
313 | 315 |
314 #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ | 316 #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ |
OLD | NEW |