- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / system / dispatcher.cc
1 // Copyright 2013 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 "mojo/system/dispatcher.h"
6
7 #include "base/logging.h"
8
9 namespace mojo {
10 namespace system {
11
12 MojoResult Dispatcher::Close() {
13   base::AutoLock locker(lock_);
14   if (is_closed_)
15     return MOJO_RESULT_INVALID_ARGUMENT;
16
17   is_closed_ = true;
18   CancelAllWaitersNoLock();
19   return CloseImplNoLock();
20 }
21
22 MojoResult Dispatcher::WriteMessage(const void* bytes,
23                                     uint32_t num_bytes,
24                                     const MojoHandle* handles,
25                                     uint32_t num_handles,
26                                     MojoWriteMessageFlags flags) {
27   base::AutoLock locker(lock_);
28   if (is_closed_)
29     return MOJO_RESULT_INVALID_ARGUMENT;
30
31   return WriteMessageImplNoLock(bytes, num_bytes, handles, num_handles, flags);
32 }
33
34 MojoResult Dispatcher::ReadMessage(void* bytes,
35                                    uint32_t* num_bytes,
36                                    MojoHandle* handles,
37                                    uint32_t* num_handles,
38                                    MojoReadMessageFlags flags) {
39   base::AutoLock locker(lock_);
40   if (is_closed_)
41     return MOJO_RESULT_INVALID_ARGUMENT;
42
43   return ReadMessageImplNoLock(bytes, num_bytes, handles, num_handles, flags);
44 }
45
46 MojoResult Dispatcher::AddWaiter(Waiter* waiter,
47                                  MojoWaitFlags flags,
48                                  MojoResult wake_result) {
49   DCHECK_GE(wake_result, 0);
50
51   base::AutoLock locker(lock_);
52   if (is_closed_)
53     return MOJO_RESULT_INVALID_ARGUMENT;
54
55   return AddWaiterImplNoLock(waiter, flags, wake_result);
56 }
57
58 void Dispatcher::RemoveWaiter(Waiter* waiter) {
59   base::AutoLock locker(lock_);
60   if (is_closed_)
61     return;
62   RemoveWaiterImplNoLock(waiter);
63 }
64
65 Dispatcher::Dispatcher()
66     : is_closed_(false) {
67 }
68
69 Dispatcher::~Dispatcher() {
70   // Make sure that |Close()| was called.
71   DCHECK(is_closed_);
72 }
73
74 void Dispatcher::CancelAllWaitersNoLock() {
75   lock_.AssertAcquired();
76   DCHECK(is_closed_);
77   // By default, waiting isn't supported. Only dispatchers that can be waited on
78   // will do something nontrivial.
79 }
80
81 MojoResult Dispatcher::CloseImplNoLock() {
82   lock_.AssertAcquired();
83   DCHECK(is_closed_);
84   // This may not need to do anything. Dispatchers should override this to do
85   // any actual close-time cleanup necessary.
86   return MOJO_RESULT_OK;
87 }
88
89 MojoResult Dispatcher::WriteMessageImplNoLock(const void* bytes,
90                                               uint32_t num_bytes,
91                                               const MojoHandle* handles,
92                                               uint32_t num_handles,
93                                               MojoWriteMessageFlags flags) {
94   lock_.AssertAcquired();
95   DCHECK(!is_closed_);
96   // By default, this isn't supported. Only dispatchers for message pipes (with
97   // whatever implementation, possibly a proxy) will do something nontrivial.
98   return MOJO_RESULT_INVALID_ARGUMENT;
99 }
100
101 MojoResult Dispatcher::ReadMessageImplNoLock(void* bytes,
102                                              uint32_t* num_bytes,
103                                              MojoHandle* handles,
104                                              uint32_t* num_handles,
105                                              MojoReadMessageFlags flags) {
106   lock_.AssertAcquired();
107   DCHECK(!is_closed_);
108   // By default, this isn't supported. Only dispatchers for message pipes (with
109   // whatever implementation, possibly a proxy) will do something nontrivial.
110   return MOJO_RESULT_INVALID_ARGUMENT;
111 }
112
113 MojoResult Dispatcher::AddWaiterImplNoLock(Waiter* waiter,
114                                            MojoWaitFlags flags,
115                                            MojoResult wake_result) {
116   lock_.AssertAcquired();
117   DCHECK(!is_closed_);
118   // By default, waiting isn't supported. Only dispatchers that can be waited on
119   // will do something nontrivial.
120   return MOJO_RESULT_FAILED_PRECONDITION;
121 }
122
123 void Dispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
124   lock_.AssertAcquired();
125   DCHECK(!is_closed_);
126   // By default, waiting isn't supported. Only dispatchers that can be waited on
127   // will do something nontrivial.
128 }
129
130 }  // namespace system
131 }  // namespace mojo