Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_node.cc
blobb87af213161184808696980c19bdb66a0978fe38
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.
5 #include "content/browser/frame_host/frame_tree_node.h"
7 #include <queue>
9 #include "base/command_line.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/stl_util.h"
12 #include "content/browser/frame_host/frame_tree.h"
13 #include "content/browser/frame_host/navigation_request.h"
14 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/render_frame_host_impl.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/common/content_switches.h"
20 namespace content {
22 namespace {
24 // This is a global map between frame_tree_node_ids and pointers to
25 // FrameTreeNodes.
26 typedef base::hash_map<int, FrameTreeNode*> FrameTreeNodeIDMap;
28 base::LazyInstance<FrameTreeNodeIDMap> g_frame_tree_node_id_map =
29 LAZY_INSTANCE_INITIALIZER;
31 // These values indicate the loading progress status. The minimum progress
32 // value matches what Blink's ProgressTracker has traditionally used for a
33 // minimum progress value.
34 const double kLoadingProgressNotStarted = 0.0;
35 const double kLoadingProgressMinimum = 0.1;
36 const double kLoadingProgressDone = 1.0;
38 } // namespace
40 int FrameTreeNode::next_frame_tree_node_id_ = 1;
42 // static
43 FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 FrameTreeNodeIDMap* nodes = g_frame_tree_node_id_map.Pointer();
46 FrameTreeNodeIDMap::iterator it = nodes->find(frame_tree_node_id);
47 return it == nodes->end() ? nullptr : it->second;
50 FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
51 Navigator* navigator,
52 RenderFrameHostDelegate* render_frame_delegate,
53 RenderViewHostDelegate* render_view_delegate,
54 RenderWidgetHostDelegate* render_widget_delegate,
55 RenderFrameHostManager::Delegate* manager_delegate,
56 const std::string& name,
57 SandboxFlags sandbox_flags)
58 : frame_tree_(frame_tree),
59 navigator_(navigator),
60 render_manager_(this,
61 render_frame_delegate,
62 render_view_delegate,
63 render_widget_delegate,
64 manager_delegate),
65 frame_tree_node_id_(next_frame_tree_node_id_++),
66 parent_(NULL),
67 replication_state_(name, sandbox_flags),
68 // Effective sandbox flags also need to be set, since initial sandbox
69 // flags should apply to the initial empty document in the frame.
70 effective_sandbox_flags_(sandbox_flags),
71 loading_progress_(kLoadingProgressNotStarted) {
72 std::pair<FrameTreeNodeIDMap::iterator, bool> result =
73 g_frame_tree_node_id_map.Get().insert(
74 std::make_pair(frame_tree_node_id_, this));
75 CHECK(result.second);
78 FrameTreeNode::~FrameTreeNode() {
79 frame_tree_->FrameRemoved(this);
81 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
84 bool FrameTreeNode::IsMainFrame() const {
85 return frame_tree_->root() == this;
88 void FrameTreeNode::AddChild(scoped_ptr<FrameTreeNode> child,
89 int process_id,
90 int frame_routing_id) {
91 // Child frame must always be created in the same process as the parent.
92 CHECK_EQ(process_id, render_manager_.current_host()->GetProcess()->GetID());
94 // Initialize the RenderFrameHost for the new node. We always create child
95 // frames in the same SiteInstance as the current frame, and they can swap to
96 // a different one if they navigate away.
97 child->render_manager()->Init(
98 render_manager_.current_host()->GetSiteInstance()->GetBrowserContext(),
99 render_manager_.current_host()->GetSiteInstance(),
100 render_manager_.current_host()->GetRoutingID(),
101 frame_routing_id);
102 child->set_parent(this);
104 // Other renderer processes in this BrowsingInstance may need to find out
105 // about the new frame. Create a proxy for the child frame in all
106 // SiteInstances that have a proxy for the frame's parent, since all frames
107 // in a frame tree should have the same set of proxies.
108 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
109 switches::kSitePerProcess))
110 render_manager_.CreateProxiesForChildFrame(child.get());
112 children_.push_back(child.release());
115 void FrameTreeNode::RemoveChild(FrameTreeNode* child) {
116 std::vector<FrameTreeNode*>::iterator iter;
117 for (iter = children_.begin(); iter != children_.end(); ++iter) {
118 if ((*iter) == child)
119 break;
122 if (iter != children_.end()) {
123 // Subtle: we need to make sure the node is gone from the tree before
124 // observers are notified of its deletion.
125 scoped_ptr<FrameTreeNode> node_to_delete(*iter);
126 children_.weak_erase(iter);
127 node_to_delete.reset();
131 void FrameTreeNode::ResetForNewProcess() {
132 current_url_ = GURL();
134 // The children may not have been cleared if a cross-process navigation
135 // commits before the old process cleans everything up. Make sure the child
136 // nodes get deleted before swapping to a new process.
137 ScopedVector<FrameTreeNode> old_children = children_.Pass();
138 old_children.clear(); // May notify observers.
141 void FrameTreeNode::SetCurrentOrigin(const url::Origin& origin) {
142 if (!origin.IsSameAs(replication_state_.origin))
143 render_manager_.OnDidUpdateOrigin(origin);
144 replication_state_.origin = origin;
147 void FrameTreeNode::SetFrameName(const std::string& name) {
148 if (name != replication_state_.name)
149 render_manager_.OnDidUpdateName(name);
150 replication_state_.name = name;
153 bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
154 if (!other || !other->child_count())
155 return false;
157 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
158 if (node == other)
159 return true;
162 return false;
165 FrameTreeNode* FrameTreeNode::PreviousSibling() const {
166 if (!parent_)
167 return nullptr;
169 for (size_t i = 0; i < parent_->child_count(); ++i) {
170 if (parent_->child_at(i) == this)
171 return (i == 0) ? nullptr : parent_->child_at(i - 1);
174 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
175 return nullptr;
178 bool FrameTreeNode::IsLoading() const {
179 RenderFrameHostImpl* current_frame_host =
180 render_manager_.current_frame_host();
181 RenderFrameHostImpl* pending_frame_host =
182 render_manager_.pending_frame_host();
184 DCHECK(current_frame_host);
186 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
187 switches::kEnableBrowserSideNavigation)) {
188 if (navigation_request_)
189 return true;
190 } else {
191 if (pending_frame_host && pending_frame_host->is_loading())
192 return true;
194 return current_frame_host->is_loading();
197 bool FrameTreeNode::CommitPendingSandboxFlags() {
198 bool did_change_flags =
199 effective_sandbox_flags_ != replication_state_.sandbox_flags;
200 effective_sandbox_flags_ = replication_state_.sandbox_flags;
201 return did_change_flags;
204 void FrameTreeNode::SetNavigationRequest(
205 scoped_ptr<NavigationRequest> navigation_request) {
206 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
207 switches::kEnableBrowserSideNavigation));
208 ResetNavigationRequest(false);
210 // Force the throbber to start to keep it in sync with what is happening in
211 // the UI. Blink doesn't send throb notifications for JavaScript URLs, so it
212 // is not done here either.
213 if (!navigation_request->common_params().url.SchemeIs(
214 url::kJavaScriptScheme)) {
215 // TODO(fdegans): Check if this is a same-document navigation and set the
216 // proper argument.
217 DidStartLoading(true);
220 navigation_request_ = navigation_request.Pass();
223 void FrameTreeNode::ResetNavigationRequest(bool is_commit) {
224 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
225 switches::kEnableBrowserSideNavigation));
226 if (!navigation_request_)
227 return;
228 navigation_request_.reset();
230 // During commit, the clean up of a speculative RenderFrameHost is done in
231 // RenderFrameHostManager::DidNavigateFrame. The load is also still being
232 // tracked.
233 if (is_commit)
234 return;
236 // If the reset corresponds to a cancelation, the RenderFrameHostManager
237 // should clean up any speculative RenderFrameHost it created for the
238 // navigation.
239 DidStopLoading();
240 render_manager_.CleanUpNavigation();
243 bool FrameTreeNode::has_started_loading() const {
244 return loading_progress_ != kLoadingProgressNotStarted;
247 void FrameTreeNode::reset_loading_progress() {
248 loading_progress_ = kLoadingProgressNotStarted;
251 void FrameTreeNode::DidStartLoading(bool to_different_document) {
252 // Any main frame load to a new document should reset the load progress since
253 // it will replace the current page and any frames. The WebContents will
254 // be notified when DidChangeLoadProgress is called.
255 if (to_different_document && IsMainFrame())
256 frame_tree_->ResetLoadProgress();
258 // Notify the WebContents.
259 if (!frame_tree_->IsLoading())
260 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
262 // Set initial load progress and update overall progress. This will notify
263 // the WebContents of the load progress change.
264 DidChangeLoadProgress(kLoadingProgressMinimum);
266 // Notify the RenderFrameHostManager of the event.
267 render_manager()->OnDidStartLoading();
270 void FrameTreeNode::DidStopLoading() {
271 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
272 tracked_objects::ScopedTracker tracking_profile1(
273 FROM_HERE_WITH_EXPLICIT_FUNCTION(
274 "465796 FrameTreeNode::DidStopLoading::Start"));
276 // Set final load progress and update overall progress. This will notify
277 // the WebContents of the load progress change.
278 DidChangeLoadProgress(kLoadingProgressDone);
280 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
281 tracked_objects::ScopedTracker tracking_profile2(
282 FROM_HERE_WITH_EXPLICIT_FUNCTION(
283 "465796 FrameTreeNode::DidStopLoading::WCIDidStopLoading"));
285 // Notify the WebContents.
286 if (!frame_tree_->IsLoading())
287 navigator()->GetDelegate()->DidStopLoading();
289 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
290 tracked_objects::ScopedTracker tracking_profile3(
291 FROM_HERE_WITH_EXPLICIT_FUNCTION(
292 "465796 FrameTreeNode::DidStopLoading::RFHMDidStopLoading"));
294 // Notify the RenderFrameHostManager of the event.
295 render_manager()->OnDidStopLoading();
297 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
298 tracked_objects::ScopedTracker tracking_profile4(
299 FROM_HERE_WITH_EXPLICIT_FUNCTION(
300 "465796 FrameTreeNode::DidStopLoading::End"));
303 void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
304 loading_progress_ = load_progress;
305 frame_tree_->UpdateLoadProgress();
308 } // namespace content