ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_node.cc
blob845aecea62afc5945311f283f029c8d8b6504682
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/stl_util.h"
11 #include "content/browser/frame_host/frame_tree.h"
12 #include "content/browser/frame_host/navigator.h"
13 #include "content/browser/frame_host/render_frame_host_impl.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/public/common/content_switches.h"
17 namespace content {
19 int64 FrameTreeNode::next_frame_tree_node_id_ = 1;
21 FrameTreeNode::FrameTreeNode(FrameTree* frame_tree,
22 Navigator* navigator,
23 RenderFrameHostDelegate* render_frame_delegate,
24 RenderViewHostDelegate* render_view_delegate,
25 RenderWidgetHostDelegate* render_widget_delegate,
26 RenderFrameHostManager::Delegate* manager_delegate,
27 const std::string& name)
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_++),
36 parent_(NULL),
37 replication_state_(name),
38 is_loading_(false) {
41 FrameTreeNode::~FrameTreeNode() {
42 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
43 switches::kEnableBrowserSideNavigation)) {
44 navigator_->CancelNavigation(this);
48 bool FrameTreeNode::IsMainFrame() const {
49 return frame_tree_->root() == this;
52 void FrameTreeNode::AddChild(scoped_ptr<FrameTreeNode> child,
53 int process_id,
54 int frame_routing_id) {
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());
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);
66 child->set_parent(this);
67 children_.push_back(child.release());
70 void FrameTreeNode::RemoveChild(FrameTreeNode* child) {
71 std::vector<FrameTreeNode*>::iterator iter;
73 for (iter = children_.begin(); iter != children_.end(); ++iter) {
74 if ((*iter) == child)
75 break;
78 if (iter != children_.end()) {
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();
88 void FrameTreeNode::ResetForNewProcess() {
89 current_url_ = GURL();
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
93 // nodes get deleted before swapping to a new process.
94 ScopedVector<FrameTreeNode> old_children = children_.Pass();
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);
103 old_children.clear(); // May notify observers.
106 bool FrameTreeNode::IsDescendantOf(FrameTreeNode* other) const {
107 if (!other || !other->child_count())
108 return false;
110 for (FrameTreeNode* node = parent(); node; node = node->parent()) {
111 if (node == other)
112 return true;
115 return false;
118 } // namespace content