summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/history_entry.cc
blob: 7686165f4eea2c131e1cb51a800383429b6277f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/*
 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
 *     (http://www.torchmobile.com/)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "content/renderer/history_entry.h"

#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_view_impl.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"

using blink::WebFrame;
using blink::WebHistoryItem;

namespace content {

// Frame routing ids are not safe to serialize, so instead create a mapping
// from routing ids to frame sequence numbers. The sequence numbers can be
// benignly serialized with limited risk of collision in a different process.
// FrameMap is a singleton per-process.
typedef base::hash_map<uint64_t, uint64_t> FrameMap;
static FrameMap& GetFrameMap() {
  CR_DEFINE_STATIC_LOCAL(FrameMap, routing_ids_to_internal_frame_ids, ());
  return routing_ids_to_internal_frame_ids;
}

HistoryEntry::HistoryNode* HistoryEntry::HistoryNode::AddChild(
    const WebHistoryItem& item,
    int64_t frame_id) {
  children_->push_back(new HistoryNode(entry_, item, frame_id));
  return children_->back();
}

HistoryEntry::HistoryNode* HistoryEntry::HistoryNode::AddChild() {
  return AddChild(WebHistoryItem(), kInvalidFrameRoutingID);
}

HistoryEntry::HistoryNode* HistoryEntry::HistoryNode::CloneAndReplace(
    HistoryEntry* new_entry,
    const WebHistoryItem& new_item,
    bool clone_children_of_target,
    RenderFrameImpl* target_frame,
    RenderFrameImpl* current_frame) {
  bool is_target_frame = target_frame == current_frame;
  const WebHistoryItem& item_for_create = is_target_frame ? new_item : item_;
  HistoryNode* new_history_node = new HistoryNode(
      new_entry, item_for_create, current_frame->GetRoutingID());

  if (is_target_frame && clone_children_of_target && !item_.isNull()) {
    new_history_node->item().setDocumentSequenceNumber(
        item_.documentSequenceNumber());
  }

  if (clone_children_of_target || !is_target_frame) {
    for (WebFrame* child = current_frame->GetWebFrame()->firstChild(); child;
         child = child->nextSibling()) {
      RenderFrameImpl* child_render_frame =
          RenderFrameImpl::FromWebFrame(child);
      // TODO(creis): A child frame may be a RenderFrameProxy.  We should still
      // process its children, but that will be possible when we move this code
      // to the browser process in https://crbug.com/236848.
      if (!child_render_frame)
        continue;
      HistoryNode* child_history_node =
          entry_->GetHistoryNodeForFrame(child_render_frame);
      if (!child_history_node)
        continue;
      HistoryNode* new_child_node =
          child_history_node->CloneAndReplace(new_entry,
                                              new_item,
                                              clone_children_of_target,
                                              target_frame,
                                              child_render_frame);
      new_history_node->children_->push_back(new_child_node);
    }
  }
  return new_history_node;
}

void HistoryEntry::HistoryNode::set_item(const WebHistoryItem& item) {
  // The previous HistoryItem might not have had a target set, or it might be
  // different than the current one.
  entry_->unique_names_to_items_[item.target().utf8()] = this;
  entry_->frames_to_items_[item.frameSequenceNumber()] = this;
  item_ = item;
}

HistoryEntry::HistoryNode::HistoryNode(HistoryEntry* entry,
                                       const WebHistoryItem& item,
                                       int64_t frame_id)
    : entry_(entry), item_(item) {
  if (frame_id != kInvalidFrameRoutingID) {
    // Each history item is given a frame sequence number on creation.
    // If we've already mapped this frame id to a sequence number, standardize
    // this item to that sequence number. Otherwise, map the frame id to this
    // item's existing sequence number.
    if (GetFrameMap()[frame_id] == 0)
        GetFrameMap()[frame_id] = item_.frameSequenceNumber();
    else if (!item_.isNull())
        item_.setFrameSequenceNumber(GetFrameMap()[frame_id]);
    entry_->frames_to_items_[GetFrameMap()[frame_id]] = this;
  }

  if (!item_.isNull())
    entry_->unique_names_to_items_[item_.target().utf8()] = this;
  children_.reset(new ScopedVector<HistoryNode>);
}

HistoryEntry::HistoryNode::~HistoryNode() {
}

void HistoryEntry::HistoryNode::RemoveChildren() {
  // TODO(japhet): This is inefficient. Figure out a cleaner way to ensure
  // this HistoryNode isn't cached anywhere.
  std::vector<uint64_t> frames_to_remove;
  std::vector<std::string> unique_names_to_remove;
  for (size_t i = 0; i < children().size(); i++) {
    children().at(i)->RemoveChildren();

    HistoryEntry::FramesToItems::iterator frames_end =
        entry_->frames_to_items_.end();
    HistoryEntry::UniqueNamesToItems::iterator unique_names_end =
        entry_->unique_names_to_items_.end();
    for (HistoryEntry::FramesToItems::iterator it =
             entry_->frames_to_items_.begin();
         it != frames_end;
         ++it) {
      if (it->second == children().at(i))
        frames_to_remove.push_back(GetFrameMap()[it->first]);
    }
    for (HistoryEntry::UniqueNamesToItems::iterator it =
             entry_->unique_names_to_items_.begin();
         it != unique_names_end;
         ++it) {
      if (it->second == children().at(i))
        unique_names_to_remove.push_back(it->first);
    }
  }
  for (unsigned i = 0; i < frames_to_remove.size(); i++)
    entry_->frames_to_items_.erase(frames_to_remove[i]);
  for (unsigned i = 0; i < unique_names_to_remove.size(); i++)
    entry_->unique_names_to_items_.erase(unique_names_to_remove[i]);
  children_.reset(new ScopedVector<HistoryNode>);
}

HistoryEntry::HistoryEntry() {
  root_.reset(new HistoryNode(this, WebHistoryItem(), kInvalidFrameRoutingID));
}

HistoryEntry::~HistoryEntry() {
}

HistoryEntry::HistoryEntry(const WebHistoryItem& root, int64_t frame_id) {
  root_.reset(new HistoryNode(this, root, frame_id));
}

HistoryEntry* HistoryEntry::CloneAndReplace(const WebHistoryItem& new_item,
                                            bool clone_children_of_target,
                                            RenderFrameImpl* target_frame,
                                            RenderViewImpl* render_view) {
  HistoryEntry* new_entry = new HistoryEntry();
  new_entry->root_.reset(
      root_->CloneAndReplace(new_entry,
                             new_item,
                             clone_children_of_target,
                             target_frame,
                             render_view->GetMainRenderFrame()));
  return new_entry;
}

HistoryEntry::HistoryNode* HistoryEntry::GetHistoryNodeForFrame(
    RenderFrameImpl* frame) {
  if (HistoryNode* history_node =
          frames_to_items_[GetFrameMap()[frame->GetRoutingID()]])
    return history_node;
  return unique_names_to_items_[frame->GetWebFrame()->uniqueName().utf8()];
}

WebHistoryItem HistoryEntry::GetItemForFrame(RenderFrameImpl* frame) {
  if (HistoryNode* history_node = GetHistoryNodeForFrame(frame))
    return history_node->item();
  return WebHistoryItem();
}

}  // namespace content