Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: extensions/renderer/guest_view/guest_view_internal_custom_bindings.cc

Issue 1167423002: Use V8 Maybe APIs in extensions/renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/guest_view/guest_view_internal_custom_bindings.h" 5 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 CHECK(args.Length() >= 3 && args.Length() <= 4); 101 CHECK(args.Length() >= 3 && args.Length() <= 4);
102 // Element Instance ID. 102 // Element Instance ID.
103 CHECK(args[0]->IsInt32()); 103 CHECK(args[0]->IsInt32());
104 // Guest Instance ID. 104 // Guest Instance ID.
105 CHECK(args[1]->IsInt32()); 105 CHECK(args[1]->IsInt32());
106 // Attach Parameters. 106 // Attach Parameters.
107 CHECK(args[2]->IsObject()); 107 CHECK(args[2]->IsObject());
108 // Optional Callback Function. 108 // Optional Callback Function.
109 CHECK(args.Length() < 4 || args[3]->IsFunction()); 109 CHECK(args.Length() < 4 || args[3]->IsFunction());
110 110
111 int element_instance_id = args[0]->Int32Value(); 111 int element_instance_id = args[0].As<v8::Int32>()->Value();
112 // An element instance ID uniquely identifies a GuestViewContainer. 112 // An element instance ID uniquely identifies a GuestViewContainer.
113 auto guest_view_container = 113 auto guest_view_container =
114 guest_view::GuestViewContainer::FromID(element_instance_id); 114 guest_view::GuestViewContainer::FromID(element_instance_id);
115 115
116 // TODO(fsamuel): Should we be reporting an error if the element instance ID 116 // TODO(fsamuel): Should we be reporting an error if the element instance ID
117 // is invalid? 117 // is invalid?
118 if (!guest_view_container) 118 if (!guest_view_container)
119 return; 119 return;
120 120
121 int guest_instance_id = args[1]->Int32Value(); 121 int guest_instance_id = args[1].As<v8::Int32>()->Value();
122 122
123 scoped_ptr<base::DictionaryValue> params; 123 scoped_ptr<base::DictionaryValue> params;
124 { 124 {
125 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); 125 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
126 scoped_ptr<base::Value> params_as_value( 126 scoped_ptr<base::Value> params_as_value(
127 converter->FromV8Value(args[2], context()->v8_context())); 127 converter->FromV8Value(args[2], context()->v8_context()));
128 CHECK(params_as_value->IsType(base::Value::TYPE_DICTIONARY)); 128 CHECK(params_as_value->IsType(base::Value::TYPE_DICTIONARY));
129 params.reset( 129 params.reset(
130 static_cast<base::DictionaryValue*>(params_as_value.release())); 130 static_cast<base::DictionaryValue*>(params_as_value.release()));
131 } 131 }
(...skipping 15 matching lines...) Expand all
147 147
148 void GuestViewInternalCustomBindings::DetachGuest( 148 void GuestViewInternalCustomBindings::DetachGuest(
149 const v8::FunctionCallbackInfo<v8::Value>& args) { 149 const v8::FunctionCallbackInfo<v8::Value>& args) {
150 // Allow for an optional callback parameter. 150 // Allow for an optional callback parameter.
151 CHECK(args.Length() >= 1 && args.Length() <= 2); 151 CHECK(args.Length() >= 1 && args.Length() <= 2);
152 // Element Instance ID. 152 // Element Instance ID.
153 CHECK(args[0]->IsInt32()); 153 CHECK(args[0]->IsInt32());
154 // Optional Callback Function. 154 // Optional Callback Function.
155 CHECK(args.Length() < 2 || args[1]->IsFunction()); 155 CHECK(args.Length() < 2 || args[1]->IsFunction());
156 156
157 int element_instance_id = args[0]->Int32Value(); 157 int element_instance_id = args[0].As<v8::Int32>()->Value();
158 // An element instance ID uniquely identifies a GuestViewContainer. 158 // An element instance ID uniquely identifies a GuestViewContainer.
159 auto guest_view_container = 159 auto guest_view_container =
160 guest_view::GuestViewContainer::FromID(element_instance_id); 160 guest_view::GuestViewContainer::FromID(element_instance_id);
161 161
162 // TODO(fsamuel): Should we be reporting an error if the element instance ID 162 // TODO(fsamuel): Should we be reporting an error if the element instance ID
163 // is invalid? 163 // is invalid?
164 if (!guest_view_container) 164 if (!guest_view_container)
165 return; 165 return;
166 166
167 linked_ptr<guest_view::GuestViewRequest> request( 167 linked_ptr<guest_view::GuestViewRequest> request(
(...skipping 11 matching lines...) Expand all
179 // Default to returning null. 179 // Default to returning null.
180 args.GetReturnValue().SetNull(); 180 args.GetReturnValue().SetNull();
181 181
182 if (args.Length() != 1) 182 if (args.Length() != 1)
183 return; 183 return;
184 184
185 // The routing ID for the RenderView. 185 // The routing ID for the RenderView.
186 if (!args[0]->IsInt32()) 186 if (!args[0]->IsInt32())
187 return; 187 return;
188 188
189 int view_id = args[0]->Int32Value(); 189 int view_id = args[0].As<v8::Int32>()->Value();
190 if (view_id == MSG_ROUTING_NONE) 190 if (view_id == MSG_ROUTING_NONE)
191 return; 191 return;
192 192
193 content::RenderView* view = content::RenderView::FromRoutingID(view_id); 193 content::RenderView* view = content::RenderView::FromRoutingID(view_id);
194 if (!view) 194 if (!view)
195 return; 195 return;
196 196
197 blink::WebFrame* frame = view->GetWebView()->mainFrame(); 197 blink::WebFrame* frame = view->GetWebView()->mainFrame();
198 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); 198 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global();
199 args.GetReturnValue().Set(window); 199 args.GetReturnValue().Set(window);
200 } 200 }
201 201
202 void GuestViewInternalCustomBindings::GetViewFromID( 202 void GuestViewInternalCustomBindings::GetViewFromID(
203 const v8::FunctionCallbackInfo<v8::Value>& args) { 203 const v8::FunctionCallbackInfo<v8::Value>& args) {
204 // Default to returning null. 204 // Default to returning null.
205 args.GetReturnValue().SetNull(); 205 args.GetReturnValue().SetNull();
206 // There is one argument. 206 // There is one argument.
207 CHECK(args.Length() == 1); 207 CHECK(args.Length() == 1);
208 // The view ID. 208 // The view ID.
209 CHECK(args[0]->IsInt32()); 209 CHECK(args[0]->IsInt32());
210 int view_id = args[0]->Int32Value(); 210 int view_id = args[0].As<v8::Int32>()->Value();
211 211
212 ViewMap& view_map = weak_view_map.Get(); 212 ViewMap& view_map = weak_view_map.Get();
213 auto map_entry = view_map.find(view_id); 213 auto map_entry = view_map.find(view_id);
214 if (map_entry == view_map.end()) 214 if (map_entry == view_map.end())
215 return; 215 return;
216 216
217 auto return_object = v8::Handle<v8::Object>::New(args.GetIsolate(), 217 auto return_object = v8::Handle<v8::Object>::New(args.GetIsolate(),
218 *map_entry->second); 218 *map_entry->second);
219 args.GetReturnValue().Set(return_object); 219 args.GetReturnValue().Set(return_object);
220 } 220 }
221 221
222 void GuestViewInternalCustomBindings::RegisterDestructionCallback( 222 void GuestViewInternalCustomBindings::RegisterDestructionCallback(
223 const v8::FunctionCallbackInfo<v8::Value>& args) { 223 const v8::FunctionCallbackInfo<v8::Value>& args) {
224 // There are two parameters. 224 // There are two parameters.
225 CHECK(args.Length() == 2); 225 CHECK(args.Length() == 2);
226 // Element Instance ID. 226 // Element Instance ID.
227 CHECK(args[0]->IsInt32()); 227 CHECK(args[0]->IsInt32());
228 // Callback function. 228 // Callback function.
229 CHECK(args[1]->IsFunction()); 229 CHECK(args[1]->IsFunction());
230 230
231 int element_instance_id = args[0]->Int32Value(); 231 int element_instance_id = args[0].As<v8::Int32>()->Value();
232 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer 232 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
233 // within a RenderView. 233 // within a RenderView.
234 auto guest_view_container = static_cast<ExtensionsGuestViewContainer*>( 234 auto guest_view_container = static_cast<ExtensionsGuestViewContainer*>(
235 guest_view::GuestViewContainer::FromID(element_instance_id)); 235 guest_view::GuestViewContainer::FromID(element_instance_id));
236 if (!guest_view_container) 236 if (!guest_view_container)
237 return; 237 return;
238 238
239 guest_view_container->RegisterDestructionCallback(args[1].As<v8::Function>(), 239 guest_view_container->RegisterDestructionCallback(args[1].As<v8::Function>(),
240 args.GetIsolate()); 240 args.GetIsolate());
241 241
242 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); 242 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
243 } 243 }
244 244
245 void GuestViewInternalCustomBindings::RegisterElementResizeCallback( 245 void GuestViewInternalCustomBindings::RegisterElementResizeCallback(
246 const v8::FunctionCallbackInfo<v8::Value>& args) { 246 const v8::FunctionCallbackInfo<v8::Value>& args) {
247 // There are two parameters. 247 // There are two parameters.
248 CHECK(args.Length() == 2); 248 CHECK(args.Length() == 2);
249 // Element Instance ID. 249 // Element Instance ID.
250 CHECK(args[0]->IsInt32()); 250 CHECK(args[0]->IsInt32());
251 // Callback function. 251 // Callback function.
252 CHECK(args[1]->IsFunction()); 252 CHECK(args[1]->IsFunction());
253 253
254 int element_instance_id = args[0]->Int32Value(); 254 int element_instance_id = args[0].As<v8::Int32>()->Value();
255 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer 255 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
256 // within a RenderView. 256 // within a RenderView.
257 auto guest_view_container = static_cast<ExtensionsGuestViewContainer*>( 257 auto guest_view_container = static_cast<ExtensionsGuestViewContainer*>(
258 guest_view::GuestViewContainer::FromID(element_instance_id)); 258 guest_view::GuestViewContainer::FromID(element_instance_id));
259 if (!guest_view_container) 259 if (!guest_view_container)
260 return; 260 return;
261 261
262 guest_view_container->RegisterElementResizeCallback( 262 guest_view_container->RegisterElementResizeCallback(
263 args[1].As<v8::Function>(), args.GetIsolate()); 263 args[1].As<v8::Function>(), args.GetIsolate());
264 264
265 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); 265 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
266 } 266 }
267 267
268 void GuestViewInternalCustomBindings::RegisterView( 268 void GuestViewInternalCustomBindings::RegisterView(
269 const v8::FunctionCallbackInfo<v8::Value>& args) { 269 const v8::FunctionCallbackInfo<v8::Value>& args) {
270 // There are two parameters. 270 // There are two parameters.
271 CHECK(args.Length() == 2); 271 CHECK(args.Length() == 2);
272 // View Instance ID. 272 // View Instance ID.
273 CHECK(args[0]->IsInt32()); 273 CHECK(args[0]->IsInt32());
274 // View element. 274 // View element.
275 CHECK(args[1]->IsObject()); 275 CHECK(args[1]->IsObject());
276 276
277 // A reference to the view object is stored in |weak_view_map| using its view 277 // A reference to the view object is stored in |weak_view_map| using its view
278 // ID as the key. The reference is made weak so that it will not extend the 278 // ID as the key. The reference is made weak so that it will not extend the
279 // lifetime of the object. 279 // lifetime of the object.
280 int view_id = args[0]->Int32Value(); 280 int view_id = args[0].As<v8::Int32>()->Value();
281 auto object = 281 auto object =
282 new v8::Global<v8::Object>(args.GetIsolate(), args[1].As<v8::Object>()); 282 new v8::Global<v8::Object>(args.GetIsolate(), args[1].As<v8::Object>());
283 weak_view_map.Get().insert(std::make_pair(view_id, object)); 283 weak_view_map.Get().insert(std::make_pair(view_id, object));
284 284
285 // The view_id is given to the SetWeak callback so that that view's entry in 285 // The view_id is given to the SetWeak callback so that that view's entry in
286 // |weak_view_map| can be cleared when the view object is garbage collected. 286 // |weak_view_map| can be cleared when the view object is garbage collected.
287 object->SetWeak(new int(view_id), 287 object->SetWeak(new int(view_id),
288 &GuestViewInternalCustomBindings::ResetMapEntry, 288 &GuestViewInternalCustomBindings::ResetMapEntry,
289 v8::WeakCallbackType::kParameter); 289 v8::WeakCallbackType::kParameter);
290 } 290 }
291 291
292 void GuestViewInternalCustomBindings::RunWithGesture( 292 void GuestViewInternalCustomBindings::RunWithGesture(
293 const v8::FunctionCallbackInfo<v8::Value>& args) { 293 const v8::FunctionCallbackInfo<v8::Value>& args) {
294 // Gesture is required to request fullscreen. 294 // Gesture is required to request fullscreen.
295 blink::WebScopedUserGesture user_gesture; 295 blink::WebScopedUserGesture user_gesture;
296 CHECK_EQ(args.Length(), 1); 296 CHECK_EQ(args.Length(), 1);
297 CHECK(args[0]->IsFunction()); 297 CHECK(args[0]->IsFunction());
298 v8::Local<v8::Value> no_args; 298 v8::Local<v8::Value> no_args;
299 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); 299 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args);
300 } 300 }
301 301
302 } // namespace extensions 302 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698