| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/lib/validation_errors.h" | 5 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "mojo/public/cpp/bindings/message.h" | 8 #include "mojo/public/cpp/bindings/message.h" |
| 9 | 9 |
| 10 namespace mojo { | 10 namespace mojo { |
| 11 namespace internal { | 11 namespace internal { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 ValidationErrorObserverForTesting* g_validation_error_observer = nullptr; | 14 ValidationErrorObserverForTesting* g_validation_error_observer = nullptr; |
| 15 SerializationWarningObserverForTesting* g_serialization_warning_observer = | 15 SerializationWarningObserverForTesting* g_serialization_warning_observer = |
| 16 nullptr; | 16 nullptr; |
| 17 bool g_suppress_logging = false; |
| 17 | 18 |
| 18 } // namespace | 19 } // namespace |
| 19 | 20 |
| 20 const char* ValidationErrorToString(ValidationError error) { | 21 const char* ValidationErrorToString(ValidationError error) { |
| 21 switch (error) { | 22 switch (error) { |
| 22 case VALIDATION_ERROR_NONE: | 23 case VALIDATION_ERROR_NONE: |
| 23 return "VALIDATION_ERROR_NONE"; | 24 return "VALIDATION_ERROR_NONE"; |
| 24 case VALIDATION_ERROR_MISALIGNED_OBJECT: | 25 case VALIDATION_ERROR_MISALIGNED_OBJECT: |
| 25 return "VALIDATION_ERROR_MISALIGNED_OBJECT"; | 26 return "VALIDATION_ERROR_MISALIGNED_OBJECT"; |
| 26 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE: | 27 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 65 |
| 65 void ReportValidationError(ValidationContext* context, | 66 void ReportValidationError(ValidationContext* context, |
| 66 ValidationError error, | 67 ValidationError error, |
| 67 const char* description) { | 68 const char* description) { |
| 68 if (g_validation_error_observer) { | 69 if (g_validation_error_observer) { |
| 69 g_validation_error_observer->set_last_error(error); | 70 g_validation_error_observer->set_last_error(error); |
| 70 return; | 71 return; |
| 71 } | 72 } |
| 72 | 73 |
| 73 if (description) { | 74 if (description) { |
| 74 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error) << " (" | 75 if (!g_suppress_logging) { |
| 75 << description << ")"; | 76 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error) |
| 77 << " (" << description << ")"; |
| 78 } |
| 76 if (context->message()) { | 79 if (context->message()) { |
| 77 context->message()->NotifyBadMessage( | 80 context->message()->NotifyBadMessage( |
| 78 base::StringPrintf("Validation failed for %s [%s (%s)]", | 81 base::StringPrintf("Validation failed for %s [%s (%s)]", |
| 79 context->description().data(), | 82 context->description().data(), |
| 80 ValidationErrorToString(error), description)); | 83 ValidationErrorToString(error), description)); |
| 81 } | 84 } |
| 82 } else { | 85 } else { |
| 83 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error); | 86 if (!g_suppress_logging) |
| 87 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error); |
| 84 if (context->message()) { | 88 if (context->message()) { |
| 85 context->message()->NotifyBadMessage( | 89 context->message()->NotifyBadMessage( |
| 86 base::StringPrintf("Validation failed for %s [%s]", | 90 base::StringPrintf("Validation failed for %s [%s]", |
| 87 context->description().data(), | 91 context->description().data(), |
| 88 ValidationErrorToString(error))); | 92 ValidationErrorToString(error))); |
| 89 } | 93 } |
| 90 } | 94 } |
| 91 } | 95 } |
| 92 | 96 |
| 93 void ReportValidationErrorForMessage( | 97 void ReportValidationErrorForMessage( |
| 94 mojo::Message* message, | 98 mojo::Message* message, |
| 95 ValidationError error, | 99 ValidationError error, |
| 96 const char* description) { | 100 const char* description) { |
| 97 ValidationContext validation_context( | 101 ValidationContext validation_context( |
| 98 message->data(), message->data_num_bytes(), | 102 message->data(), message->data_num_bytes(), |
| 99 message->handles()->size(), message, | 103 message->handles()->size(), message, |
| 100 description); | 104 description); |
| 101 ReportValidationError(&validation_context, error); | 105 ReportValidationError(&validation_context, error); |
| 102 } | 106 } |
| 103 | 107 |
| 108 ScopedSuppressValidationErrorLoggingForTests |
| 109 ::ScopedSuppressValidationErrorLoggingForTests() |
| 110 : was_suppressed_(g_suppress_logging) { |
| 111 g_suppress_logging = true; |
| 112 } |
| 113 |
| 114 ScopedSuppressValidationErrorLoggingForTests |
| 115 ::~ScopedSuppressValidationErrorLoggingForTests() { |
| 116 g_suppress_logging = was_suppressed_; |
| 117 } |
| 118 |
| 104 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting( | 119 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting( |
| 105 const base::Closure& callback) | 120 const base::Closure& callback) |
| 106 : last_error_(VALIDATION_ERROR_NONE), callback_(callback) { | 121 : last_error_(VALIDATION_ERROR_NONE), callback_(callback) { |
| 107 DCHECK(!g_validation_error_observer); | 122 DCHECK(!g_validation_error_observer); |
| 108 g_validation_error_observer = this; | 123 g_validation_error_observer = this; |
| 109 } | 124 } |
| 110 | 125 |
| 111 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() { | 126 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() { |
| 112 DCHECK(g_validation_error_observer == this); | 127 DCHECK(g_validation_error_observer == this); |
| 113 g_validation_error_observer = nullptr; | 128 g_validation_error_observer = nullptr; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 129 } | 144 } |
| 130 | 145 |
| 131 SerializationWarningObserverForTesting:: | 146 SerializationWarningObserverForTesting:: |
| 132 ~SerializationWarningObserverForTesting() { | 147 ~SerializationWarningObserverForTesting() { |
| 133 DCHECK(g_serialization_warning_observer == this); | 148 DCHECK(g_serialization_warning_observer == this); |
| 134 g_serialization_warning_observer = nullptr; | 149 g_serialization_warning_observer = nullptr; |
| 135 } | 150 } |
| 136 | 151 |
| 137 } // namespace internal | 152 } // namespace internal |
| 138 } // namespace mojo | 153 } // namespace mojo |
| OLD | NEW |