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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
// Copyright 2019 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 "weblayer/browser/navigation_controller_impl.h"
#include <utility>
#include "base/auto_reset.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/page_transition_types.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/public/navigation_observer.h"
#if defined(OS_ANDROID)
#include "base/android/jni_string.h"
#include "base/trace_event/trace_event.h"
#include "weblayer/browser/java/jni/NavigationControllerImpl_jni.h"
#endif
#if defined(OS_ANDROID)
using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
#endif
namespace weblayer {
// NavigationThrottle implementation responsible for delaying certain
// operations and performing them when safe. This is necessary as content
// does allow certain operations to be called at certain times. For example,
// content does not allow calling WebContents::Stop() from
// WebContentsObserver::DidStartNavigation() (to do so crashes). To work around
// this NavigationControllerImpl detects these scenarios and delays processing
// until safe.
//
// Most of the support for these scenarios is handled by a custom
// NavigationThrottle. To make things interesting, the NavigationThrottle is
// created after some of the scenarios this code wants to handle. As such,
// NavigationImpl does some amount of caching until the NavigationThrottle is
// created.
class NavigationControllerImpl::NavigationThrottleImpl
: public content::NavigationThrottle {
public:
NavigationThrottleImpl(NavigationControllerImpl* controller,
content::NavigationHandle* handle)
: NavigationThrottle(handle), controller_(controller) {}
NavigationThrottleImpl(const NavigationThrottleImpl&) = delete;
NavigationThrottleImpl& operator=(const NavigationThrottleImpl&) = delete;
~NavigationThrottleImpl() override = default;
void ScheduleCancel() { should_cancel_ = true; }
void ScheduleNavigate(
std::unique_ptr<content::NavigationController::LoadURLParams> params) {
load_params_ = std::move(params);
}
// content::NavigationThrottle:
ThrottleCheckResult WillStartRequest() override {
const bool should_cancel = should_cancel_;
if (load_params_)
controller_->DoNavigate(std::move(load_params_));
// WARNING: this may have been deleted.
return should_cancel ? CANCEL : PROCEED;
}
ThrottleCheckResult WillRedirectRequest() override {
controller_->WillRedirectRequest(this, navigation_handle());
const bool should_cancel = should_cancel_;
if (load_params_)
controller_->DoNavigate(std::move(load_params_));
// WARNING: this may have been deleted.
return should_cancel ? CANCEL : PROCEED;
}
const char* GetNameForLogging() override {
return "WebLayerNavigationControllerThrottle";
}
private:
NavigationControllerImpl* controller_;
bool should_cancel_ = false;
std::unique_ptr<content::NavigationController::LoadURLParams> load_params_;
};
NavigationControllerImpl::NavigationControllerImpl(TabImpl* tab)
: WebContentsObserver(tab->web_contents()) {}
NavigationControllerImpl::~NavigationControllerImpl() = default;
std::unique_ptr<content::NavigationThrottle>
NavigationControllerImpl::CreateNavigationThrottle(
content::NavigationHandle* handle) {
if (!handle->IsInMainFrame())
return nullptr;
auto throttle = std::make_unique<NavigationThrottleImpl>(this, handle);
DCHECK(navigation_map_.find(handle) != navigation_map_.end());
auto* navigation = navigation_map_[handle].get();
if (navigation->should_stop_when_throttle_created())
throttle->ScheduleCancel();
auto load_params = navigation->TakeParamsToLoadWhenSafe();
if (load_params)
throttle->ScheduleNavigate(std::move(load_params));
return throttle;
}
#if defined(OS_ANDROID)
void NavigationControllerImpl::SetNavigationControllerImpl(
JNIEnv* env,
const JavaParamRef<jobject>& java_controller) {
java_controller_ = java_controller;
}
void NavigationControllerImpl::Navigate(JNIEnv* env,
const JavaParamRef<jstring>& url) {
Navigate(GURL(base::android::ConvertJavaStringToUTF8(env, url)));
}
void NavigationControllerImpl::NavigateWithParams(
JNIEnv* env,
const JavaParamRef<jstring>& url,
jboolean should_replace_current_entry) {
auto params = std::make_unique<content::NavigationController::LoadURLParams>(
GURL(base::android::ConvertJavaStringToUTF8(env, url)));
params->should_replace_current_entry = should_replace_current_entry;
DoNavigate(std::move(params));
}
ScopedJavaLocalRef<jstring>
NavigationControllerImpl::GetNavigationEntryDisplayUri(
JNIEnv* env,
int index) {
return ScopedJavaLocalRef<jstring>(base::android::ConvertUTF8ToJavaString(
env, GetNavigationEntryDisplayURL(index).spec()));
}
ScopedJavaLocalRef<jstring> NavigationControllerImpl::GetNavigationEntryTitle(
JNIEnv* env,
int index) {
return ScopedJavaLocalRef<jstring>(base::android::ConvertUTF8ToJavaString(
env, GetNavigationEntryTitle(index)));
}
bool NavigationControllerImpl::IsNavigationEntrySkippable(JNIEnv* env,
int index) {
return IsNavigationEntrySkippable(index);
}
#endif
void NavigationControllerImpl::WillRedirectRequest(
NavigationThrottleImpl* throttle,
content::NavigationHandle* navigation_handle) {
DCHECK(navigation_handle->IsInMainFrame());
DCHECK(navigation_map_.find(navigation_handle) != navigation_map_.end());
auto* navigation = navigation_map_[navigation_handle].get();
navigation->set_safe_to_set_request_headers(true);
DCHECK(!active_throttle_);
base::AutoReset<NavigationThrottleImpl*> auto_reset(&active_throttle_,
throttle);
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_navigationRedirected");
Java_NavigationControllerImpl_navigationRedirected(
AttachCurrentThread(), java_controller_, navigation->java_navigation());
}
#endif
for (auto& observer : observers_)
observer.NavigationRedirected(navigation);
navigation->set_safe_to_set_request_headers(false);
}
void NavigationControllerImpl::AddObserver(NavigationObserver* observer) {
observers_.AddObserver(observer);
}
void NavigationControllerImpl::RemoveObserver(NavigationObserver* observer) {
observers_.RemoveObserver(observer);
}
void NavigationControllerImpl::Navigate(const GURL& url) {
DoNavigate(
std::make_unique<content::NavigationController::LoadURLParams>(url));
}
void NavigationControllerImpl::Navigate(
const GURL& url,
const NavigationController::NavigateParams& params) {
auto load_params =
std::make_unique<content::NavigationController::LoadURLParams>(url);
load_params->should_replace_current_entry =
params.should_replace_current_entry;
DoNavigate(std::move(load_params));
}
void NavigationControllerImpl::GoBack() {
web_contents()->GetController().GoBack();
}
void NavigationControllerImpl::GoForward() {
web_contents()->GetController().GoForward();
}
bool NavigationControllerImpl::CanGoBack() {
return web_contents()->GetController().CanGoBack();
}
bool NavigationControllerImpl::CanGoForward() {
return web_contents()->GetController().CanGoForward();
}
void NavigationControllerImpl::GoToIndex(int index) {
web_contents()->GetController().GoToIndex(index);
}
void NavigationControllerImpl::Reload() {
web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
}
void NavigationControllerImpl::Stop() {
NavigationImpl* navigation = nullptr;
if (navigation_starting_) {
navigation_starting_->set_should_stop_when_throttle_created();
navigation = navigation_starting_;
} else if (active_throttle_) {
active_throttle_->ScheduleCancel();
DCHECK(navigation_map_.find(active_throttle_->navigation_handle()) !=
navigation_map_.end());
navigation = navigation_map_[active_throttle_->navigation_handle()].get();
} else {
web_contents()->Stop();
}
if (navigation)
navigation->set_was_stopped();
}
int NavigationControllerImpl::GetNavigationListSize() {
return web_contents()->GetController().GetEntryCount();
}
int NavigationControllerImpl::GetNavigationListCurrentIndex() {
return web_contents()->GetController().GetCurrentEntryIndex();
}
GURL NavigationControllerImpl::GetNavigationEntryDisplayURL(int index) {
auto* entry = web_contents()->GetController().GetEntryAtIndex(index);
if (!entry)
return GURL();
return entry->GetVirtualURL();
}
std::string NavigationControllerImpl::GetNavigationEntryTitle(int index) {
auto* entry = web_contents()->GetController().GetEntryAtIndex(index);
if (!entry)
return std::string();
return base::UTF16ToUTF8(entry->GetTitle());
}
bool NavigationControllerImpl::IsNavigationEntrySkippable(int index) {
return web_contents()->GetController().IsEntryMarkedToBeSkipped(index);
}
void NavigationControllerImpl::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
if (!navigation_handle->IsInMainFrame())
return;
// This function should not be called reentrantly.
DCHECK(!navigation_starting_);
DCHECK(!base::Contains(navigation_map_, navigation_handle));
navigation_map_[navigation_handle] =
std::make_unique<NavigationImpl>(navigation_handle);
auto* navigation = navigation_map_[navigation_handle].get();
base::AutoReset<NavigationImpl*> auto_reset(&navigation_starting_,
navigation);
navigation->set_safe_to_set_request_headers(true);
navigation->set_safe_to_set_user_agent(true);
#if defined(OS_ANDROID)
if (java_controller_) {
JNIEnv* env = AttachCurrentThread();
{
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_createNavigation");
Java_NavigationControllerImpl_createNavigation(
env, java_controller_, reinterpret_cast<jlong>(navigation));
}
TRACE_EVENT0("weblayer", "Java_NavigationControllerImpl_navigationStarted");
Java_NavigationControllerImpl_navigationStarted(
env, java_controller_, navigation->java_navigation());
}
#endif
for (auto& observer : observers_)
observer.NavigationStarted(navigation);
navigation->set_safe_to_set_user_agent(false);
navigation->set_safe_to_set_request_headers(false);
}
void NavigationControllerImpl::DidRedirectNavigation(
content::NavigationHandle* navigation_handle) {
// NOTE: this implementation should remain empty. Real implementation is in
// WillRedirectNavigation(). See description of NavigationThrottleImpl for
// more information.
}
void NavigationControllerImpl::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
if (!navigation_handle->IsInMainFrame())
return;
DCHECK(navigation_map_.find(navigation_handle) != navigation_map_.end());
auto* navigation = navigation_map_[navigation_handle].get();
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_readyToCommitNavigation");
Java_NavigationControllerImpl_readyToCommitNavigation(
AttachCurrentThread(), java_controller_, navigation->java_navigation());
}
#endif
for (auto& observer : observers_)
observer.ReadyToCommitNavigation(navigation);
}
void NavigationControllerImpl::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (!navigation_handle->IsInMainFrame())
return;
DCHECK(navigation_map_.find(navigation_handle) != navigation_map_.end());
auto* navigation = navigation_map_[navigation_handle].get();
if (navigation_handle->GetNetErrorCode() == net::OK &&
!navigation_handle->IsErrorPage()) {
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_navigationCompleted");
Java_NavigationControllerImpl_navigationCompleted(
AttachCurrentThread(), java_controller_,
navigation->java_navigation());
}
#endif
for (auto& observer : observers_)
observer.NavigationCompleted(navigation);
} else {
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_navigationFailed");
Java_NavigationControllerImpl_navigationFailed(
AttachCurrentThread(), java_controller_,
navigation->java_navigation());
}
#endif
for (auto& observer : observers_)
observer.NavigationFailed(navigation);
}
// Note InsertVisualStateCallback currently does not take into account
// any delays from surface sync, ie a frame submitted by renderer may not
// be displayed immediately. Such situations should be rare however, so
// this should be good enough for the purposes needed.
web_contents()->GetMainFrame()->InsertVisualStateCallback(base::BindOnce(
&NavigationControllerImpl::OldPageNoLongerRendered,
weak_ptr_factory_.GetWeakPtr(), navigation_handle->GetURL()));
navigation_map_.erase(navigation_map_.find(navigation_handle));
}
void NavigationControllerImpl::DidStartLoading() {
NotifyLoadStateChanged();
}
void NavigationControllerImpl::DidStopLoading() {
NotifyLoadStateChanged();
}
void NavigationControllerImpl::LoadProgressChanged(double progress) {
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_loadProgressChanged");
Java_NavigationControllerImpl_loadProgressChanged(
AttachCurrentThread(), java_controller_, progress);
}
#endif
for (auto& observer : observers_)
observer.LoadProgressChanged(progress);
}
void NavigationControllerImpl::DidFirstVisuallyNonEmptyPaint() {
#if defined(OS_ANDROID)
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_onFirstContentfulPaint");
Java_NavigationControllerImpl_onFirstContentfulPaint(AttachCurrentThread(),
java_controller_);
#endif
for (auto& observer : observers_)
observer.OnFirstContentfulPaint();
}
void NavigationControllerImpl::OldPageNoLongerRendered(const GURL& url,
bool success) {
#if defined(OS_ANDROID)
TRACE_EVENT0("weblayer",
"Java_NavigationControllerImpl_onOldPageNoLongerRendered");
JNIEnv* env = AttachCurrentThread();
Java_NavigationControllerImpl_onOldPageNoLongerRendered(
env, java_controller_,
base::android::ConvertUTF8ToJavaString(env, url.spec()));
#endif
for (auto& observer : observers_)
observer.OnOldPageNoLongerRendered(url);
}
void NavigationControllerImpl::NotifyLoadStateChanged() {
#if defined(OS_ANDROID)
if (java_controller_) {
TRACE_EVENT0("weblayer", "Java_NavigationControllerImpl_loadStateChanged");
Java_NavigationControllerImpl_loadStateChanged(
AttachCurrentThread(), java_controller_, web_contents()->IsLoading(),
web_contents()->IsLoadingToDifferentDocument());
}
#endif
for (auto& observer : observers_) {
observer.LoadStateChanged(web_contents()->IsLoading(),
web_contents()->IsLoadingToDifferentDocument());
}
}
void NavigationControllerImpl::DoNavigate(
std::unique_ptr<content::NavigationController::LoadURLParams> params) {
// Navigations should use the default user-agent. If the embedder wants a
// custom user-agent, the embedder will call Navigation::SetUserAgentString().
params->override_user_agent =
content::NavigationController::UA_OVERRIDE_FALSE;
if (navigation_starting_) {
// DoNavigate() is being called reentrantly. Delay processing until it's
// safe.
Stop();
navigation_starting_->SetParamsToLoadWhenSafe(std::move(params));
return;
}
if (active_throttle_) {
// DoNavigate() is being called reentrantly. Delay processing until it's
// safe.
Stop();
active_throttle_->ScheduleNavigate(std::move(params));
return;
}
// For WebLayer's production use cases, navigations from the embedder are most
// appropriately viewed as being from links with user gestures. In particular,
// this ensures that intents resulting from these navigations get launched as
// the embedder expects.
params->transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK);
params->has_user_gesture = true;
web_contents()->GetController().LoadURLWithParams(*params);
// So that if the user had entered the UI in a bar it stops flashing the
// caret.
web_contents()->Focus();
}
#if defined(OS_ANDROID)
static jlong JNI_NavigationControllerImpl_GetNavigationController(JNIEnv* env,
jlong tab) {
return reinterpret_cast<jlong>(
reinterpret_cast<Tab*>(tab)->GetNavigationController());
}
#endif
} // namespace weblayer
|