Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: mojo/public/cpp/bindings/lib/message.cc

Issue 2064903002: Mojo: Report bindings validation errors via MojoNotifyBadMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/logging.h" 14 #include "base/logging.h"
15 #include "base/strings/stringprintf.h"
15 16
16 namespace mojo { 17 namespace mojo {
17 18
18 Message::Message() { 19 Message::Message() {
19 } 20 }
20 21
21 Message::~Message() { 22 Message::~Message() {
22 CloseHandles(); 23 CloseHandles();
23 } 24 }
24 25
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 void* new_buffer = nullptr; 72 void* new_buffer = nullptr;
72 rv = GetMessageBuffer(new_message.get(), &new_buffer); 73 rv = GetMessageBuffer(new_message.get(), &new_buffer);
73 CHECK_EQ(rv, MOJO_RESULT_OK); 74 CHECK_EQ(rv, MOJO_RESULT_OK);
74 75
75 memcpy(new_buffer, data(), data_num_bytes()); 76 memcpy(new_buffer, data(), data_num_bytes());
76 buffer_.reset(); 77 buffer_.reset();
77 78
78 return new_message; 79 return new_message;
79 } 80 }
80 81
82 void Message::NotifyBadMessage(const std::string& error) {
83 buffer_->NotifyBadMessage(error);
84 }
85
81 void Message::CloseHandles() { 86 void Message::CloseHandles() {
82 for (std::vector<Handle>::iterator it = handles_.begin(); 87 for (std::vector<Handle>::iterator it = handles_.begin();
83 it != handles_.end(); ++it) { 88 it != handles_.end(); ++it) {
84 if (it->is_valid()) 89 if (it->is_valid())
85 CloseRaw(*it); 90 CloseRaw(*it);
86 } 91 }
87 } 92 }
88 93
94
95 MessageReceiver::Result::Result(Type type) : Result(type, "") {}
yzshen1 2016/06/15 16:22:33 nit: it is preferred to use a default constructed
96
97 MessageReceiver::Result::Result(Type type, const std::string& details)
98 : type_(type), details_(details) {}
99
100 MessageReceiver::Result::Result(Result&& other) { *this = std::move(other); }
101
102 MessageReceiver::Result::~Result() {}
103
104 MessageReceiver::Result& MessageReceiver::Result::operator=(Result&& other) {
105 type_ = other.type_;
106 details_ = std::move(other.details_);
107 other.message_.MoveTo(&message_);
108 return *this;
109 }
110
111 // static
112 MessageReceiver::Result MessageReceiver::Result::ForBadMessage(
113 const std::string& details,
114 Message* message) {
115 Result error(Type::BAD_MESSAGE, details);
116 error.set_message(message);
117 return error;
118 }
119
120 // static
121 MessageReceiver::Result MessageReceiver::Result::ForBadRequest(
122 const std::string& interface_name,
123 const std::string& method_name,
124 Message* message) {
125 Result error(Type::BAD_MESSAGE,
126 base::StringPrintf("%s::%s", interface_name.c_str(),
127 method_name.c_str()));
128 error.set_message(message);
129 return error;
130 }
131
132 // static
133 MessageReceiver::Result MessageReceiver::Result::ForBadResponse(
134 const std::string& interface_name,
135 const std::string& method_name,
136 Message* message) {
137 Result error(Type::BAD_MESSAGE,
138 base::StringPrintf("%s::%s",
139 interface_name.c_str(), method_name.c_str()));
140 error.set_message(message);
141 return error;
142 }
143
144 // static
145 MessageReceiver::Result MessageReceiver::Result::ForUnexpectedRequest(
146 const std::string& interface_name,
147 Message* message) {
148 Result error(Type::BAD_MESSAGE,
yzshen1 2016/06/15 16:22:33 It seems the difference between unexpected request
149 base::StringPrintf("%s (unexpected request %u)",
150 interface_name.c_str(), message->name()));
151 error.set_message(message);
152 return error;
153 }
154
155 // static
156 MessageReceiver::Result MessageReceiver::Result::ForUnexpectedResponse(
157 const std::string& interface_name,
158 Message* message) {
159 Result error(Type::BAD_MESSAGE,
160 base::StringPrintf("%s (unexpected response %u)",
161 interface_name.c_str(), message->name()));
162 error.set_message(message);
163 return error;
164 }
165
166 // static
167 MessageReceiver::Result MessageReceiver::Result::ForBadControlMessage(
yzshen1 2016/06/15 16:22:33 Does this also apply to "pipe-level" control messa
168 const std::string& interface_name,
169 Message* message) {
170 Result error(Type::BAD_MESSAGE,
171 base::StringPrintf("%s (control message ID %u)",
172 interface_name.c_str(), message->name()));
173 error.set_message(message);
174 return error;
175 }
176
89 MojoResult ReadMessage(MessagePipeHandle handle, Message* message) { 177 MojoResult ReadMessage(MessagePipeHandle handle, Message* message) {
90 MojoResult rv; 178 MojoResult rv;
91 179
92 std::vector<Handle> handles; 180 std::vector<Handle> handles;
93 ScopedMessageHandle mojo_message; 181 ScopedMessageHandle mojo_message;
94 uint32_t num_bytes = 0, num_handles = 0; 182 uint32_t num_bytes = 0, num_handles = 0;
95 rv = ReadMessageNew(handle, 183 rv = ReadMessageNew(handle,
96 &mojo_message, 184 &mojo_message,
97 &num_bytes, 185 &num_bytes,
98 nullptr, 186 nullptr,
(...skipping 12 matching lines...) Expand all
111 199
112 if (rv != MOJO_RESULT_OK) 200 if (rv != MOJO_RESULT_OK)
113 return rv; 201 return rv;
114 202
115 message->InitializeFromMojoMessage( 203 message->InitializeFromMojoMessage(
116 std::move(mojo_message), num_bytes, &handles); 204 std::move(mojo_message), num_bytes, &handles);
117 return MOJO_RESULT_OK; 205 return MOJO_RESULT_OK;
118 } 206 }
119 207
120 } // namespace mojo 208 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698