OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
6 | |
7 #include "mojo/public/cpp/environment/logging.h" | |
8 | |
9 namespace mojo { | |
10 namespace internal { | |
11 namespace { | |
12 | |
13 ValidationErrorObserverForTesting* g_validation_error_observer = nullptr; | |
14 SerializationWarningObserverForTesting* g_serialization_warning_observer = | |
15 nullptr; | |
16 | |
17 } // namespace | |
18 | |
19 const char* ValidationErrorToString(ValidationError error) { | |
20 switch (error) { | |
21 case VALIDATION_ERROR_NONE: | |
22 return "VALIDATION_ERROR_NONE"; | |
23 case VALIDATION_ERROR_MISALIGNED_OBJECT: | |
24 return "VALIDATION_ERROR_MISALIGNED_OBJECT"; | |
25 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE: | |
26 return "VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE"; | |
27 case VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER: | |
28 return "VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER"; | |
29 case VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER: | |
30 return "VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER"; | |
31 case VALIDATION_ERROR_ILLEGAL_HANDLE: | |
32 return "VALIDATION_ERROR_ILLEGAL_HANDLE"; | |
33 case VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE: | |
34 return "VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE"; | |
35 case VALIDATION_ERROR_ILLEGAL_POINTER: | |
36 return "VALIDATION_ERROR_ILLEGAL_POINTER"; | |
37 case VALIDATION_ERROR_UNEXPECTED_NULL_POINTER: | |
38 return "VALIDATION_ERROR_UNEXPECTED_NULL_POINTER"; | |
39 case VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION: | |
40 return "VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION"; | |
41 case VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID: | |
42 return "VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID"; | |
43 case VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP: | |
44 return "VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP"; | |
45 } | |
46 | |
47 return "Unknown error"; | |
48 } | |
49 | |
50 void ReportValidationError(ValidationError error, const char* description) { | |
51 if (g_validation_error_observer) { | |
52 g_validation_error_observer->set_last_error(error); | |
53 } else if (description) { | |
54 MOJO_LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error) | |
55 << " (" << description << ")"; | |
56 } else { | |
57 MOJO_LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error); | |
58 } | |
59 } | |
60 | |
61 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting() | |
62 : last_error_(VALIDATION_ERROR_NONE) { | |
63 MOJO_DCHECK(!g_validation_error_observer); | |
64 g_validation_error_observer = this; | |
65 } | |
66 | |
67 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() { | |
68 MOJO_DCHECK(g_validation_error_observer == this); | |
69 g_validation_error_observer = nullptr; | |
70 } | |
71 | |
72 bool ReportSerializationWarning(ValidationError error) { | |
73 if (g_serialization_warning_observer) { | |
74 g_serialization_warning_observer->set_last_warning(error); | |
75 return true; | |
76 } | |
77 | |
78 return false; | |
79 } | |
80 | |
81 SerializationWarningObserverForTesting::SerializationWarningObserverForTesting() | |
82 : last_warning_(VALIDATION_ERROR_NONE) { | |
83 MOJO_DCHECK(!g_serialization_warning_observer); | |
84 g_serialization_warning_observer = this; | |
85 } | |
86 | |
87 SerializationWarningObserverForTesting:: | |
88 ~SerializationWarningObserverForTesting() { | |
89 MOJO_DCHECK(g_serialization_warning_observer == this); | |
90 g_serialization_warning_observer = nullptr; | |
91 } | |
92 | |
93 } // namespace internal | |
94 } // namespace mojo | |
OLD | NEW |