OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/shared_impl/tracked_callback.h" | 5 #include "ppapi/shared_impl/tracked_callback.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "ppapi/c/pp_completion_callback.h" | 12 #include "ppapi/c/pp_completion_callback.h" |
13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/c/ppb_message_loop.h" | 14 #include "ppapi/c/ppb_message_loop.h" |
15 #include "ppapi/shared_impl/callback_tracker.h" | 15 #include "ppapi/shared_impl/callback_tracker.h" |
16 #include "ppapi/shared_impl/ppapi_globals.h" | 16 #include "ppapi/shared_impl/ppapi_globals.h" |
17 #include "ppapi/shared_impl/ppb_message_loop_shared.h" | 17 #include "ppapi/shared_impl/ppb_message_loop_shared.h" |
18 #include "ppapi/shared_impl/proxy_lock.h" | 18 #include "ppapi/shared_impl/proxy_lock.h" |
19 #include "ppapi/shared_impl/resource.h" | 19 #include "ppapi/shared_impl/resource.h" |
20 | 20 |
21 namespace ppapi { | 21 namespace ppapi { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 bool IsMainThread() { | 25 bool IsMainThread() { |
26 return | 26 return PpapiGlobals::Get() |
27 PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread(); | 27 ->GetMainThreadMessageLoop() |
| 28 ->BelongsToCurrentThread(); |
28 } | 29 } |
29 | 30 |
30 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task, | 31 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task, |
31 int32_t result) { | 32 int32_t result) { |
32 int32_t task_result = completion_task.Run(result); | 33 int32_t task_result = completion_task.Run(result); |
33 if (result != PP_ERROR_ABORTED) | 34 if (result != PP_ERROR_ABORTED) |
34 result = task_result; | 35 result = task_result; |
35 return result; | 36 return result; |
36 } | 37 } |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 // TrackedCallback ------------------------------------------------------------- | 41 // TrackedCallback ------------------------------------------------------------- |
41 | 42 |
42 // Note: don't keep a Resource* since it may go out of scope before us. | 43 // Note: don't keep a Resource* since it may go out of scope before us. |
43 TrackedCallback::TrackedCallback( | 44 TrackedCallback::TrackedCallback(Resource* resource, |
44 Resource* resource, | 45 const PP_CompletionCallback& callback) |
45 const PP_CompletionCallback& callback) | |
46 : is_scheduled_(false), | 46 : is_scheduled_(false), |
47 resource_id_(resource ? resource->pp_resource() : 0), | 47 resource_id_(resource ? resource->pp_resource() : 0), |
48 completed_(false), | 48 completed_(false), |
49 aborted_(false), | 49 aborted_(false), |
50 callback_(callback), | 50 callback_(callback), |
51 target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()), | 51 target_loop_(PpapiGlobals::Get()->GetCurrentMessageLoop()), |
52 result_for_blocked_callback_(PP_OK) { | 52 result_for_blocked_callback_(PP_OK) { |
53 // Note that target_loop_ may be NULL at this point, if the plugin has not | 53 // Note that target_loop_ may be NULL at this point, if the plugin has not |
54 // attached a loop to this thread, or if this is an in-process plugin. | 54 // attached a loop to this thread, or if this is an in-process plugin. |
55 // The Enter class should handle checking this for us. | 55 // The Enter class should handle checking this for us. |
(...skipping 17 matching lines...) Expand all Loading... |
73 new base::ConditionVariable(proxy_lock)); | 73 new base::ConditionVariable(proxy_lock)); |
74 } else { | 74 } else { |
75 // It's a non-blocking callback, so we should have a MessageLoopResource | 75 // It's a non-blocking callback, so we should have a MessageLoopResource |
76 // to dispatch to. Note that we don't error check here, though. Later, | 76 // to dispatch to. Note that we don't error check here, though. Later, |
77 // EnterResource::SetResult will check to make sure the callback is valid | 77 // EnterResource::SetResult will check to make sure the callback is valid |
78 // and take appropriate action. | 78 // and take appropriate action. |
79 } | 79 } |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 TrackedCallback::~TrackedCallback() { | 83 TrackedCallback::~TrackedCallback() {} |
84 } | |
85 | 84 |
86 void TrackedCallback::Abort() { | 85 void TrackedCallback::Abort() { Run(PP_ERROR_ABORTED); } |
87 Run(PP_ERROR_ABORTED); | |
88 } | |
89 | 86 |
90 void TrackedCallback::PostAbort() { | 87 void TrackedCallback::PostAbort() { PostRun(PP_ERROR_ABORTED); } |
91 PostRun(PP_ERROR_ABORTED); | |
92 } | |
93 | 88 |
94 void TrackedCallback::Run(int32_t result) { | 89 void TrackedCallback::Run(int32_t result) { |
95 // Only allow the callback to be run once. Note that this also covers the case | 90 // Only allow the callback to be run once. Note that this also covers the case |
96 // where the callback was previously Aborted because its associated Resource | 91 // where the callback was previously Aborted because its associated Resource |
97 // went away. The callback may live on for a while because of a reference from | 92 // went away. The callback may live on for a while because of a reference from |
98 // a Closure. But when the Closure runs, Run() quietly does nothing, and the | 93 // a Closure. But when the Closure runs, Run() quietly does nothing, and the |
99 // callback will go away when all referring Closures go away. | 94 // callback will go away when all referring Closures go away. |
100 if (completed()) | 95 if (completed()) |
101 return; | 96 return; |
102 if (result == PP_ERROR_ABORTED) | 97 if (result == PP_ERROR_ABORTED) |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 // until we're done. | 233 // until we're done. |
239 scoped_refptr<TrackedCallback> thiz = this; | 234 scoped_refptr<TrackedCallback> thiz = this; |
240 completed_ = true; | 235 completed_ = true; |
241 // We may not have a valid resource, in which case we're not in the tracker. | 236 // We may not have a valid resource, in which case we're not in the tracker. |
242 if (resource_id_) | 237 if (resource_id_) |
243 tracker_->Remove(thiz); | 238 tracker_->Remove(thiz); |
244 tracker_ = NULL; | 239 tracker_ = NULL; |
245 } | 240 } |
246 | 241 |
247 } // namespace ppapi | 242 } // namespace ppapi |
OLD | NEW |