Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_node.cc
blob1f1b9dca05c10a91cb67cbd8ab49ccc31c831173
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/common/frame_messages.h"
18 #include "content/common/site_isolation_policy.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/common/content_switches.h"
22 namespace content {
24 namespace {
26 // This is a global map between frame_tree_node_ids and pointers to
27 // FrameTreeNodes.
28 typedef base::hash_map<int, FrameTreeNode*> FrameTreeNodeIDMap;
30 base::LazyInstance<FrameTreeNodeIDMap> g_frame_tree_node_id_map =
31 LAZY_INSTANCE_INITIALIZER;
33 // These values indicate the loading progress status. The minimum progress
34 // value matches what Blink's ProgressTracker has traditionally used for a
35 // minimum progress value.
36 const double kLoadingProgressNotStarted = 0.0;
37 const double kLoadingProgressMinimum = 0.1;
38 const double kLoadingProgressDone = 1.0;
40 } // namespace
42 // This observer watches the opener of its owner FrameTreeNode and clears the
43 // owner's opener if the opener is destroyed.
44 class FrameTreeNode::OpenerDestroyedObserver : public FrameTreeNode::Observer {
45 public:
46 OpenerDestroyedObserver(FrameTreeNode* owner) : owner_(owner) {}
48 // FrameTreeNode::Observer
49 void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override {
50 CHECK_EQ(owner_->opener(), node);
51 owner_->SetOpener(nullptr);
54 private:
55 FrameTreeNode* owner_;
57 DISALLOW_COPY_AND_ASSIGN(OpenerDestroyedObserver);
60 int FrameTreeNode::next_frame_tree_node_id_ = 1;
62 // static
63 FrameTreeNode* FrameTreeNode::GloballyFindByID(int frame_tree_node_id) {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 FrameTreeNodeIDMap* nodes = g_frame_tree_node_id_map.Pointer();
66 FrameTreeNodeIDMap::iterator it = nodes->find(frame_tree_node_id);
67 return it == nodes->end() ? nullptr : it->second;
70 FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
71 Navigator* navigator,
72 RenderFrameHostDelegate* render_frame_delegate,
73 RenderViewHostDelegate* render_view_delegate,
74 RenderWidgetHostDelegate* render_widget_delegate,
75 RenderFrameHostManager::Delegate* manager_delegate,
76 blink::WebTreeScopeType scope,
77 const std::string& name,
78 blink::WebSandboxFlags sandbox_flags)
79 : frame_tree_(frame_tree),
80 navigator_(navigator),
81 render_manager_(this,
82 render_frame_delegate,
83 render_view_delegate,
84 render_widget_delegate,
85 manager_delegate),
86 frame_tree_node_id_(next_frame_tree_node_id_++),
87 parent_(NULL),
88 opener_(nullptr),
89 opener_observer_(nullptr),
90 has_committed_real_load_(false),
91 replication_state_(scope, name, sandbox_flags),
92 // Effective sandbox flags also need to be set, since initial sandbox
93 // flags should apply to the initial empty document in the frame.
94 effective_sandbox_flags_(sandbox_flags),
95 loading_progress_(kLoadingProgressNotStarted) {
96 std::pair<FrameTreeNodeIDMap::iterator, bool> result =
97 g_frame_tree_node_id_map.Get().insert(
98 std::make_pair(frame_tree_node_id_, this));
99 CHECK(result.second);
102 FrameTreeNode::~FrameTreeNode() {
103 frame_tree_->FrameRemoved(this);
104 FOR_EACH_OBSERVER(Observer, observers_, OnFrameTreeNodeDestroyed(this));
106 if (opener_)
107 opener_->RemoveObserver(opener_observer_.get());
109 g_frame_tree_node_id_map.Get().erase(frame_tree_node_id_);
112 void FrameTreeNode::AddObserver(Observer* observer) {
113 observers_.AddObserver(observer);
116 void FrameTreeNode::RemoveObserver(Observer* observer) {
117 observers_.RemoveObserver(observer);
120 bool FrameTreeNode::IsMainFrame() const {
121 return frame_tree_->root() == this;
124 void FrameTreeNode::AddChild(scoped_ptr<FrameTreeNode> child,
125 int process_id,
126 int frame_routing_id) {
127 // Child frame must always be created in the same process as the parent.
128 CHECK_EQ(process_id, render_manager_.current_host()->GetProcess()->GetID());
130 // Initialize the RenderFrameHost for the new node. We always create child
131 // frames in the same SiteInstance as the current frame, and they can swap to
132 // a different one if they navigate away.
133 child->render_manager()->Init(
134 render_manager_.current_host()->GetSiteInstance()->GetBrowserContext(),
135 render_manager_.current_host()->GetSiteInstance(),
136 render_manager_.current_host()->GetRoutingID(),
137 frame_routing_id);
138 child->set_parent(this);
140 // Other renderer processes in this BrowsingInstance may need to find out
141 // about the new frame. Create a proxy for the child frame in all
142 // SiteInstances that have a proxy for the frame's parent, since all frames
143 // in a frame tree should have the same set of proxies.
144 // TODO(alexmos, nick): We ought to do this for non-oopif too, for openers.
145 if (SiteIsolationPolicy::AreCrossProcessFramesPossible())
146 render_manager_.CreateProxiesForChildFrame(child.get());
148 children_.push_back(child.release());
151 void FrameTreeNode::RemoveChild(FrameTreeNode* child) {
152 std::vector<FrameTreeNode*>::iterator iter;
153 for (iter = children_.begin(); iter != children_.end(); ++iter) {
154 if ((*iter) == child)
155 break;
158 if (iter != children_.end()) {
159 // Subtle: we need to make sure the node is gone from the tree before
160 // observers are notified of its deletion.
161 scoped_ptr<FrameTreeNode> node_to_delete(*iter);
162 children_.weak_erase(iter);
163 node_to_delete.reset();
167 void FrameTreeNode::ResetForNewProcess() {
168 current_url_ = GURL();
170 // The children may not have been cleared if a cross-process navigation
171 // commits before the old process cleans everything up. Make sure the child
172 // nodes get deleted before swapping to a new process.
173 ScopedVector<FrameTreeNode> old_children = children_.Pass();
174 old_children.clear(); // May notify observers.
177 void FrameTreeNode::SetOpener(FrameTreeNode* opener) {
178 if (opener_) {
179 opener_->RemoveObserver(opener_observer_.get());
180 opener_observer_.reset();
183 opener_ = opener;
185 if (opener_) {
186 if (!opener_observer_)
187 opener_observer_ = make_scoped_ptr(new OpenerDestroyedObserver(this));
188 opener_->AddObserver(opener_observer_.get());
192 void FrameTreeNode::SetCurrentURL(const GURL& url) {
193 if (!has_committed_real_load_ && url != GURL(url::kAboutBlankURL))
194 has_committed_real_load_ = true;
195 current_url_ = url;
198 void FrameTreeNode::SetCurrentOrigin(const url::Origin& origin) {
199 if (!origin.IsSameOriginWith(replication_state_.origin))
200 render_manager_.OnDidUpdateOrigin(origin);
201 replication_state_.origin = origin;
204 void FrameTreeNode::SetFrameName(const std::string& name) {
205 if (name != replication_state_.name)
206 render_manager_.OnDidUpdateName(name);
207 replication_state_.name = name;
210 bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
211 if (!other || !other->child_count())
212 return false;
214 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
215 if (node == other)
216 return true;
219 return false;
222 FrameTreeNode* FrameTreeNode::PreviousSibling() const {
223 if (!parent_)
224 return nullptr;
226 for (size_t i = 0; i < parent_->child_count(); ++i) {
227 if (parent_->child_at(i) == this)
228 return (i == 0) ? nullptr : parent_->child_at(i - 1);
231 NOTREACHED() << "FrameTreeNode not found in its parent's children.";
232 return nullptr;
235 bool FrameTreeNode::IsLoading() const {
236 RenderFrameHostImpl* current_frame_host =
237 render_manager_.current_frame_host();
238 RenderFrameHostImpl* pending_frame_host =
239 render_manager_.pending_frame_host();
241 DCHECK(current_frame_host);
243 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
244 switches::kEnableBrowserSideNavigation)) {
245 if (navigation_request_)
246 return true;
248 RenderFrameHostImpl* speculative_frame_host =
249 render_manager_.speculative_frame_host();
250 if (speculative_frame_host && speculative_frame_host->is_loading())
251 return true;
252 } else {
253 if (pending_frame_host && pending_frame_host->is_loading())
254 return true;
256 return current_frame_host->is_loading();
259 bool FrameTreeNode::CommitPendingSandboxFlags() {
260 bool did_change_flags =
261 effective_sandbox_flags_ != replication_state_.sandbox_flags;
262 effective_sandbox_flags_ = replication_state_.sandbox_flags;
263 return did_change_flags;
266 void FrameTreeNode::CreatedNavigationRequest(
267 scoped_ptr<NavigationRequest> navigation_request) {
268 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
269 switches::kEnableBrowserSideNavigation));
270 ResetNavigationRequest(false);
272 // Force the throbber to start to keep it in sync with what is happening in
273 // the UI. Blink doesn't send throb notifications for JavaScript URLs, so it
274 // is not done here either.
275 if (!navigation_request->common_params().url.SchemeIs(
276 url::kJavaScriptScheme)) {
277 // TODO(fdegans): Check if this is a same-document navigation and set the
278 // proper argument.
279 DidStartLoading(true);
282 navigation_request_ = navigation_request.Pass();
284 render_manager()->DidCreateNavigationRequest(*navigation_request_);
287 void FrameTreeNode::ResetNavigationRequest(bool is_commit) {
288 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
289 switches::kEnableBrowserSideNavigation));
290 if (!navigation_request_)
291 return;
292 navigation_request_.reset();
294 // During commit, the clean up of a speculative RenderFrameHost is done in
295 // RenderFrameHostManager::DidNavigateFrame. The load is also still being
296 // tracked.
297 if (is_commit)
298 return;
300 // If the reset corresponds to a cancelation, the RenderFrameHostManager
301 // should clean up any speculative RenderFrameHost it created for the
302 // navigation.
303 DidStopLoading();
304 render_manager_.CleanUpNavigation();
307 bool FrameTreeNode::has_started_loading() const {
308 return loading_progress_ != kLoadingProgressNotStarted;
311 void FrameTreeNode::reset_loading_progress() {
312 loading_progress_ = kLoadingProgressNotStarted;
315 void FrameTreeNode::DidStartLoading(bool to_different_document) {
316 // Any main frame load to a new document should reset the load progress since
317 // it will replace the current page and any frames. The WebContents will
318 // be notified when DidChangeLoadProgress is called.
319 if (to_different_document && IsMainFrame())
320 frame_tree_->ResetLoadProgress();
322 // Notify the WebContents.
323 if (!frame_tree_->IsLoading())
324 navigator()->GetDelegate()->DidStartLoading(this, to_different_document);
326 // Set initial load progress and update overall progress. This will notify
327 // the WebContents of the load progress change.
328 DidChangeLoadProgress(kLoadingProgressMinimum);
330 // Notify the RenderFrameHostManager of the event.
331 render_manager()->OnDidStartLoading();
334 void FrameTreeNode::DidStopLoading() {
335 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
336 tracked_objects::ScopedTracker tracking_profile1(
337 FROM_HERE_WITH_EXPLICIT_FUNCTION(
338 "465796 FrameTreeNode::DidStopLoading::Start"));
340 // Set final load progress and update overall progress. This will notify
341 // the WebContents of the load progress change.
342 DidChangeLoadProgress(kLoadingProgressDone);
344 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
345 tracked_objects::ScopedTracker tracking_profile2(
346 FROM_HERE_WITH_EXPLICIT_FUNCTION(
347 "465796 FrameTreeNode::DidStopLoading::WCIDidStopLoading"));
349 // Notify the WebContents.
350 if (!frame_tree_->IsLoading())
351 navigator()->GetDelegate()->DidStopLoading();
353 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
354 tracked_objects::ScopedTracker tracking_profile3(
355 FROM_HERE_WITH_EXPLICIT_FUNCTION(
356 "465796 FrameTreeNode::DidStopLoading::RFHMDidStopLoading"));
358 // Notify the RenderFrameHostManager of the event.
359 render_manager()->OnDidStopLoading();
361 // TODO(erikchen): Remove ScopedTracker below once crbug.com/465796 is fixed.
362 tracked_objects::ScopedTracker tracking_profile4(
363 FROM_HERE_WITH_EXPLICIT_FUNCTION(
364 "465796 FrameTreeNode::DidStopLoading::End"));
367 void FrameTreeNode::DidChangeLoadProgress(double load_progress) {
368 loading_progress_ = load_progress;
369 frame_tree_->UpdateLoadProgress();
372 bool FrameTreeNode::StopLoading() {
373 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
374 switches::kEnableBrowserSideNavigation)) {
375 ResetNavigationRequest(false);
378 // TODO(nasko): see if child frames should send IPCs in site-per-process
379 // mode.
380 if (!IsMainFrame())
381 return true;
383 render_manager_.Stop();
384 return true;
387 } // namespace content