| OLD | NEW |
| 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_container.h" | 5 #include "extensions/renderer/guest_view/web_view_container.h" |
| 6 | 6 |
| 7 #include "content/public/renderer/browser_plugin_delegate.h" | |
| 8 #include "content/public/renderer/render_frame.h" | 7 #include "content/public/renderer/render_frame.h" |
| 9 #include "content/public/renderer/render_view.h" | 8 #include "content/public/renderer/render_view.h" |
| 10 #include "extensions/common/extension_messages.h" | 9 #include "extensions/common/extension_messages.h" |
| 11 #include "extensions/common/guest_view/guest_view_constants.h" | 10 #include "extensions/common/guest_view/guest_view_constants.h" |
| 12 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 11 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 13 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 12 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 14 #include "third_party/WebKit/public/web/WebView.h" | 13 #include "third_party/WebKit/public/web/WebView.h" |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 typedef std::pair<int, int> GuestViewID; | 16 typedef std::pair<int, int> GuestViewID; |
| 18 typedef std::map<GuestViewID, extensions::GuestViewContainer*> | 17 typedef std::map<GuestViewID, extensions::WebViewContainer*> |
| 19 GuestViewContainerMap; | 18 WebViewContainerMap; |
| 20 static base::LazyInstance<GuestViewContainerMap> g_guest_view_container_map = | 19 static base::LazyInstance<WebViewContainerMap> g_guest_view_container_map = |
| 21 LAZY_INSTANCE_INITIALIZER; | 20 LAZY_INSTANCE_INITIALIZER; |
| 22 } // namespace | 21 } // namespace |
| 23 | 22 |
| 24 namespace extensions { | 23 namespace extensions { |
| 25 | 24 |
| 26 GuestViewContainer::AttachRequest::AttachRequest( | 25 WebViewContainer::AttachRequest::AttachRequest( |
| 27 int element_instance_id, | 26 int element_instance_id, |
| 28 int guest_instance_id, | 27 int guest_instance_id, |
| 29 scoped_ptr<base::DictionaryValue> params, | 28 scoped_ptr<base::DictionaryValue> params, |
| 30 v8::Handle<v8::Function> callback, | 29 v8::Handle<v8::Function> callback, |
| 31 v8::Isolate* isolate) | 30 v8::Isolate* isolate) |
| 32 : element_instance_id_(element_instance_id), | 31 : element_instance_id_(element_instance_id), |
| 33 guest_instance_id_(guest_instance_id), | 32 guest_instance_id_(guest_instance_id), |
| 34 params_(params.Pass()), | 33 params_(params.Pass()), |
| 35 callback_(callback), | 34 callback_(callback), |
| 36 isolate_(isolate) { | 35 isolate_(isolate) { |
| 37 } | 36 } |
| 38 | 37 |
| 39 GuestViewContainer::AttachRequest::~AttachRequest() { | 38 WebViewContainer::AttachRequest::~AttachRequest() { |
| 40 } | 39 } |
| 41 | 40 |
| 42 bool GuestViewContainer::AttachRequest::HasCallback() const { | 41 bool WebViewContainer::AttachRequest::HasCallback() const { |
| 43 return !callback_.IsEmpty(); | 42 return !callback_.IsEmpty(); |
| 44 } | 43 } |
| 45 | 44 |
| 46 v8::Handle<v8::Function> | 45 v8::Handle<v8::Function> |
| 47 GuestViewContainer::AttachRequest::GetCallback() const { | 46 WebViewContainer::AttachRequest::GetCallback() const { |
| 48 return callback_.NewHandle(isolate_); | 47 return callback_.NewHandle(isolate_); |
| 49 } | 48 } |
| 50 | 49 |
| 51 GuestViewContainer::GuestViewContainer( | 50 WebViewContainer::WebViewContainer(content::RenderFrame* render_frame) |
| 52 content::RenderFrame* render_frame, | 51 : GuestViewContainer(render_frame), |
| 53 const std::string& mime_type) | |
| 54 : content::BrowserPluginDelegate(render_frame, mime_type), | |
| 55 content::RenderFrameObserver(render_frame), | |
| 56 mime_type_(mime_type), | |
| 57 element_instance_id_(guestview::kInstanceIDNone), | |
| 58 render_view_routing_id_(render_frame->GetRenderView()->GetRoutingID()), | |
| 59 attached_(false), | |
| 60 ready_(false) { | 52 ready_(false) { |
| 61 } | 53 } |
| 62 | 54 |
| 63 GuestViewContainer::~GuestViewContainer() { | 55 WebViewContainer::~WebViewContainer() { |
| 64 if (element_instance_id_ != guestview::kInstanceIDNone) { | 56 if (element_instance_id_ != guestview::kInstanceIDNone) { |
| 65 g_guest_view_container_map.Get().erase( | 57 g_guest_view_container_map.Get().erase( |
| 66 GuestViewID(render_view_routing_id_, element_instance_id_)); | 58 GuestViewID(render_view_routing_id_, element_instance_id_)); |
| 67 } | 59 } |
| 68 } | 60 } |
| 69 | 61 |
| 70 GuestViewContainer* GuestViewContainer::FromID(int render_view_routing_id, | 62 WebViewContainer* WebViewContainer::FromID(int render_view_routing_id, |
| 71 int element_instance_id) { | 63 int element_instance_id) { |
| 72 GuestViewContainerMap* guest_view_containers = | 64 WebViewContainerMap* guest_view_containers = |
| 73 g_guest_view_container_map.Pointer(); | 65 g_guest_view_container_map.Pointer(); |
| 74 GuestViewContainerMap::iterator it = guest_view_containers->find( | 66 WebViewContainerMap::iterator it = guest_view_containers->find( |
| 75 GuestViewID(render_view_routing_id, element_instance_id)); | 67 GuestViewID(render_view_routing_id, element_instance_id)); |
| 76 return it == guest_view_containers->end() ? NULL : it->second; | 68 return it == guest_view_containers->end() ? NULL : it->second; |
| 77 } | 69 } |
| 78 | 70 |
| 79 void GuestViewContainer::AttachGuest(linked_ptr<AttachRequest> request) { | 71 void WebViewContainer::AttachGuest(linked_ptr<AttachRequest> request) { |
| 80 EnqueueAttachRequest(request); | 72 EnqueueAttachRequest(request); |
| 81 PerformPendingAttachRequest(); | 73 PerformPendingAttachRequest(); |
| 82 } | 74 } |
| 83 | 75 |
| 84 void GuestViewContainer::SetElementInstanceID(int element_instance_id) { | 76 void WebViewContainer::SetElementInstanceID(int element_instance_id) { |
| 77 GuestViewContainer::SetElementInstanceID(element_instance_id); |
| 78 |
| 85 GuestViewID guest_view_id(render_view_routing_id_, element_instance_id); | 79 GuestViewID guest_view_id(render_view_routing_id_, element_instance_id); |
| 86 DCHECK_EQ(element_instance_id_, guestview::kInstanceIDNone); | |
| 87 DCHECK(g_guest_view_container_map.Get().find(guest_view_id) == | 80 DCHECK(g_guest_view_container_map.Get().find(guest_view_id) == |
| 88 g_guest_view_container_map.Get().end()); | 81 g_guest_view_container_map.Get().end()); |
| 89 element_instance_id_ = element_instance_id; | |
| 90 g_guest_view_container_map.Get().insert(std::make_pair(guest_view_id, this)); | 82 g_guest_view_container_map.Get().insert(std::make_pair(guest_view_id, this)); |
| 91 } | 83 } |
| 92 | 84 |
| 93 void GuestViewContainer::DidFinishLoading() { | 85 void WebViewContainer::Ready() { |
| 94 if (mime_type_.empty()) | |
| 95 return; | |
| 96 | |
| 97 DCHECK_NE(element_instance_id_, guestview::kInstanceIDNone); | |
| 98 render_frame()->Send(new ExtensionHostMsg_CreateMimeHandlerViewGuest( | |
| 99 routing_id(), html_string_, mime_type_, element_instance_id_)); | |
| 100 } | |
| 101 | |
| 102 void GuestViewContainer::DidReceiveData(const char* data, int data_length) { | |
| 103 std::string value(data, data_length); | |
| 104 html_string_ += value; | |
| 105 } | |
| 106 | |
| 107 void GuestViewContainer::Ready() { | |
| 108 ready_ = true; | 86 ready_ = true; |
| 109 CHECK(!pending_response_.get()); | 87 CHECK(!pending_response_.get()); |
| 110 PerformPendingAttachRequest(); | 88 PerformPendingAttachRequest(); |
| 111 } | 89 } |
| 112 | 90 |
| 113 void GuestViewContainer::OnDestruct() { | 91 bool WebViewContainer::HandlesMessage(const IPC::Message& message) { |
| 114 // GuestViewContainer's lifetime is managed by BrowserPlugin so don't let | 92 return message.type() == ExtensionMsg_GuestAttached::ID; |
| 115 // RenderFrameObserver self-destruct here. | |
| 116 } | 93 } |
| 117 | 94 |
| 118 bool GuestViewContainer::OnMessageReceived(const IPC::Message& message) { | 95 bool WebViewContainer::OnMessage(const IPC::Message& message) { |
| 119 if (!ShouldHandleMessage(message)) | 96 bool handled = false; |
| 120 return false; | 97 IPC_BEGIN_MESSAGE_MAP(WebViewContainer, message) |
| 121 | |
| 122 DCHECK_NE(element_instance_id_, guestview::kInstanceIDNone); | |
| 123 int element_instance_id = guestview::kInstanceIDNone; | |
| 124 PickleIterator iter(message); | |
| 125 bool success = iter.ReadInt(&element_instance_id); | |
| 126 DCHECK(success); | |
| 127 if (element_instance_id != element_instance_id_) | |
| 128 return false; | |
| 129 | |
| 130 bool handled = true; | |
| 131 IPC_BEGIN_MESSAGE_MAP(GuestViewContainer, message) | |
| 132 IPC_MESSAGE_HANDLER(ExtensionMsg_CreateMimeHandlerViewGuestACK, | |
| 133 OnCreateMimeHandlerViewGuestACK) | |
| 134 IPC_MESSAGE_HANDLER(ExtensionMsg_GuestAttached, OnGuestAttached) | 98 IPC_MESSAGE_HANDLER(ExtensionMsg_GuestAttached, OnGuestAttached) |
| 135 IPC_MESSAGE_UNHANDLED(handled = false) | 99 IPC_MESSAGE_UNHANDLED(handled = false) |
| 136 IPC_END_MESSAGE_MAP() | 100 IPC_END_MESSAGE_MAP() |
| 137 return handled; | 101 return handled; |
| 138 } | 102 } |
| 139 | 103 |
| 140 void GuestViewContainer::OnCreateMimeHandlerViewGuestACK( | 104 void WebViewContainer::OnGuestAttached(int guest_proxy_routing_id) { |
| 141 int element_instance_id) { | |
| 142 DCHECK_NE(element_instance_id_, guestview::kInstanceIDNone); | |
| 143 DCHECK_EQ(element_instance_id_, element_instance_id); | |
| 144 DCHECK(!mime_type_.empty()); | |
| 145 render_frame()->AttachGuest(element_instance_id); | |
| 146 } | |
| 147 | |
| 148 void GuestViewContainer::OnGuestAttached(int element_instance_id, | |
| 149 int guest_proxy_routing_id) { | |
| 150 attached_ = true; | |
| 151 | |
| 152 if (!mime_type_.empty()) { | |
| 153 // MimeHandlerView's creation and attachment is not done via JS API. | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 // Handle the callback for the current request with a pending response. | 105 // Handle the callback for the current request with a pending response. |
| 158 HandlePendingResponseCallback(guest_proxy_routing_id); | 106 HandlePendingResponseCallback(guest_proxy_routing_id); |
| 159 // Perform the subsequent attach request if one exists. | 107 // Perform the subsequent attach request if one exists. |
| 160 PerformPendingAttachRequest(); | 108 PerformPendingAttachRequest(); |
| 161 } | 109 } |
| 162 | 110 |
| 163 void GuestViewContainer::AttachGuestInternal( | 111 void WebViewContainer::AttachGuestInternal( |
| 164 linked_ptr<AttachRequest> request) { | 112 linked_ptr<AttachRequest> request) { |
| 165 CHECK(!pending_response_.get()); | 113 CHECK(!pending_response_.get()); |
| 166 // Step 1, send the attach params to chrome/. | 114 // Step 1, send the attach params to chrome/. |
| 167 render_frame()->Send( | 115 render_frame()->Send( |
| 168 new ExtensionHostMsg_AttachGuest(render_view_routing_id_, | 116 new ExtensionHostMsg_AttachGuest(render_view_routing_id_, |
| 169 request->element_instance_id(), | 117 request->element_instance_id(), |
| 170 request->guest_instance_id(), | 118 request->guest_instance_id(), |
| 171 *request->attach_params())); | 119 *request->attach_params())); |
| 172 | 120 |
| 173 // Step 2, attach plugin through content/. | 121 // Step 2, attach plugin through content/. |
| 174 render_frame()->AttachGuest(request->element_instance_id()); | 122 render_frame()->AttachGuest(request->element_instance_id()); |
| 175 | 123 |
| 176 pending_response_ = request; | 124 pending_response_ = request; |
| 177 } | 125 } |
| 178 | 126 |
| 179 void GuestViewContainer::EnqueueAttachRequest( | 127 void WebViewContainer::EnqueueAttachRequest( |
| 180 linked_ptr<AttachRequest> request) { | 128 linked_ptr<AttachRequest> request) { |
| 181 pending_requests_.push_back(request); | 129 pending_requests_.push_back(request); |
| 182 } | 130 } |
| 183 | 131 |
| 184 void GuestViewContainer::PerformPendingAttachRequest() { | 132 void WebViewContainer::PerformPendingAttachRequest() { |
| 185 if (!ready_ || pending_requests_.empty() || pending_response_.get()) | 133 if (!ready_ || pending_requests_.empty() || pending_response_.get()) |
| 186 return; | 134 return; |
| 187 | 135 |
| 188 linked_ptr<AttachRequest> pending_request = pending_requests_.front(); | 136 linked_ptr<AttachRequest> pending_request = pending_requests_.front(); |
| 189 pending_requests_.pop_front(); | 137 pending_requests_.pop_front(); |
| 190 AttachGuestInternal(pending_request); | 138 AttachGuestInternal(pending_request); |
| 191 } | 139 } |
| 192 | 140 |
| 193 void GuestViewContainer::HandlePendingResponseCallback( | 141 void WebViewContainer::HandlePendingResponseCallback( |
| 194 int guest_proxy_routing_id) { | 142 int guest_proxy_routing_id) { |
| 195 CHECK(pending_response_.get()); | 143 CHECK(pending_response_.get()); |
| 196 linked_ptr<AttachRequest> pending_response(pending_response_.release()); | 144 linked_ptr<AttachRequest> pending_response(pending_response_.release()); |
| 197 | 145 |
| 198 // If we don't have a callback then there's nothing more to do. | 146 // If we don't have a callback then there's nothing more to do. |
| 199 if (!pending_response->HasCallback()) | 147 if (!pending_response->HasCallback()) |
| 200 return; | 148 return; |
| 201 | 149 |
| 202 content::RenderView* guest_proxy_render_view = | 150 content::RenderView* guest_proxy_render_view = |
| 203 content::RenderView::FromRoutingID(guest_proxy_routing_id); | 151 content::RenderView::FromRoutingID(guest_proxy_routing_id); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 218 v8::Handle<v8::Value> argv[argc] = { window }; | 166 v8::Handle<v8::Value> argv[argc] = { window }; |
| 219 | 167 |
| 220 v8::Context::Scope context_scope(context); | 168 v8::Context::Scope context_scope(context); |
| 221 blink::WebScopedMicrotaskSuppression suppression; | 169 blink::WebScopedMicrotaskSuppression suppression; |
| 222 | 170 |
| 223 // Call the AttachGuest API's callback with the guest proxy as the first | 171 // Call the AttachGuest API's callback with the guest proxy as the first |
| 224 // parameter. | 172 // parameter. |
| 225 callback->Call(context->Global(), argc, argv); | 173 callback->Call(context->Global(), argc, argv); |
| 226 } | 174 } |
| 227 | 175 |
| 228 // static | |
| 229 bool GuestViewContainer::ShouldHandleMessage(const IPC::Message& message) { | |
| 230 switch (message.type()) { | |
| 231 case ExtensionMsg_CreateMimeHandlerViewGuestACK::ID: | |
| 232 case ExtensionMsg_GuestAttached::ID: | |
| 233 return true; | |
| 234 default: | |
| 235 break; | |
| 236 } | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 240 } // namespace extensions | 176 } // namespace extensions |
| OLD | NEW |