Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "extensions/renderer/api_last_error.h" | |
| 6 | |
| 7 #include "gin/converter.h" | |
| 8 #include "gin/handle.h" | |
| 9 #include "gin/object_template_builder.h" | |
| 10 #include "gin/wrappable.h" | |
| 11 | |
| 12 namespace extensions { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char kLastErrorProperty[] = "lastError"; | |
| 17 | |
| 18 class LastErrorObject final : public gin::Wrappable<LastErrorObject> { | |
| 19 public: | |
| 20 explicit LastErrorObject(const std::string& error) : error_(error) {} | |
| 21 | |
| 22 static gin::WrapperInfo kWrapperInfo; | |
| 23 | |
| 24 // gin::Wrappable: | |
| 25 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
| 26 v8::Isolate* isolate) override { | |
| 27 DCHECK(isolate); | |
| 28 return Wrappable<LastErrorObject>::GetObjectTemplateBuilder(isolate) | |
| 29 .SetProperty("message", &LastErrorObject::GetLastError); | |
| 30 } | |
| 31 | |
| 32 void Reset(const std::string& error) { | |
| 33 error_ = error; | |
| 34 accessed_ = false; | |
| 35 } | |
| 36 | |
| 37 const std::string& error() const { return error_; } | |
| 38 bool accessed() const { return accessed_; } | |
| 39 | |
| 40 private: | |
| 41 std::string GetLastError() { | |
| 42 accessed_ = true; | |
| 43 return error_; | |
| 44 } | |
| 45 | |
| 46 std::string error_; | |
| 47 bool accessed_ = false; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(LastErrorObject); | |
| 50 }; | |
| 51 | |
| 52 gin::WrapperInfo LastErrorObject::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 APILastError::APILastError(const GetParent& get_parent) | |
| 57 : get_parent_(get_parent) {} | |
| 58 APILastError::APILastError(APILastError&& other) = default; | |
| 59 APILastError::~APILastError() = default; | |
| 60 | |
| 61 void APILastError::SetError(v8::Local<v8::Context> context, | |
| 62 const std::string& error) { | |
| 63 v8::Isolate* isolate = context->GetIsolate(); | |
| 64 DCHECK(isolate); | |
| 65 v8::HandleScope handle_scope(isolate); | |
| 66 | |
| 67 // The various accesses/sets on an object could potentially fail if script has | |
| 68 // set any crazy interceptors. For the most part, we don't care about behaving | |
| 69 // perfectly in these circumstances, but we eat the exception so callers don't | |
| 70 // have to worry about it. We also SetVerbose() so that developers will have a | |
| 71 // clue what happened if this does arise. | |
| 72 // TODO(devlin): Whether or not this needs to be verbose is debatable. | |
| 73 v8::TryCatch try_catch(isolate); | |
| 74 try_catch.SetVerbose(true); | |
| 75 | |
| 76 v8::Local<v8::Object> parent = get_parent_.Run(context); | |
| 77 if (parent.IsEmpty()) | |
| 78 return; | |
| 79 v8::Local<v8::String> key = gin::StringToSymbol(isolate, kLastErrorProperty); | |
| 80 v8::MaybeLocal<v8::Value> maybe_error = parent->Get(context, key); | |
| 81 v8::Local<v8::Value> local_error; | |
| 82 if (!maybe_error.ToLocal(&local_error)) | |
|
jbroman
2017/02/13 19:41:27
nit: Don't have to, but it's usual to do the ToLoc
Devlin
2017/02/14 04:57:08
Whoops, been trying to do that more, but didn't he
| |
| 83 return; | |
| 84 | |
| 85 if (!local_error->IsUndefined()) { | |
| 86 // There may be an existing last error to overwrite. | |
| 87 LastErrorObject* last_error = nullptr; | |
| 88 if (!gin::Converter<LastErrorObject*>::FromV8(context->GetIsolate(), | |
| 89 local_error, &last_error)) { | |
| 90 // If it's not a real lastError (e.g. if a script manually set it), don't | |
| 91 // do anything. | |
| 92 return; | |
| 93 } | |
| 94 last_error->Reset(error); | |
| 95 } else { | |
| 96 DCHECK(context->GetIsolate()); | |
| 97 v8::Local<v8::Value> last_error = | |
| 98 gin::CreateHandle(context->GetIsolate(), new LastErrorObject(error)) | |
| 99 .ToV8(); | |
| 100 DCHECK(!last_error.IsEmpty()); | |
| 101 // This Set() can fail, but there's nothing to do if it does (the exception | |
| 102 // will be caught by the TryCatch above). | |
| 103 ignore_result(parent->Set( | |
| 104 context, gin::StringToSymbol(isolate, kLastErrorProperty), last_error)); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void APILastError::ClearError(v8::Local<v8::Context> context, | |
| 109 bool report_if_unchecked) { | |
| 110 v8::Isolate* isolate = context->GetIsolate(); | |
| 111 v8::HandleScope handle_scope(isolate); | |
| 112 | |
| 113 v8::Local<v8::Object> parent; | |
| 114 LastErrorObject* last_error = nullptr; | |
| 115 v8::Local<v8::String> key; | |
| 116 { | |
| 117 // See comment in SetError(). | |
| 118 v8::TryCatch try_catch(isolate); | |
| 119 try_catch.SetVerbose(true); | |
| 120 | |
| 121 parent = get_parent_.Run(context); | |
| 122 if (parent.IsEmpty()) | |
| 123 return; | |
| 124 key = gin::StringToSymbol(isolate, kLastErrorProperty); | |
| 125 v8::MaybeLocal<v8::Value> maybe_error = parent->Get(context, key); | |
|
jbroman
2017/02/13 19:41:27
ditto here about ToLocal
Devlin
2017/02/14 04:57:08
Done.
| |
| 126 v8::Local<v8::Value> local_error; | |
| 127 if (!maybe_error.ToLocal(&local_error)) | |
| 128 return; | |
| 129 if (!gin::Converter<LastErrorObject*>::FromV8(context->GetIsolate(), | |
| 130 local_error, &last_error)) { | |
| 131 return; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 if (report_if_unchecked && !last_error->accessed()) { | |
| 136 isolate->ThrowException( | |
| 137 v8::Exception::Error(gin::StringToV8(isolate, last_error->error()))); | |
| 138 } | |
| 139 | |
| 140 // See comment in SetError(). | |
| 141 v8::TryCatch try_catch(isolate); | |
| 142 try_catch.SetVerbose(true); | |
| 143 | |
| 144 // This Delete() can fail, but there's nothing to do if it does (the exception | |
| 145 // will be caught by the TryCatch above). | |
| 146 parent->Delete(context, key); | |
| 147 } | |
| 148 | |
| 149 } // namespace extensions | |
| OLD | NEW |