Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / components / guest_view / renderer / guest_view_request.cc
blobf666c1c5dc73ac9c8f94896e491ca1a2088f378b
1 // Copyright 2015 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.
5 #include "components/guest_view/renderer/guest_view_request.h"
7 #include "components/guest_view/common/guest_view_messages.h"
8 #include "components/guest_view/renderer/guest_view_container.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_view.h"
11 #include "third_party/WebKit/public/web/WebLocalFrame.h"
12 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
13 #include "third_party/WebKit/public/web/WebView.h"
15 namespace guest_view {
17 GuestViewRequest::GuestViewRequest(GuestViewContainer* container,
18 v8::Local<v8::Function> callback,
19 v8::Isolate* isolate)
20 : container_(container),
21 callback_(isolate, callback),
22 isolate_(isolate) {
25 GuestViewRequest::~GuestViewRequest() {
28 void GuestViewRequest::ExecuteCallbackIfAvailable(
29 int argc,
30 scoped_ptr<v8::Local<v8::Value>[]> argv) {
31 if (callback_.IsEmpty())
32 return;
34 v8::HandleScope handle_scope(isolate());
35 v8::Local<v8::Function> callback =
36 v8::Local<v8::Function>::New(isolate_, callback_);
37 v8::Local<v8::Context> context = callback->CreationContext();
38 if (context.IsEmpty())
39 return;
41 v8::Context::Scope context_scope(context);
42 blink::WebScopedMicrotaskSuppression suppression;
44 callback->Call(context->Global(), argc, argv.get());
47 GuestViewAttachRequest::GuestViewAttachRequest(
48 GuestViewContainer* container,
49 int guest_instance_id,
50 scoped_ptr<base::DictionaryValue> params,
51 v8::Local<v8::Function> callback,
52 v8::Isolate* isolate)
53 : GuestViewRequest(container, callback, isolate),
54 guest_instance_id_(guest_instance_id),
55 params_(params.Pass()) {
58 GuestViewAttachRequest::~GuestViewAttachRequest() {
61 void GuestViewAttachRequest::PerformRequest() {
62 if (!container()->render_frame())
63 return;
65 // Step 1, send the attach params to guest_view/.
66 container()->render_frame()->Send(
67 new GuestViewHostMsg_AttachGuest(container()->element_instance_id(),
68 guest_instance_id_,
69 *params_));
71 // Step 2, attach plugin through content/.
72 container()->render_frame()->AttachGuest(container()->element_instance_id());
75 void GuestViewAttachRequest::HandleResponse(const IPC::Message& message) {
76 // TODO(fsamuel): Rename this message so that it's apparent that this is a
77 // response to GuestViewHostMsg_AttachGuest. Perhaps
78 // GuestViewMsg_AttachGuest_ACK?
79 GuestViewMsg_GuestAttached::Param param;
80 if (!GuestViewMsg_GuestAttached::Read(&message, &param))
81 return;
83 content::RenderView* guest_proxy_render_view =
84 content::RenderView::FromRoutingID(base::get<1>(param));
85 // TODO(fsamuel): Should we be reporting an error to JavaScript or DCHECKing?
86 if (!guest_proxy_render_view)
87 return;
89 v8::HandleScope handle_scope(isolate());
90 blink::WebFrame* frame = guest_proxy_render_view->GetWebView()->mainFrame();
91 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global();
93 const int argc = 1;
94 scoped_ptr<v8::Local<v8::Value>[]> argv(new v8::Local<v8::Value>[argc]);
95 argv[0] = window;
97 // Call the AttachGuest API's callback with the guest proxy as the first
98 // parameter.
99 ExecuteCallbackIfAvailable(argc, argv.Pass());
102 GuestViewDetachRequest::GuestViewDetachRequest(
103 GuestViewContainer* container,
104 v8::Local<v8::Function> callback,
105 v8::Isolate* isolate)
106 : GuestViewRequest(container, callback, isolate) {
109 GuestViewDetachRequest::~GuestViewDetachRequest() {
112 void GuestViewDetachRequest::PerformRequest() {
113 if (!container()->render_frame())
114 return;
116 container()->render_frame()->DetachGuest(container()->element_instance_id());
119 void GuestViewDetachRequest::HandleResponse(const IPC::Message& message) {
120 ExecuteCallbackIfAvailable(0 /* argc */, nullptr);
123 } // namespace guest_view