summaryrefslogtreecommitdiffstats
path: root/components/web_view/navigation_controller.cc
blob: 87304fae2866a0f8c1158e79d71ca337d8b3552a (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
// Copyright 2015 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.

#include "components/web_view/navigation_controller.h"

#include "components/web_view/frame.h"
#include "components/web_view/navigation_controller_delegate.h"
#include "components/web_view/navigation_entry.h"
#include "components/web_view/reload_type.h"

namespace web_view {

NavigationController::NavigationController(
    NavigationControllerDelegate* delegate)
    : pending_entry_(nullptr),
      last_committed_entry_index_(-1),
      pending_entry_index_(-1),
      delegate_(delegate) {}

NavigationController::~NavigationController() {
  DiscardPendingEntry(false);
}

int NavigationController::GetCurrentEntryIndex() const {
  if (pending_entry_index_ != -1)
    return pending_entry_index_;
  return last_committed_entry_index_;
}

int NavigationController::GetIndexForOffset(int offset) const {
  return GetCurrentEntryIndex() + offset;
}

int NavigationController::GetEntryCount() const {
  // TODO(erg): Have a max number of entries, and DCHECK that we are smaller
  // than it here.
  return static_cast<int>(entries_.size());
}

NavigationEntry* NavigationController::GetEntryAtIndex(int index) const {
  if (index < 0 || index >= GetEntryCount())
    return nullptr;

  return entries_[index];
}

NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
  return GetEntryAtIndex(GetIndexForOffset(offset));
}

bool NavigationController::CanGoBack() const {
  return CanGoToOffset(-1);
}

bool NavigationController::CanGoForward() const {
  return CanGoToOffset(1);
}

bool NavigationController::CanGoToOffset(int offset) const {
  int index = GetIndexForOffset(offset);
  return index >= 0 && index < GetEntryCount();
}

void NavigationController::GoBack() {
  if (!CanGoBack()) {
    NOTREACHED();
    return;
  }

  // Base the navigation on where we are now...
  int current_index = GetCurrentEntryIndex();

  DiscardPendingEntry(false);

  pending_entry_index_ = current_index - 1;
  // TODO(erg): Transition type handled here.
  NavigateToPendingEntry(ReloadType::NO_RELOAD, true);
}

void NavigationController::GoForward() {
  if (!CanGoForward()) {
    NOTREACHED();
    return;
  }

  // TODO(erg): The upstream version handles transience here.

  // Base the navigation on where we are now...
  int current_index = GetCurrentEntryIndex();

  DiscardPendingEntry(false);

  pending_entry_index_ = current_index + 1;
  // TODO(erg): Transition type handled here.
  NavigateToPendingEntry(ReloadType::NO_RELOAD, true);
}

void NavigationController::LoadURL(mojo::URLRequestPtr request) {
  // TODO(erg): This mimics part of NavigationControllerImpl::LoadURL(), minus
  // all the error checking.
  SetPendingEntry(make_scoped_ptr(new NavigationEntry(request.Pass())));
  NavigateToPendingEntry(ReloadType::NO_RELOAD, false);
}

void NavigationController::NavigateToPendingEntry(
    ReloadType reload_type,
    bool update_navigation_start_time) {
  // TODO(erg): Deal with session history navigations while trying to navigate
  // to a slow-to-commit page.

  // TODO(erg): Deal with interstitials.

  // For session history navigations only the pending_entry_index_ is set.
  if (!pending_entry_) {
    CHECK_NE(pending_entry_index_, -1);
    pending_entry_ = entries_[pending_entry_index_];
  }

  // TODO(erg): Eventually, we need to deal with restoring the state of the
  // full tree. For now, we'll just shell back to the WebView.
  delegate_->OnNavigate(
      pending_entry_->BuildURLRequest(update_navigation_start_time));
}

void NavigationController::DiscardPendingEntry(bool was_failure) {
  // TODO(erg): We might copy the CHECK regarding NavigateToEntry here.

  // TODO(erg): We need to maintain the failed_pending_entry_ during
  // |was_failure| here.

  if (pending_entry_index_ == -1)
    delete pending_entry_;
  pending_entry_ = nullptr;
  pending_entry_index_ = -1;
}

void NavigationController::SetPendingEntry(scoped_ptr<NavigationEntry> entry) {
  // TODO(erg): Should be DiscardNonCommittedEntries() if we start porting
  // transient states over.
  DiscardPendingEntry(false);
  pending_entry_ = entry.release();
  DCHECK_EQ(-1, pending_entry_index_);
  // TODO(erg): The content code sends NOTIFICATION_NAV_ENTRY_PENDING here.
}

void NavigationController::FrameDidCommitProvisionalLoad(Frame* frame) {
  // Our renderer is committing a frame. If this was a real implementation,
  // we'd do something!
  if (frame->parent())
    return;

  // TODO(erg): Need some equivalent of InsertOrReplaceEntry(), as we need to
  // prune the history list.

  // TODO(erg): We should copy all the logic from the various
  // RendererDidNavigate* methods here.

  // TODO(erg): Medium term, we shouldn't be reusing the NavigationEntry, as it
  // appears that blink can change some of the data during the navigation. Do
  // it for now for bootstrapping purposes.
  if (pending_entry_index_ == -1 && pending_entry_) {
    ClearForwardEntries();
    entries_.push_back(pending_entry_);
    last_committed_entry_index_ = static_cast<int>(entries_.size() - 1);
    pending_entry_ = nullptr;
  } else if (pending_entry_index_ != -1 && pending_entry_) {
    last_committed_entry_index_ = pending_entry_index_;
    pending_entry_ = nullptr;

    // This was a historical navigation. In this limited prototype, we don't
    // actually do anything with it.
  } else {
    LOG(ERROR)
        << "Hit an unknown case in NavigationController::RendererDidNavigate";
  }

  DiscardPendingEntry(false);

  delegate_->OnDidNavigate();
}

void NavigationController::FrameDidNavigateLocally(Frame* frame,
                                                   const GURL& url) {
  // If this is a local navigation of a non-top frame, don't try to commit
  // it.
  if (frame->parent())
    return;

  ClearForwardEntries();

  // TODO(erg): This is overly cheap handling of local navigations in
  // frames. We don't have all the information needed to construct a real
  // URLRequest.

  entries_.push_back(new NavigationEntry(url));
  last_committed_entry_index_ = static_cast<int>(entries_.size() - 1);

  delegate_->OnDidNavigate();
}

void NavigationController::ClearForwardEntries() {
  int current_size = static_cast<int>(entries_.size());
  if (current_size > 0) {
    while (last_committed_entry_index_ < (current_size - 1)) {
      entries_.pop_back();
      current_size--;
    }
  }
}

}  // namespace web_view