| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/renderer/chrome_setting.h" | 5 #include "extensions/renderer/chrome_setting.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "extensions/renderer/api_event_handler.h" | 10 #include "extensions/renderer/api_event_handler.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 .SetProperty("onChange", &ChromeSetting::GetOnChangeEvent); | 97 .SetProperty("onChange", &ChromeSetting::GetOnChangeEvent); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void ChromeSetting::Get(gin::Arguments* arguments) { | 100 void ChromeSetting::Get(gin::Arguments* arguments) { |
| 101 HandleFunction("get", arguments); | 101 HandleFunction("get", arguments); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ChromeSetting::Set(gin::Arguments* arguments) { | 104 void ChromeSetting::Set(gin::Arguments* arguments) { |
| 105 v8::Isolate* isolate = arguments->isolate(); | 105 v8::Isolate* isolate = arguments->isolate(); |
| 106 v8::HandleScope handle_scope(isolate); | 106 v8::HandleScope handle_scope(isolate); |
| 107 | 107 v8::Local<v8::Context> context = arguments->GetHolderCreationContext(); |
| 108 v8::Local<v8::Object> holder; | |
| 109 CHECK(arguments->GetHolder(&holder)); | |
| 110 v8::Local<v8::Context> context = holder->CreationContext(); | |
| 111 | 108 |
| 112 v8::Local<v8::Value> value = arguments->PeekNext(); | 109 v8::Local<v8::Value> value = arguments->PeekNext(); |
| 113 // The set schema included in the Schema object is generic, since it varies | 110 // The set schema included in the Schema object is generic, since it varies |
| 114 // per-setting. However, this is only ever for a single setting, so we can | 111 // per-setting. However, this is only ever for a single setting, so we can |
| 115 // enforce the types more thoroughly. | 112 // enforce the types more thoroughly. |
| 116 std::string error; | 113 std::string error; |
| 117 if (!value.IsEmpty() && !argument_spec_.ParseArgument( | 114 if (!value.IsEmpty() && !argument_spec_.ParseArgument( |
| 118 context, value, *type_refs_, nullptr, &error)) { | 115 context, value, *type_refs_, nullptr, &error)) { |
| 119 arguments->ThrowTypeError("Invalid invocation"); | 116 arguments->ThrowTypeError("Invalid invocation"); |
| 120 return; | 117 return; |
| 121 } | 118 } |
| 122 HandleFunction("set", arguments); | 119 HandleFunction("set", arguments); |
| 123 } | 120 } |
| 124 | 121 |
| 125 void ChromeSetting::Clear(gin::Arguments* arguments) { | 122 void ChromeSetting::Clear(gin::Arguments* arguments) { |
| 126 HandleFunction("clear", arguments); | 123 HandleFunction("clear", arguments); |
| 127 } | 124 } |
| 128 | 125 |
| 129 v8::Local<v8::Value> ChromeSetting::GetOnChangeEvent( | 126 v8::Local<v8::Value> ChromeSetting::GetOnChangeEvent( |
| 130 gin::Arguments* arguments) { | 127 gin::Arguments* arguments) { |
| 131 v8::Isolate* isolate = arguments->isolate(); | 128 v8::Isolate* isolate = arguments->isolate(); |
| 132 v8::Local<v8::Object> holder; | 129 v8::Local<v8::Context> context = arguments->GetHolderCreationContext(); |
| 133 CHECK(arguments->GetHolder(&holder)); | |
| 134 v8::Local<v8::Context> context = holder->CreationContext(); | |
| 135 v8::Local<v8::Object> wrapper = GetWrapper(isolate); | 130 v8::Local<v8::Object> wrapper = GetWrapper(isolate); |
| 136 v8::Local<v8::Private> key = v8::Private::ForApi( | 131 v8::Local<v8::Private> key = v8::Private::ForApi( |
| 137 isolate, gin::StringToSymbol(isolate, "onChangeEvent")); | 132 isolate, gin::StringToSymbol(isolate, "onChangeEvent")); |
| 138 v8::Local<v8::Value> event; | 133 v8::Local<v8::Value> event; |
| 139 if (!wrapper->GetPrivate(context, key).ToLocal(&event)) { | 134 if (!wrapper->GetPrivate(context, key).ToLocal(&event)) { |
| 140 NOTREACHED(); | 135 NOTREACHED(); |
| 141 return v8::Local<v8::Value>(); | 136 return v8::Local<v8::Value>(); |
| 142 } | 137 } |
| 143 | 138 |
| 144 DCHECK(!event.IsEmpty()); | 139 DCHECK(!event.IsEmpty()); |
| 145 if (event->IsUndefined()) { | 140 if (event->IsUndefined()) { |
| 146 bool supports_filters = false; | 141 bool supports_filters = false; |
| 147 event = event_handler_->CreateEventInstance( | 142 event = event_handler_->CreateEventInstance( |
| 148 base::StringPrintf("types.ChromeSetting.%s.onChange", | 143 base::StringPrintf("types.ChromeSetting.%s.onChange", |
| 149 pref_name_.c_str()), | 144 pref_name_.c_str()), |
| 150 supports_filters, context); | 145 supports_filters, context); |
| 151 v8::Maybe<bool> set_result = wrapper->SetPrivate(context, key, event); | 146 v8::Maybe<bool> set_result = wrapper->SetPrivate(context, key, event); |
| 152 if (!set_result.IsJust() || !set_result.FromJust()) { | 147 if (!set_result.IsJust() || !set_result.FromJust()) { |
| 153 NOTREACHED(); | 148 NOTREACHED(); |
| 154 return v8::Local<v8::Value>(); | 149 return v8::Local<v8::Value>(); |
| 155 } | 150 } |
| 156 } | 151 } |
| 157 return event; | 152 return event; |
| 158 } | 153 } |
| 159 | 154 |
| 160 void ChromeSetting::HandleFunction(const std::string& method_name, | 155 void ChromeSetting::HandleFunction(const std::string& method_name, |
| 161 gin::Arguments* arguments) { | 156 gin::Arguments* arguments) { |
| 162 v8::Isolate* isolate = arguments->isolate(); | 157 v8::Isolate* isolate = arguments->isolate(); |
| 163 v8::HandleScope handle_scope(isolate); | 158 v8::HandleScope handle_scope(isolate); |
| 164 v8::Local<v8::Object> holder; | 159 v8::Local<v8::Context> context = arguments->GetHolderCreationContext(); |
| 165 CHECK(arguments->GetHolder(&holder)); | |
| 166 v8::Local<v8::Context> context = holder->CreationContext(); | |
| 167 | 160 |
| 168 std::vector<v8::Local<v8::Value>> argument_list; | 161 std::vector<v8::Local<v8::Value>> argument_list; |
| 169 if (arguments->Length() > 0) { | 162 if (arguments->Length() > 0) { |
| 170 // Just copying handles should never fail. | 163 // Just copying handles should never fail. |
| 171 CHECK(arguments->GetRemaining(&argument_list)); | 164 CHECK(arguments->GetRemaining(&argument_list)); |
| 172 } | 165 } |
| 173 | 166 |
| 174 std::string full_name = "types.ChromeSetting." + method_name; | 167 std::string full_name = "types.ChromeSetting." + method_name; |
| 175 std::unique_ptr<base::ListValue> converted_arguments; | 168 std::unique_ptr<base::ListValue> converted_arguments; |
| 176 v8::Local<v8::Function> callback; | 169 v8::Local<v8::Function> callback; |
| 177 std::string error; | 170 std::string error; |
| 178 if (!type_refs_->GetTypeMethodSignature(full_name)->ParseArgumentsToJSON( | 171 if (!type_refs_->GetTypeMethodSignature(full_name)->ParseArgumentsToJSON( |
| 179 context, argument_list, *type_refs_, &converted_arguments, &callback, | 172 context, argument_list, *type_refs_, &converted_arguments, &callback, |
| 180 &error)) { | 173 &error)) { |
| 181 arguments->ThrowTypeError("Invalid invocation"); | 174 arguments->ThrowTypeError("Invalid invocation"); |
| 182 return; | 175 return; |
| 183 } | 176 } |
| 184 | 177 |
| 185 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(pref_name_)); | 178 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(pref_name_)); |
| 186 request_handler_->StartRequest(context, full_name, | 179 request_handler_->StartRequest(context, full_name, |
| 187 std::move(converted_arguments), callback, | 180 std::move(converted_arguments), callback, |
| 188 v8::Local<v8::Function>()); | 181 v8::Local<v8::Function>()); |
| 189 } | 182 } |
| 190 | 183 |
| 191 } // namespace extensions | 184 } // namespace extensions |
| OLD | NEW |