File: observer_wrapper.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,531 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_OBSERVER_WRAPPER_H_
#define SERVICES_NETWORK_OBSERVER_WRAPPER_H_

#include "base/memory/raw_ptr.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace network {

// Manages an observer interface pointer that might come from a request-specific
// mojo::PendingRemote or a shared, factory-level fallback pointer.
//
// This pattern is used because for browser-initiated requests, observer remotes
// (like DevToolsObserver, CookieAccessObserver) might be provided specifically
// for that request via `ResourceRequest::TrustedParams`. In such cases, the
// `remote` parameter passed to the constructor will be valid, and this wrapper
// will bind and use it.
//
// For other requests (e.g., subresources initiated by a renderer), the
// `TrustedParams` does not contain these specific observer remotes. Instead,
// these requests need to use a shared observer remote that was provided by the
// browser process when the URLLoaderFactory was created and is stored within
// that factory. The `fallback` parameter passed to the constructor typically
// points to the implementation associated with this shared remote.
//
// This class abstracts this selection logic.
//
// NOTE: This implementation caches the raw pointer at construction and does not
// dynamically update if the remote disconnects.
template <typename T>
class ObserverWrapper {
 public:
  // Default constructor initializes with no observer.
  ObserverWrapper() : ptr_(nullptr) {}

  // Constructor taking a PendingRemote and an optional fallback raw pointer.
  // If the remote is valid, it's bound, and the internal pointer points to it.
  // Otherwise, the internal pointer points to the fallback.
  explicit ObserverWrapper(mojo::PendingRemote<T> remote, T* fallback = nullptr)
      : remote_(std::move(remote)) {
    ptr_ = remote_.is_bound() ? remote_.get() : fallback;
  }

  // Allows checking the wrapper in boolean contexts (true if it points to
  // a observer).
  explicit operator bool() const { return !!ptr_; }

  // Returns the cached raw pointer (either remote impl or fallback).
  T* get() const { return ptr_.get(); }
  // Provides pointer-like access via the arrow operator.
  T* operator->() const {
    CHECK(get());
    return get();
  }
  // Provides pointer-like access via the dereference operator.
  T& operator*() const {
    CHECK(get());
    return *get();
  }

  ObserverWrapper(const ObserverWrapper&) = delete;
  ObserverWrapper& operator=(const ObserverWrapper&) = delete;

  // Move constructor.
  ObserverWrapper(ObserverWrapper&& other)
      : remote_(std::move(other.remote_)),
        ptr_(std::exchange(other.ptr_, nullptr)) {}
  // Move assignment operator.
  ObserverWrapper& operator=(ObserverWrapper&& other) {
    if (this == &other) {
      return *this;
    }
    remote_ = std::move(other.remote_);
    ptr_ = other.ptr_;
    other.ptr_ = nullptr;
    return *this;
  }

  // Allows taking ownership of the internal `mojo::Remote`.
  mojo::Remote<T> TakeRemote() {
    ptr_ = nullptr;
    return std::move(remote_);
  }

 private:
  // Holds the Mojo remote connection, if one was provided.
  mojo::Remote<T> remote_;
  // Cached raw pointer to either the remote implementation or the fallback.
  raw_ptr<T> ptr_;
};

}  // namespace network

#endif  // SERVICES_NETWORK_OBSERVER_WRAPPER_H_