| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/public/cpp/bindings/message.h" | 5 #include "mojo/public/cpp/bindings/message.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/threading/thread_local.h" |
| 16 | 19 |
| 17 namespace mojo { | 20 namespace mojo { |
| 18 | 21 |
| 22 namespace { |
| 23 |
| 24 base::LazyInstance<base::ThreadLocalPointer<internal::MessageDispatchContext>> |
| 25 g_tls_message_dispatch_context = LAZY_INSTANCE_INITIALIZER; |
| 26 |
| 27 base::LazyInstance<base::ThreadLocalPointer<SyncMessageResponseContext>> |
| 28 g_tls_sync_response_context = LAZY_INSTANCE_INITIALIZER; |
| 29 |
| 30 } // namespace |
| 31 |
| 19 Message::Message() { | 32 Message::Message() { |
| 20 } | 33 } |
| 21 | 34 |
| 22 Message::~Message() { | 35 Message::~Message() { |
| 23 CloseHandles(); | 36 CloseHandles(); |
| 24 } | 37 } |
| 25 | 38 |
| 26 void Message::Initialize(size_t capacity, bool zero_initialized) { | 39 void Message::Initialize(size_t capacity, bool zero_initialized) { |
| 27 DCHECK(!buffer_); | 40 DCHECK(!buffer_); |
| 28 buffer_.reset(new internal::MessageBuffer(capacity, zero_initialized)); | 41 buffer_.reset(new internal::MessageBuffer(capacity, zero_initialized)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 rv = GetMessageBuffer(new_message.get(), &new_buffer); | 86 rv = GetMessageBuffer(new_message.get(), &new_buffer); |
| 74 CHECK_EQ(rv, MOJO_RESULT_OK); | 87 CHECK_EQ(rv, MOJO_RESULT_OK); |
| 75 | 88 |
| 76 memcpy(new_buffer, data(), data_num_bytes()); | 89 memcpy(new_buffer, data(), data_num_bytes()); |
| 77 buffer_.reset(); | 90 buffer_.reset(); |
| 78 | 91 |
| 79 return new_message; | 92 return new_message; |
| 80 } | 93 } |
| 81 | 94 |
| 82 void Message::NotifyBadMessage(const std::string& error) { | 95 void Message::NotifyBadMessage(const std::string& error) { |
| 96 DCHECK(buffer_); |
| 83 buffer_->NotifyBadMessage(error); | 97 buffer_->NotifyBadMessage(error); |
| 84 } | 98 } |
| 85 | 99 |
| 86 void Message::CloseHandles() { | 100 void Message::CloseHandles() { |
| 87 for (std::vector<Handle>::iterator it = handles_.begin(); | 101 for (std::vector<Handle>::iterator it = handles_.begin(); |
| 88 it != handles_.end(); ++it) { | 102 it != handles_.end(); ++it) { |
| 89 if (it->is_valid()) | 103 if (it->is_valid()) |
| 90 CloseRaw(*it); | 104 CloseRaw(*it); |
| 91 } | 105 } |
| 92 } | 106 } |
| 93 | 107 |
| 108 SyncMessageResponseContext::SyncMessageResponseContext() |
| 109 : outer_context_(current()) { |
| 110 g_tls_sync_response_context.Get().Set(this); |
| 111 } |
| 112 |
| 113 SyncMessageResponseContext::~SyncMessageResponseContext() { |
| 114 DCHECK_EQ(current(), this); |
| 115 g_tls_sync_response_context.Get().Set(outer_context_); |
| 116 } |
| 117 |
| 118 // static |
| 119 SyncMessageResponseContext* SyncMessageResponseContext::current() { |
| 120 return g_tls_sync_response_context.Get().Get(); |
| 121 } |
| 122 |
| 123 void SyncMessageResponseContext::ReportBadMessage(const std::string& error) { |
| 124 GetBadMessageCallback().Run(error); |
| 125 } |
| 126 |
| 127 const ReportBadMessageCallback& |
| 128 SyncMessageResponseContext::GetBadMessageCallback() { |
| 129 if (bad_message_callback_.is_null()) { |
| 130 std::unique_ptr<Message> new_message(new Message); |
| 131 response_.MoveTo(new_message.get()); |
| 132 bad_message_callback_ = base::Bind(&Message::NotifyBadMessage, |
| 133 base::Owned(new_message.release())); |
| 134 } |
| 135 return bad_message_callback_; |
| 136 } |
| 137 |
| 94 MojoResult ReadMessage(MessagePipeHandle handle, Message* message) { | 138 MojoResult ReadMessage(MessagePipeHandle handle, Message* message) { |
| 95 MojoResult rv; | 139 MojoResult rv; |
| 96 | 140 |
| 97 std::vector<Handle> handles; | 141 std::vector<Handle> handles; |
| 98 ScopedMessageHandle mojo_message; | 142 ScopedMessageHandle mojo_message; |
| 99 uint32_t num_bytes = 0, num_handles = 0; | 143 uint32_t num_bytes = 0, num_handles = 0; |
| 100 rv = ReadMessageNew(handle, | 144 rv = ReadMessageNew(handle, |
| 101 &mojo_message, | 145 &mojo_message, |
| 102 &num_bytes, | 146 &num_bytes, |
| 103 nullptr, | 147 nullptr, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 115 } | 159 } |
| 116 | 160 |
| 117 if (rv != MOJO_RESULT_OK) | 161 if (rv != MOJO_RESULT_OK) |
| 118 return rv; | 162 return rv; |
| 119 | 163 |
| 120 message->InitializeFromMojoMessage( | 164 message->InitializeFromMojoMessage( |
| 121 std::move(mojo_message), num_bytes, &handles); | 165 std::move(mojo_message), num_bytes, &handles); |
| 122 return MOJO_RESULT_OK; | 166 return MOJO_RESULT_OK; |
| 123 } | 167 } |
| 124 | 168 |
| 169 void ReportBadMessage(const std::string& error) { |
| 170 internal::MessageDispatchContext* context = |
| 171 internal::MessageDispatchContext::current(); |
| 172 DCHECK(context); |
| 173 context->GetBadMessageCallback().Run(error); |
| 174 } |
| 175 |
| 176 ReportBadMessageCallback GetBadMessageCallback() { |
| 177 internal::MessageDispatchContext* context = |
| 178 internal::MessageDispatchContext::current(); |
| 179 DCHECK(context); |
| 180 return context->GetBadMessageCallback(); |
| 181 } |
| 182 |
| 183 namespace internal { |
| 184 |
| 185 MessageDispatchContext::MessageDispatchContext(Message* message) |
| 186 : outer_context_(current()), message_(message) { |
| 187 g_tls_message_dispatch_context.Get().Set(this); |
| 188 } |
| 189 |
| 190 MessageDispatchContext::~MessageDispatchContext() { |
| 191 DCHECK_EQ(current(), this); |
| 192 g_tls_message_dispatch_context.Get().Set(outer_context_); |
| 193 } |
| 194 |
| 195 // static |
| 196 MessageDispatchContext* MessageDispatchContext::current() { |
| 197 return g_tls_message_dispatch_context.Get().Get(); |
| 198 } |
| 199 |
| 200 const ReportBadMessageCallback& |
| 201 MessageDispatchContext::GetBadMessageCallback() { |
| 202 if (bad_message_callback_.is_null()) { |
| 203 std::unique_ptr<Message> new_message(new Message); |
| 204 message_->MoveTo(new_message.get()); |
| 205 bad_message_callback_ = base::Bind(&Message::NotifyBadMessage, |
| 206 base::Owned(new_message.release())); |
| 207 } |
| 208 return bad_message_callback_; |
| 209 } |
| 210 |
| 211 // static |
| 212 void SyncMessageResponseSetup::SetCurrentSyncResponseMessage(Message* message) { |
| 213 SyncMessageResponseContext* context = SyncMessageResponseContext::current(); |
| 214 if (context) |
| 215 message->MoveTo(&context->response_); |
| 216 } |
| 217 |
| 218 } // namespace internal |
| 219 |
| 125 } // namespace mojo | 220 } // namespace mojo |
| OLD | NEW |