summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/navigation_impl.h
blob: c002d887525abfbb68477a8b13abc18cdc58ff02 (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
// 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.

#ifndef WEBLAYER_BROWSER_NAVIGATION_IMPL_H_
#define WEBLAYER_BROWSER_NAVIGATION_IMPL_H_

#include <memory>

#include "base/macros.h"
#include "base/optional.h"
#include "build/build_config.h"
#include "content/public/browser/navigation_controller.h"
#include "weblayer/public/navigation.h"

#if defined(OS_ANDROID)
#include "base/android/scoped_java_ref.h"
#endif

namespace content {
class NavigationHandle;
}

namespace weblayer {

class NavigationImpl : public Navigation {
 public:
  explicit NavigationImpl(content::NavigationHandle* navigation_handle);
  ~NavigationImpl() override;

  void set_should_stop_when_throttle_created() {
    should_stop_when_throttle_created_ = true;
  }
  bool should_stop_when_throttle_created() const {
    return should_stop_when_throttle_created_;
  }

  void set_safe_to_set_request_headers(bool value) {
    safe_to_set_request_headers_ = value;
  }

  void set_safe_to_set_user_agent(bool value) {
    safe_to_set_user_agent_ = value;
  }

  void set_was_stopped() { was_stopped_ = true; }

  void SetParamsToLoadWhenSafe(
      std::unique_ptr<content::NavigationController::LoadURLParams> params);
  std::unique_ptr<content::NavigationController::LoadURLParams>
  TakeParamsToLoadWhenSafe();

#if defined(OS_ANDROID)
  void SetJavaNavigation(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& java_navigation);
  int GetState(JNIEnv* env) { return static_cast<int>(GetState()); }
  base::android::ScopedJavaLocalRef<jstring> GetUri(JNIEnv* env);
  base::android::ScopedJavaLocalRef<jobjectArray> GetRedirectChain(JNIEnv* env);
  int GetHttpStatusCode(JNIEnv* env) { return GetHttpStatusCode(); }
  bool IsSameDocument(JNIEnv* env) { return IsSameDocument(); }
  bool IsErrorPage(JNIEnv* env) { return IsErrorPage(); }
  bool IsDownload(JNIEnv* env) { return IsDownload(); }
  bool WasStopCalled(JNIEnv* env) { return WasStopCalled(); }
  int GetLoadError(JNIEnv* env) { return static_cast<int>(GetLoadError()); }
  jboolean SetRequestHeader(JNIEnv* env,
                            const base::android::JavaParamRef<jstring>& name,
                            const base::android::JavaParamRef<jstring>& value);
  jboolean SetUserAgentString(
      JNIEnv* env,
      const base::android::JavaParamRef<jstring>& value);

  base::android::ScopedJavaGlobalRef<jobject> java_navigation() {
    return java_navigation_;
  }
#endif

 private:
  // Navigation implementation:
  GURL GetURL() override;
  const std::vector<GURL>& GetRedirectChain() override;
  NavigationState GetState() override;
  int GetHttpStatusCode() override;
  bool IsSameDocument() override;
  bool IsErrorPage() override;
  bool IsDownload() override;
  bool WasStopCalled() override;
  LoadError GetLoadError() override;
  void SetRequestHeader(const std::string& name,
                        const std::string& value) override;
  void SetUserAgentString(const std::string& value) override;

  content::NavigationHandle* navigation_handle_;

  // Used to delay calling Stop() until safe. See
  // NavigationControllerImpl::NavigationThrottleImpl for details.
  bool should_stop_when_throttle_created_ = false;

  // Whether SetRequestHeader() is allowed at this time.
  bool safe_to_set_request_headers_ = false;

  // Whether SetUserAgentString() is allowed at this time.
  bool safe_to_set_user_agent_ = false;

  // Whether NavigationController::Stop() was called for this navigation.
  bool was_stopped_ = false;

#if defined(OS_ANDROID)
  base::android::ScopedJavaGlobalRef<jobject> java_navigation_;
#endif

  // Used to delay loading until safe. In particular, if Navigate() is called
  // from NavigationStarted(), then the parameters are captured and the
  // navigation started later on. The delaying is necessary as content is not
  // reentrant, and this triggers some amount of reentrancy.
  std::unique_ptr<content::NavigationController::LoadURLParams>
      scheduled_load_params_;

  DISALLOW_COPY_AND_ASSIGN(NavigationImpl);
};

}  // namespace weblayer

#endif  // WEBLAYER_BROWSER_NAVIGATION_IMPL_H_