• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = NULL;
14 SerializationWarningObserverForTesting* g_serialization_warning_observer = NULL;
15 
16 }  // namespace
17 
ValidationErrorToString(ValidationError error)18 const char* ValidationErrorToString(ValidationError error) {
19   switch (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";
42   }
43 
44   return "Unknown error";
45 }
46 
ReportValidationError(ValidationError error,const char * description)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 << ")";
53   } else {
54     MOJO_LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error);
55   }
56 }
57 
ValidationErrorObserverForTesting()58 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting()
59     : last_error_(VALIDATION_ERROR_NONE) {
60   MOJO_DCHECK(!g_validation_error_observer);
61   g_validation_error_observer = this;
62 }
63 
~ValidationErrorObserverForTesting()64 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() {
65   MOJO_DCHECK(g_validation_error_observer == this);
66   g_validation_error_observer = NULL;
67 }
68 
ReportSerializationWarning(ValidationError error)69 bool ReportSerializationWarning(ValidationError error) {
70   if (g_serialization_warning_observer) {
71     g_serialization_warning_observer->set_last_warning(error);
72     return true;
73   }
74 
75   return false;
76 }
77 
SerializationWarningObserverForTesting()78 SerializationWarningObserverForTesting::SerializationWarningObserverForTesting()
79     : last_warning_(VALIDATION_ERROR_NONE) {
80   MOJO_DCHECK(!g_serialization_warning_observer);
81   g_serialization_warning_observer = this;
82 }
83 
84 SerializationWarningObserverForTesting::
~SerializationWarningObserverForTesting()85 ~SerializationWarningObserverForTesting() {
86   MOJO_DCHECK(g_serialization_warning_observer == this);
87   g_serialization_warning_observer = NULL;
88 }
89 
90 }  // namespace internal
91 }  // namespace mojo
92