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

content / public / browser / document_service_internal.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_BROWSER_DOCUMENT_SERVICE_INTERNAL_H_
#define CONTENT_PUBLIC_BROWSER_DOCUMENT_SERVICE_INTERNAL_H_

#include "base/memory/raw_ref.h"
#include "base/types/pass_key.h"
#include "content/common/content_export.h"

namespace content {

class DocumentAssociatedData;
class RenderFrameHost;
enum class DocumentServiceDestructionReason : int;

template <typename T>
class DocumentService;

namespace internal {

// Internal helper to provide a common base class for `DocumentService<T>` so
// that //content can internally track all live `DocumentService<T>` instances.
class CONTENT_EXPORT DocumentServiceBase {
 public:
  DocumentServiceBase(const DocumentServiceBase&) = delete;
  DocumentServiceBase& operator=(const DocumentServiceBase&) = delete;

  virtual ~DocumentServiceBase();

  // To be called just before the destructor, when the object does not
  // self-destroy via one of the *AndDeleteThis() helpers. `reason` provides
  // context on why `this` is being destroyed (i.e. the document is deleted or
  // the Mojo message pipe is disconnected); subclasses can override this method
  // to react in a specific way to a destruction reason.
  virtual void WillBeDestroyed(DocumentServiceDestructionReason reason) {}

  // Internal implementation helpers:
  // Resets the receiver and then deletes `this`.
  virtual void ResetAndDeleteThisInternal(
      base::PassKey<DocumentAssociatedData>) = 0;

  // Unregisters `this` from `DocumentAssociatedData`.
  template <typename T>
  void InternalUnregister(base::PassKey<DocumentService<T>>) {
    InternalUnregisterImpl();
  }

 protected:
  explicit DocumentServiceBase(RenderFrameHost& render_frame_host);

  RenderFrameHost& render_frame_host() const { return *render_frame_host_; }

 private:
  void InternalUnregisterImpl();

  const raw_ref<RenderFrameHost> render_frame_host_;
};

}  // namespace internal

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_DOCUMENT_SERVICE_INTERNAL_H_