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 "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "extensions/renderer/api_binding_test.h" | |
| 10 #include "extensions/renderer/api_binding_test_util.h" | |
| 11 #include "gin/converter.h" | |
| 12 #include "gin/public/context_holder.h" | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 std::string GetLastError(v8::Local<v8::Object> parent, | |
| 19 v8::Local<v8::Context> context) { | |
| 20 v8::Local<v8::Value> last_error = | |
| 21 GetPropertyFromObject(parent, context, "lastError"); | |
| 22 if (last_error.IsEmpty() || !last_error->IsObject()) | |
| 23 return V8ToString(last_error, context); | |
| 24 v8::Local<v8::Value> message = | |
| 25 GetPropertyFromObject(last_error.As<v8::Object>(), context, "message"); | |
| 26 return V8ToString(message, context); | |
| 27 } | |
| 28 | |
| 29 using ParentList = | |
| 30 std::vector<std::pair<v8::Local<v8::Context>, v8::Local<v8::Object>>>; | |
| 31 v8::Local<v8::Object> GetParent( | |
| 32 const ParentList& parents, | |
| 33 v8::Local<v8::Context> context) { | |
| 34 // This would be simpler with a map<context, object>, but Local<> doesn't | |
| 35 // define an operator<. | |
| 36 for (const auto& parent : parents) { | |
| 37 if (parent.first == context) | |
| 38 return parent.second; | |
| 39 } | |
| 40 return v8::Local<v8::Object>(); | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 using APILastErrorTest = APIBindingTest; | |
| 46 | |
| 47 // Test basic functionality of the lastError object. | |
| 48 TEST_F(APILastErrorTest, TestLastError) { | |
| 49 v8::HandleScope handle_scope(isolate()); | |
| 50 v8::Local<v8::Context> context = ContextLocal(); | |
| 51 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
| 52 | |
| 53 ParentList parents = {{context, parent_object}}; | |
| 54 APILastError last_error(base::Bind(&GetParent, parents)); | |
| 55 | |
| 56 EXPECT_EQ("undefined", GetLastError(parent_object, context)); | |
| 57 | |
| 58 last_error.SetError(context, "Some last error"); | |
| 59 EXPECT_EQ("\"Some last error\"", GetLastError(parent_object, context)); | |
| 60 | |
| 61 last_error.ClearError(context, false); | |
| 62 EXPECT_EQ("undefined", GetLastError(parent_object, context)); | |
| 63 } | |
| 64 | |
| 65 // Test throwing an error if the last error wasn't checked. | |
| 66 TEST_F(APILastErrorTest, ReportIfUnchecked) { | |
| 67 v8::HandleScope handle_scope(isolate()); | |
| 68 v8::Local<v8::Context> context = ContextLocal(); | |
| 69 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
| 70 | |
| 71 ParentList parents = {{context, parent_object}}; | |
| 72 APILastError last_error(base::Bind(&GetParent, parents)); | |
| 73 | |
| 74 { | |
| 75 v8::TryCatch try_catch(isolate()); | |
|
jbroman
2017/02/06 18:31:26
nit: whichever you prefer, but you could reuse the
Devlin
2017/02/06 20:28:22
I actually slightly prefer this - it makes it easi
| |
| 76 last_error.SetError(context, "foo"); | |
| 77 // GetLastError() will count as accessing the error property, so we | |
| 78 // shouldn't throw an exception. | |
| 79 EXPECT_EQ("\"foo\"", GetLastError(parent_object, context)); | |
| 80 last_error.ClearError(context, true); | |
| 81 EXPECT_FALSE(try_catch.HasCaught()); | |
| 82 } | |
| 83 | |
| 84 { | |
| 85 v8::TryCatch try_catch(isolate()); | |
| 86 // This time, we should throw an exception. | |
| 87 last_error.SetError(context, "A last error"); | |
| 88 last_error.ClearError(context, true); | |
| 89 EXPECT_TRUE(try_catch.HasCaught()); | |
| 90 EXPECT_EQ("Uncaught Error: A last error", | |
| 91 gin::V8ToString(try_catch.Message()->Get())); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 // Test behavior when something else sets a lastError property on the parent | |
| 96 // object. | |
| 97 TEST_F(APILastErrorTest, NonLastErrorObject) { | |
| 98 v8::HandleScope handle_scope(isolate()); | |
| 99 v8::Local<v8::Context> context = ContextLocal(); | |
| 100 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
| 101 | |
| 102 ParentList parents = {{context, parent_object}}; | |
| 103 APILastError last_error(base::Bind(&GetParent, parents)); | |
| 104 | |
| 105 auto checked_set = [context](v8::Local<v8::Object> object, | |
| 106 base::StringPiece key, | |
| 107 v8::Local<v8::Value> value) { | |
| 108 v8::Maybe<bool> success = | |
| 109 object->Set(context, gin::StringToSymbol(context->GetIsolate(), key), | |
| 110 value); | |
| 111 ASSERT_TRUE(success.IsJust()); | |
| 112 ASSERT_TRUE(success.FromJust()); | |
| 113 }; | |
| 114 | |
| 115 // Set a "fake" lastError property on the parent. | |
| 116 v8::Local<v8::Object> fake_last_error = v8::Object::New(isolate()); | |
| 117 checked_set(fake_last_error, "message", | |
| 118 gin::StringToV8(isolate(), "fake error")); | |
| 119 checked_set(parent_object, "lastError", fake_last_error); | |
| 120 | |
| 121 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
| 122 | |
| 123 // The bindings shouldn't mangle an existing property. | |
| 124 // TODO(devlin): Or should we? If someone sets chrome.runtime.lastError, it | |
| 125 // might be the right course of action to overwrite it. | |
| 126 last_error.SetError(context, "Real last error"); | |
| 127 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
| 128 last_error.ClearError(context, false); | |
| 129 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
| 130 } | |
| 131 | |
| 132 // Test lastError in multiple different contexts. | |
| 133 TEST_F(APILastErrorTest, MultipleContexts) { | |
| 134 v8::HandleScope handle_scope(isolate()); | |
| 135 v8::Local<v8::Context> context_a = ContextLocal(); | |
| 136 v8::Local<v8::Context> context_b = v8::Context::New(isolate()); | |
| 137 gin::ContextHolder holder_b(isolate()); | |
| 138 holder_b.SetContext(context_b); | |
| 139 | |
| 140 v8::Local<v8::Object> parent_a = v8::Object::New(isolate()); | |
| 141 v8::Local<v8::Object> parent_b = v8::Object::New(isolate()); | |
| 142 ParentList parents = {{context_a, parent_a}, {context_b, parent_b}}; | |
| 143 APILastError last_error(base::Bind(&GetParent, parents)); | |
| 144 | |
| 145 last_error.SetError(context_a, "Last error a"); | |
| 146 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
| 147 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
| 148 | |
| 149 last_error.SetError(context_b, "Last error b"); | |
| 150 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
| 151 EXPECT_EQ("\"Last error b\"", GetLastError(parent_b, context_b)); | |
| 152 | |
| 153 last_error.ClearError(context_b, false); | |
| 154 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
| 155 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
| 156 | |
| 157 last_error.ClearError(context_a, false); | |
| 158 EXPECT_EQ("undefined", GetLastError(parent_a, context_a)); | |
| 159 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
| 160 } | |
| 161 | |
| 162 } // namespace extensions | |
| OLD | NEW |