[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 5 | #include "content/browser/frame_host/frame_tree_node.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 6 | |
| 7 | #include <queue> |
| 8 | |
clamy | 71a42ec | 2014-10-02 18:43:22 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 10 | #include "base/stl_util.h" |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 11 | #include "content/browser/frame_host/frame_tree.h" |
[email protected] | 190b8c5 | 2013-11-09 01:35:44 | [diff] [blame] | 12 | #include "content/browser/frame_host/navigator.h" |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 13 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 14 | #include "content/browser/renderer_host/render_view_host_impl.h" |
clamy | 71a42ec | 2014-10-02 18:43:22 | [diff] [blame] | 15 | #include "content/public/common/content_switches.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
[email protected] | 741fd68 | 2013-11-08 08:26:55 | [diff] [blame] | 19 | int64 FrameTreeNode::next_frame_tree_node_id_ = 1; |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 20 | |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 21 | FrameTreeNode::FrameTreeNode(FrameTree* frame_tree, |
| 22 | Navigator* navigator, |
[email protected] | 92404c6 | 2013-12-04 16:40:46 | [diff] [blame] | 23 | RenderFrameHostDelegate* render_frame_delegate, |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 24 | RenderViewHostDelegate* render_view_delegate, |
| 25 | RenderWidgetHostDelegate* render_widget_delegate, |
[email protected] | b0936d2 | 2013-11-28 06:47:36 | [diff] [blame] | 26 | RenderFrameHostManager::Delegate* manager_delegate, |
[email protected] | 5291380 | 2013-12-10 05:52:18 | [diff] [blame] | 27 | const std::string& name) |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 28 | : frame_tree_(frame_tree), |
| 29 | navigator_(navigator), |
| 30 | render_manager_(this, |
| 31 | render_frame_delegate, |
| 32 | render_view_delegate, |
| 33 | render_widget_delegate, |
| 34 | manager_delegate), |
| 35 | frame_tree_node_id_(next_frame_tree_node_id_++), |
nasko | a7064ad6e | 2015-01-15 18:44:56 | [diff] [blame] | 36 | parent_(NULL), |
alexmos | 6b29456 | 2015-03-05 19:24:10 | [diff] [blame^] | 37 | replication_state_(name), |
| 38 | effective_sandbox_flags_(SandboxFlags::NONE) { |
alexmos | 998581d | 2015-01-22 01:01:59 | [diff] [blame] | 39 | } |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 40 | |
| 41 | FrameTreeNode::~FrameTreeNode() { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 42 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 43 | switches::kEnableBrowserSideNavigation)) { |
clamy | 71a42ec | 2014-10-02 18:43:22 | [diff] [blame] | 44 | navigator_->CancelNavigation(this); |
| 45 | } |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 48 | bool FrameTreeNode::IsMainFrame() const { |
| 49 | return frame_tree_->root() == this; |
| 50 | } |
| 51 | |
| 52 | void FrameTreeNode::AddChild(scoped_ptr<FrameTreeNode> child, |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 53 | int process_id, |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 54 | int frame_routing_id) { |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 55 | // Child frame must always be created in the same process as the parent. |
| 56 | CHECK_EQ(process_id, render_manager_.current_host()->GetProcess()->GetID()); |
| 57 | |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 58 | // Initialize the RenderFrameHost for the new node. We always create child |
| 59 | // frames in the same SiteInstance as the current frame, and they can swap to |
| 60 | // a different one if they navigate away. |
| 61 | child->render_manager()->Init( |
| 62 | render_manager_.current_host()->GetSiteInstance()->GetBrowserContext(), |
| 63 | render_manager_.current_host()->GetSiteInstance(), |
| 64 | render_manager_.current_host()->GetRoutingID(), |
| 65 | frame_routing_id); |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 66 | child->set_parent(this); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 67 | children_.push_back(child.release()); |
| 68 | } |
| 69 | |
[email protected] | 741fd68 | 2013-11-08 08:26:55 | [diff] [blame] | 70 | void FrameTreeNode::RemoveChild(FrameTreeNode* child) { |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 71 | std::vector<FrameTreeNode*>::iterator iter; |
| 72 | |
| 73 | for (iter = children_.begin(); iter != children_.end(); ++iter) { |
[email protected] | 741fd68 | 2013-11-08 08:26:55 | [diff] [blame] | 74 | if ((*iter) == child) |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 78 | if (iter != children_.end()) { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 79 | // Subtle: we need to make sure the node is gone from the tree before |
| 80 | // observers are notified of its deletion. |
| 81 | scoped_ptr<FrameTreeNode> node_to_delete(*iter); |
| 82 | children_.weak_erase(iter); |
| 83 | node_to_delete->set_parent(NULL); |
| 84 | node_to_delete.reset(); |
[email protected] | bffc830 | 2014-01-23 20:52:16 | [diff] [blame] | 85 | } |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 81c6c5e | 2014-02-13 20:20:07 | [diff] [blame] | 88 | void FrameTreeNode::ResetForNewProcess() { |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 89 | current_url_ = GURL(); |
[email protected] | 482ce3c | 2013-11-27 18:17:09 | [diff] [blame] | 90 | |
[email protected] | 482ce3c | 2013-11-27 18:17:09 | [diff] [blame] | 91 | // The children may not have been cleared if a cross-process navigation |
| 92 | // commits before the old process cleans everything up. Make sure the child |
[email protected] | 81c6c5e | 2014-02-13 20:20:07 | [diff] [blame] | 93 | // nodes get deleted before swapping to a new process. |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 94 | ScopedVector<FrameTreeNode> old_children = children_.Pass(); |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 95 | |
| 96 | // Loop over all children removing them from the FrameTree. This will ensure |
| 97 | // that nodes are properly removed from the tree and notifications are sent. |
| 98 | // Note: since the |children_| vector is now empty, the calls into RemoveChild |
| 99 | // will be a noop and will not result in repeatedly traversing the list. |
| 100 | for (const auto& child : old_children) |
| 101 | frame_tree_->RemoveFrame(child); |
| 102 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 103 | old_children.clear(); // May notify observers. |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 104 | } |
| 105 | |
mlamouri | a85eb3f | 2015-01-26 17:36:27 | [diff] [blame] | 106 | bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const { |
| 107 | if (!other || !other->child_count()) |
| 108 | return false; |
| 109 | |
| 110 | for (FrameTreeNode* node = parent(); node; node = node->parent()) { |
| 111 | if (node == other) |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
fdegans | bda82dd | 2015-03-05 17:00:08 | [diff] [blame] | 118 | bool FrameTreeNode::IsLoading() const { |
| 119 | RenderFrameHostImpl* current_frame_host = |
| 120 | render_manager_.current_frame_host(); |
| 121 | RenderFrameHostImpl* pending_frame_host = |
| 122 | render_manager_.pending_frame_host(); |
| 123 | |
| 124 | DCHECK(current_frame_host); |
| 125 | // TODO(fdegans): Change the implementation logic for PlzNavigate once |
| 126 | // DidStartLoading and DidStopLoading are properly called. |
| 127 | if (pending_frame_host && pending_frame_host->is_loading()) |
| 128 | return true; |
| 129 | return current_frame_host->is_loading(); |
| 130 | } |
| 131 | |
| 132 | double FrameTreeNode::GetLoadingProgress() const { |
| 133 | RenderFrameHostImpl* current_frame_host = |
| 134 | render_manager_.current_frame_host(); |
| 135 | |
| 136 | DCHECK(current_frame_host); |
| 137 | return current_frame_host->loading_progress(); |
| 138 | } |
| 139 | |
alexmos | 6b29456 | 2015-03-05 19:24:10 | [diff] [blame^] | 140 | bool FrameTreeNode::CommitPendingSandboxFlags() { |
| 141 | bool did_change_flags = |
| 142 | effective_sandbox_flags_ != replication_state_.sandbox_flags; |
| 143 | effective_sandbox_flags_ = replication_state_.sandbox_flags; |
| 144 | return did_change_flags; |
| 145 | } |
| 146 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 147 | } // namespace content |