File: page_state_observer.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (173 lines) | stat: -rw-r--r-- 6,001 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cast_receiver/browser/page_state_observer.h"

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/process/kill.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"

namespace cast_receiver {

class PageStateObserver::WebContentsObserverWrapper
    : public content::WebContentsObserver {
 public:
  using content::WebContentsObserver::Observe;

  WebContentsObserverWrapper(PageStateObserver& wrapped,
                             content::WebContents* web_contents)
      : content::WebContentsObserver(web_contents), wrapped_(wrapped) {}

  explicit WebContentsObserverWrapper(PageStateObserver& wrapped)
      : wrapped_(wrapped) {}

  ~WebContentsObserverWrapper() override { Observe(nullptr); }

 private:
  void TryCallOnPageStopped(StopReason reason, net::Error error_code) {
    if (!navigation_handle_) {
      return;
    }

    navigation_handle_ = nullptr;
    wrapped_->OnPageStopped(reason, error_code);
  }

  void TryCallOnPageLoadComplete() {
    if (!navigation_handle_) {
      return;
    }

    navigation_handle_ = nullptr;
    wrapped_->OnPageLoadComplete();
  }

  // content::WebContentsObserver implementation.
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override {
    if (!navigation_handle->IsInPrimaryMainFrame() ||
        navigation_handle->IsSameDocument()) {
      return;
    }

    navigation_handle_ = navigation_handle;
  }

  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override {
    // Ignore sub-frame and non-current main frame navigation.
    if (navigation_handle != navigation_handle_) {
      return;
    }

    // If the navigation was not committed, it means either the page was a
    // download or error 204/205, or the navigation never left the previous
    // URL. Ignore these navigations.
    if (!navigation_handle->HasCommitted()) {
      LOG(WARNING) << "Navigation did not commit: url="
                   << navigation_handle->GetURL();
      navigation_handle_ = nullptr;
      return;
    }

    if (navigation_handle->IsErrorPage()) {
      const net::Error error_code = navigation_handle->GetNetErrorCode();
      LOG(ERROR) << "Got error on navigation: url="
                 << navigation_handle->GetURL() << ", error_code=" << error_code
                 << ", description=" << net::ErrorToShortString(error_code);
      TryCallOnPageStopped(StopReason::kHttpError, error_code);
      return;
    }

    // Notifies observers that the navigation of the main frame has finished
    // with no errors.
    TryCallOnPageLoadComplete();
  }

  void DidFinishLoad(content::RenderFrameHost* render_frame_host,
                     const GURL& validated_url) override {
    if (render_frame_host != web_contents()->GetPrimaryMainFrame()) {
      return;
    }

    // This logic is a subset of that for DidFinishLoad() in
    // CastWebContentsImpl.
    int http_status_code = 0;
    content::NavigationEntry* nav_entry =
        web_contents()->GetController().GetVisibleEntry();
    if (nav_entry) {
      http_status_code = nav_entry->GetHttpStatusCode();
    }

    if (http_status_code != 0 && http_status_code / 100 != 2) {
      DLOG(WARNING) << "Stopping after receiving http failure status code: "
                    << http_status_code;
      TryCallOnPageStopped(StopReason::kHttpError,
                           net::ERR_HTTP_RESPONSE_CODE_FAILURE);
      return;
    }

    TryCallOnPageLoadComplete();
  }

  void DidFailLoad(content::RenderFrameHost* render_frame_host,
                   const GURL& validated_url,
                   int error_code) override {
    // This logic is a subset of that for DidFailLoad() in CastWebContentsImpl.
    if (render_frame_host->GetParent()) {
      DLOG(ERROR) << "Got error on sub-iframe: url=" << validated_url.spec()
                  << ", error=" << error_code;
      return;
    }
    if (error_code == net::ERR_ABORTED) {
      // ERR_ABORTED means download was aborted by the app, typically this
      // happens when flinging URL for direct playback, the initial URLRequest
      // gets cancelled/aborted and then the same URL is requested via the
      // buffered data source for media::Pipeline playback.
      DLOG(WARNING) << "Load canceled: url=" << validated_url.spec();

      // We consider the page to be fully loaded in this case, since the app has
      // intentionally entered this state. If the app wanted to stop, it would
      // have called window.close() instead.
      TryCallOnPageLoadComplete();
      return;
    }

    TryCallOnPageStopped(StopReason::kHttpError,
                         static_cast<net::Error>(error_code));
  }

  void WebContentsDestroyed() override {
    content::WebContentsObserver::Observe(nullptr);
    TryCallOnPageStopped(StopReason::kApplicationRequest, net::OK);
  }

  void PrimaryMainFrameRenderProcessGone(
      base::TerminationStatus status) override {
    content::WebContentsObserver::Observe(nullptr);
    TryCallOnPageStopped(StopReason::kHttpError, net::ERR_UNEXPECTED);
  }

  raw_ptr<content::NavigationHandle> navigation_handle_ = nullptr;
  raw_ref<PageStateObserver> wrapped_;
};

PageStateObserver::PageStateObserver()
    : observer_wrapper_(std::make_unique<WebContentsObserverWrapper>(*this)) {}

PageStateObserver::PageStateObserver(content::WebContents* web_contents)
    : observer_wrapper_(
          std::make_unique<WebContentsObserverWrapper>(*this, web_contents)) {}

PageStateObserver::~PageStateObserver() = default;

void PageStateObserver::Observe(content::WebContents* web_contents) {
  observer_wrapper_->Observe(web_contents);
}

}  // namespace cast_receiver