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::Local<v8::Value> v8_error; |
| 81 if (!parent->Get(context, key).ToLocal(&v8_error)) |
| 82 return; |
| 83 |
| 84 if (!v8_error->IsUndefined()) { |
| 85 // There may be an existing last error to overwrite. |
| 86 LastErrorObject* last_error = nullptr; |
| 87 if (!gin::Converter<LastErrorObject*>::FromV8(context->GetIsolate(), |
| 88 v8_error, &last_error)) { |
| 89 // If it's not a real lastError (e.g. if a script manually set it), don't |
| 90 // do anything. We shouldn't mangle a property set by other script. |
| 91 // TODO(devlin): Or should we? If someone sets chrome.runtime.lastError, |
| 92 // it might be the right course of action to overwrite it. |
| 93 return; |
| 94 } |
| 95 last_error->Reset(error); |
| 96 } else { |
| 97 DCHECK(context->GetIsolate()); |
| 98 v8::Local<v8::Value> last_error = |
| 99 gin::CreateHandle(context->GetIsolate(), new LastErrorObject(error)) |
| 100 .ToV8(); |
| 101 DCHECK(!last_error.IsEmpty()); |
| 102 // This Set() can fail, but there's nothing to do if it does (the exception |
| 103 // will be caught by the TryCatch above). |
| 104 ignore_result(parent->Set( |
| 105 context, gin::StringToSymbol(isolate, kLastErrorProperty), last_error)); |
| 106 } |
| 107 } |
| 108 |
| 109 void APILastError::ClearError(v8::Local<v8::Context> context, |
| 110 bool report_if_unchecked) { |
| 111 v8::Isolate* isolate = context->GetIsolate(); |
| 112 v8::HandleScope handle_scope(isolate); |
| 113 |
| 114 v8::Local<v8::Object> parent; |
| 115 LastErrorObject* last_error = nullptr; |
| 116 v8::Local<v8::String> key; |
| 117 { |
| 118 // See comment in SetError(). |
| 119 v8::TryCatch try_catch(isolate); |
| 120 try_catch.SetVerbose(true); |
| 121 |
| 122 parent = get_parent_.Run(context); |
| 123 if (parent.IsEmpty()) |
| 124 return; |
| 125 key = gin::StringToSymbol(isolate, kLastErrorProperty); |
| 126 v8::Local<v8::Value> error; |
| 127 if (!parent->Get(context, key).ToLocal(&error)) |
| 128 return; |
| 129 if (!gin::Converter<LastErrorObject*>::FromV8(context->GetIsolate(), error, |
| 130 &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 |