| 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 "components/guest_view/renderer/guest_view_container.h" | 5 #include "components/guest_view/renderer/guest_view_container.h" |
| 6 | 6 |
| 7 #include "components/guest_view/common/guest_view_constants.h" | 7 #include "components/guest_view/common/guest_view_constants.h" |
| 8 #include "components/guest_view/common/guest_view_messages.h" | 8 #include "components/guest_view/common/guest_view_messages.h" |
| 9 #include "components/guest_view/renderer/guest_view_request.h" | 9 #include "components/guest_view/renderer/guest_view_request.h" |
| 10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 : content::RenderFrameObserver(render_frame), | 42 : content::RenderFrameObserver(render_frame), |
| 43 container_(container) {} | 43 container_(container) {} |
| 44 | 44 |
| 45 void GuestViewContainer::RenderFrameLifetimeObserver::OnDestruct() { | 45 void GuestViewContainer::RenderFrameLifetimeObserver::OnDestruct() { |
| 46 container_->RenderFrameDestroyed(); | 46 container_->RenderFrameDestroyed(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 GuestViewContainer::GuestViewContainer(content::RenderFrame* render_frame) | 49 GuestViewContainer::GuestViewContainer(content::RenderFrame* render_frame) |
| 50 : element_instance_id_(guest_view::kInstanceIDNone), | 50 : element_instance_id_(guest_view::kInstanceIDNone), |
| 51 render_frame_(render_frame), | 51 render_frame_(render_frame), |
| 52 ready_(false) { | 52 ready_(false), |
| 53 in_destruction_(false) { |
| 53 render_frame_lifetime_observer_.reset( | 54 render_frame_lifetime_observer_.reset( |
| 54 new RenderFrameLifetimeObserver(this, render_frame_)); | 55 new RenderFrameLifetimeObserver(this, render_frame_)); |
| 55 } | 56 } |
| 56 | 57 |
| 57 GuestViewContainer::~GuestViewContainer() { | 58 GuestViewContainer::~GuestViewContainer() { |
| 59 // Note: Cleanups should be done in GuestViewContainer::Destroy(), not here. |
| 60 } |
| 61 |
| 62 // static. |
| 63 GuestViewContainer* GuestViewContainer::FromID(int element_instance_id) { |
| 64 GuestViewContainerMap* guest_view_containers = |
| 65 g_guest_view_container_map.Pointer(); |
| 66 auto it = guest_view_containers->find(element_instance_id); |
| 67 return it == guest_view_containers->end() ? nullptr : it->second; |
| 68 } |
| 69 |
| 70 // Right now a GuestViewContainer can be destroyed in one of the following |
| 71 // ways: |
| 72 // |
| 73 // 1. If GuestViewContainer is driven by content/, the element (browser plugin) |
| 74 // can destroy GuestViewContainer when the element is destroyed. |
| 75 // 2. If GuestViewContainer is managed outside of content/, then the |
| 76 // <webview> element's GC will destroy it. |
| 77 // 3. If GuestViewContainer's embedder frame is destroyed, we'd also destroy |
| 78 // GuestViewContainer. |
| 79 void GuestViewContainer::Destroy() { |
| 80 if (in_destruction_) |
| 81 return; |
| 82 |
| 83 in_destruction_ = true; |
| 84 |
| 85 // Give our derived class an opportunity to perform some cleanup prior to |
| 86 // destruction. |
| 87 OnDestroy(); |
| 88 |
| 58 if (element_instance_id() != guest_view::kInstanceIDNone) | 89 if (element_instance_id() != guest_view::kInstanceIDNone) |
| 59 g_guest_view_container_map.Get().erase(element_instance_id()); | 90 g_guest_view_container_map.Get().erase(element_instance_id()); |
| 60 | 91 |
| 61 if (pending_response_.get()) | 92 if (pending_response_.get()) |
| 62 pending_response_->ExecuteCallbackIfAvailable(0 /* argc */, nullptr); | 93 pending_response_->ExecuteCallbackIfAvailable(0 /* argc */, nullptr); |
| 63 | 94 |
| 64 while (pending_requests_.size() > 0) { | 95 while (pending_requests_.size() > 0) { |
| 65 linked_ptr<GuestViewRequest> pending_request = pending_requests_.front(); | 96 linked_ptr<GuestViewRequest> pending_request = pending_requests_.front(); |
| 66 pending_requests_.pop_front(); | 97 pending_requests_.pop_front(); |
| 67 // Call the JavaScript callbacks with no arguments which implies an error. | 98 // Call the JavaScript callbacks with no arguments which implies an error. |
| 68 pending_request->ExecuteCallbackIfAvailable(0 /* argc */, nullptr); | 99 pending_request->ExecuteCallbackIfAvailable(0 /* argc */, nullptr); |
| 69 } | 100 } |
| 70 } | |
| 71 | 101 |
| 72 // static. | 102 delete this; |
| 73 GuestViewContainer* GuestViewContainer::FromID(int element_instance_id) { | |
| 74 GuestViewContainerMap* guest_view_containers = | |
| 75 g_guest_view_container_map.Pointer(); | |
| 76 auto it = guest_view_containers->find(element_instance_id); | |
| 77 return it == guest_view_containers->end() ? nullptr : it->second; | |
| 78 } | 103 } |
| 79 | 104 |
| 80 void GuestViewContainer::RenderFrameDestroyed() { | 105 void GuestViewContainer::RenderFrameDestroyed() { |
| 81 OnRenderFrameDestroyed(); | 106 OnRenderFrameDestroyed(); |
| 82 render_frame_ = nullptr; | 107 render_frame_ = nullptr; |
| 108 Destroy(); |
| 83 } | 109 } |
| 84 | 110 |
| 85 void GuestViewContainer::IssueRequest(linked_ptr<GuestViewRequest> request) { | 111 void GuestViewContainer::IssueRequest(linked_ptr<GuestViewRequest> request) { |
| 86 EnqueueRequest(request); | 112 EnqueueRequest(request); |
| 87 PerformPendingRequest(); | 113 PerformPendingRequest(); |
| 88 } | 114 } |
| 89 | 115 |
| 90 void GuestViewContainer::EnqueueRequest(linked_ptr<GuestViewRequest> request) { | 116 void GuestViewContainer::EnqueueRequest(linked_ptr<GuestViewRequest> request) { |
| 91 pending_requests_.push_back(request); | 117 pending_requests_.push_back(request); |
| 92 } | 118 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 165 |
| 140 void GuestViewContainer::SetElementInstanceID(int element_instance_id) { | 166 void GuestViewContainer::SetElementInstanceID(int element_instance_id) { |
| 141 DCHECK_EQ(element_instance_id_, guest_view::kInstanceIDNone); | 167 DCHECK_EQ(element_instance_id_, guest_view::kInstanceIDNone); |
| 142 element_instance_id_ = element_instance_id; | 168 element_instance_id_ = element_instance_id; |
| 143 | 169 |
| 144 DCHECK(!g_guest_view_container_map.Get().count(element_instance_id)); | 170 DCHECK(!g_guest_view_container_map.Get().count(element_instance_id)); |
| 145 g_guest_view_container_map.Get().insert( | 171 g_guest_view_container_map.Get().insert( |
| 146 std::make_pair(element_instance_id, this)); | 172 std::make_pair(element_instance_id, this)); |
| 147 } | 173 } |
| 148 | 174 |
| 175 void GuestViewContainer::DidDestroyElement() { |
| 176 Destroy(); |
| 177 } |
| 178 |
| 149 } // namespace guest_view | 179 } // namespace guest_view |
| OLD | NEW |