[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 7 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
| 8 | * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. |
| 9 | * (http://www.torchmobile.com/) |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 21 | * its contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 25 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 28 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | #include "content/renderer/history_entry.h" |
| 37 | |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | |
| 40 | #include "base/memory/ptr_util.h" |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 41 | #include "content/renderer/render_frame_impl.h" |
| 42 | #include "content/renderer/render_view_impl.h" |
mlamouri | 862a2ed | 2014-09-10 15:14:54 | [diff] [blame] | 43 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 44 | |
| 45 | using blink::WebFrame; |
| 46 | using blink::WebHistoryItem; |
| 47 | |
| 48 | namespace content { |
| 49 | |
| 50 | HistoryEntry::HistoryNode* HistoryEntry::HistoryNode::AddChild( |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 51 | const WebHistoryItem& item) { |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 52 | children_.push_back(base::MakeUnique<HistoryNode>(entry_, item)); |
| 53 | return children_.back().get(); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 56 | HistoryEntry::HistoryNode* HistoryEntry::HistoryNode::AddChild() { |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 57 | return AddChild(WebHistoryItem()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 58 | } |
| 59 | |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 60 | std::unique_ptr<HistoryEntry::HistoryNode> |
| 61 | HistoryEntry::HistoryNode::CloneAndReplace( |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 62 | const base::WeakPtr<HistoryEntry>& new_entry, |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 63 | const WebHistoryItem& new_item, |
| 64 | bool clone_children_of_target, |
| 65 | RenderFrameImpl* target_frame, |
| 66 | RenderFrameImpl* current_frame) { |
| 67 | bool is_target_frame = target_frame == current_frame; |
| 68 | const WebHistoryItem& item_for_create = is_target_frame ? new_item : item_; |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 69 | auto new_history_node = |
| 70 | base::MakeUnique<HistoryNode>(new_entry, item_for_create); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 71 | |
creis | 225a743 | 2016-06-03 22:56:27 | [diff] [blame] | 72 | // Use the last committed history item for the frame rather than item_, since |
| 73 | // the latter may not accurately reflect which URL is currently committed in |
| 74 | // the frame. See https://crbug.com/612713#c12. |
| 75 | const WebHistoryItem& current_item = current_frame->current_history_item(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 76 | if (is_target_frame && clone_children_of_target && !current_item.IsNull()) { |
creis | 6f4792bc | 2016-06-23 17:05:21 | [diff] [blame] | 77 | // TODO(creis): Setting the document sequence number here appears to be |
| 78 | // unnecessary. Remove this block if this DCHECK never fires. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 79 | DCHECK_EQ(current_item.DocumentSequenceNumber(), |
| 80 | new_history_node->item().DocumentSequenceNumber()); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 81 | } |
| 82 | |
nasko | 4c0feb6 | 2015-06-05 18:37:06 | [diff] [blame] | 83 | // TODO(creis): This needs to be updated to handle HistoryEntry in |
| 84 | // subframe processes, where the main frame isn't guaranteed to be in the |
| 85 | // same process. |
| 86 | if (current_frame && (clone_children_of_target || !is_target_frame)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 87 | for (WebFrame* child = current_frame->GetWebFrame()->FirstChild(); child; |
| 88 | child = child->NextSibling()) { |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 89 | RenderFrameImpl* child_render_frame = |
| 90 | RenderFrameImpl::FromWebFrame(child); |
creis | 0040d34 | 2015-02-19 01:42:37 | [diff] [blame] | 91 | // TODO(creis): A child frame may be a RenderFrameProxy. We should still |
| 92 | // process its children, but that will be possible when we move this code |
| 93 | // to the browser process in https://crbug.com/236848. |
| 94 | if (!child_render_frame) |
| 95 | continue; |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 96 | HistoryNode* child_history_node = |
| 97 | entry_->GetHistoryNodeForFrame(child_render_frame); |
| 98 | if (!child_history_node) |
| 99 | continue; |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 100 | |
| 101 | new_history_node->children_.push_back(child_history_node->CloneAndReplace( |
| 102 | new_entry, new_item, clone_children_of_target, target_frame, |
| 103 | child_render_frame)); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | return new_history_node; |
| 107 | } |
| 108 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 109 | void HistoryEntry::HistoryNode::set_item(const WebHistoryItem& item) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 110 | DCHECK(!item.IsNull()); |
| 111 | entry_->unique_names_to_items_[item.Target().Utf8()] = this; |
| 112 | unique_names_.push_back(item.Target().Utf8()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 113 | item_ = item; |
| 114 | } |
| 115 | |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 116 | HistoryEntry::HistoryNode::HistoryNode(const base::WeakPtr<HistoryEntry>& entry, |
| 117 | const WebHistoryItem& item) |
| 118 | : entry_(entry) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 119 | if (!item.IsNull()) |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 120 | set_item(item); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | HistoryEntry::HistoryNode::~HistoryNode() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 124 | if (!entry_ || item_.IsNull()) |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 125 | return; |
| 126 | |
ki.stfu | 7cf10ca | 2015-09-27 08:37:01 | [diff] [blame] | 127 | for (const std::string& name : unique_names_) { |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 128 | if (entry_->unique_names_to_items_[name] == this) |
| 129 | entry_->unique_names_to_items_.erase(name); |
| 130 | } |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 131 | } |
| 132 | |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 133 | std::vector<HistoryEntry::HistoryNode*> HistoryEntry::HistoryNode::children() |
| 134 | const { |
| 135 | std::vector<HistoryEntry::HistoryNode*> children(children_.size()); |
| 136 | std::transform(children_.cbegin(), children_.cend(), children.begin(), |
| 137 | [](const std::unique_ptr<HistoryEntry::HistoryNode>& item) { |
| 138 | return item.get(); |
| 139 | }); |
| 140 | |
| 141 | return children; |
| 142 | } |
| 143 | |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 144 | void HistoryEntry::HistoryNode::RemoveChildren() { |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 145 | children_.clear(); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 146 | } |
| 147 | |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 148 | HistoryEntry::HistoryEntry() : weak_ptr_factory_(this) { |
| 149 | root_.reset( |
| 150 | new HistoryNode(weak_ptr_factory_.GetWeakPtr(), WebHistoryItem())); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | HistoryEntry::~HistoryEntry() { |
| 154 | } |
| 155 | |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 156 | HistoryEntry::HistoryEntry(const WebHistoryItem& root) |
| 157 | : weak_ptr_factory_(this) { |
| 158 | root_.reset(new HistoryNode(weak_ptr_factory_.GetWeakPtr(), root)); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | HistoryEntry* HistoryEntry::CloneAndReplace(const WebHistoryItem& new_item, |
| 162 | bool clone_children_of_target, |
| 163 | RenderFrameImpl* target_frame, |
| 164 | RenderViewImpl* render_view) { |
| 165 | HistoryEntry* new_entry = new HistoryEntry(); |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 166 | new_entry->root_ = |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 167 | root_->CloneAndReplace(new_entry->weak_ptr_factory_.GetWeakPtr(), |
| 168 | new_item, clone_children_of_target, target_frame, |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 169 | render_view->GetMainRenderFrame()); |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 170 | return new_entry; |
| 171 | } |
| 172 | |
| 173 | HistoryEntry::HistoryNode* HistoryEntry::GetHistoryNodeForFrame( |
| 174 | RenderFrameImpl* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 175 | if (!frame->GetWebFrame()->Parent()) |
japhet | 740b2ff | 2015-06-02 00:46:08 | [diff] [blame] | 176 | return root_history_node(); |
Daniel Cheng | 999698bd | 2017-03-22 04:56:37 | [diff] [blame] | 177 | return unique_names_to_items_[frame->unique_name()]; |
[email protected] | 021b67d | 2014-04-22 00:57:27 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | WebHistoryItem HistoryEntry::GetItemForFrame(RenderFrameImpl* frame) { |
| 181 | if (HistoryNode* history_node = GetHistoryNodeForFrame(frame)) |
| 182 | return history_node->item(); |
| 183 | return WebHistoryItem(); |
| 184 | } |
| 185 | |
| 186 | } // namespace content |