1 // Copyright 2014 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.
5 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
7 #include "mojo/public/cpp/environment/logging.h"
13 ValidationErrorObserverForTesting
* g_validation_error_observer
= NULL
;
14 SerializationWarningObserverForTesting
* g_serialization_warning_observer
= NULL
;
18 const char* ValidationErrorToString(ValidationError error
) {
20 case VALIDATION_ERROR_NONE
:
21 return "VALIDATION_ERROR_NONE";
22 case VALIDATION_ERROR_MISALIGNED_OBJECT
:
23 return "VALIDATION_ERROR_MISALIGNED_OBJECT";
24 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE
:
25 return "VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE";
26 case VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER
:
27 return "VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER";
28 case VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER
:
29 return "VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER";
30 case VALIDATION_ERROR_ILLEGAL_HANDLE
:
31 return "VALIDATION_ERROR_ILLEGAL_HANDLE";
32 case VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE
:
33 return "VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE";
34 case VALIDATION_ERROR_ILLEGAL_POINTER
:
35 return "VALIDATION_ERROR_ILLEGAL_POINTER";
36 case VALIDATION_ERROR_UNEXPECTED_NULL_POINTER
:
37 return "VALIDATION_ERROR_UNEXPECTED_NULL_POINTER";
38 case VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION
:
39 return "VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION";
40 case VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID
:
41 return "VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID";
44 return "Unknown error";
47 void ReportValidationError(ValidationError error
, const char* description
) {
48 if (g_validation_error_observer
) {
49 g_validation_error_observer
->set_last_error(error
);
50 } else if (description
) {
51 MOJO_LOG(ERROR
) << "Invalid message: " << ValidationErrorToString(error
)
52 << " (" << description
<< ")";
54 MOJO_LOG(ERROR
) << "Invalid message: " << ValidationErrorToString(error
);
58 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting()
59 : last_error_(VALIDATION_ERROR_NONE
) {
60 MOJO_DCHECK(!g_validation_error_observer
);
61 g_validation_error_observer
= this;
64 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() {
65 MOJO_DCHECK(g_validation_error_observer
== this);
66 g_validation_error_observer
= NULL
;
69 bool ReportSerializationWarning(ValidationError error
) {
70 if (g_serialization_warning_observer
) {
71 g_serialization_warning_observer
->set_last_warning(error
);
78 SerializationWarningObserverForTesting::SerializationWarningObserverForTesting()
79 : last_warning_(VALIDATION_ERROR_NONE
) {
80 MOJO_DCHECK(!g_serialization_warning_observer
);
81 g_serialization_warning_observer
= this;
84 SerializationWarningObserverForTesting::
85 ~SerializationWarningObserverForTesting() {
86 MOJO_DCHECK(g_serialization_warning_observer
== this);
87 g_serialization_warning_observer
= NULL
;
90 } // namespace internal