summaryrefslogtreecommitdiff
path: root/chromium/components/guest_view/browser/guest_view.h
blob: 70c9fb18037c49da59c3a2808d79936d81901975 (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
// Copyright 2014 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 COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
#define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_

#include "base/macros.h"
#include "components/guest_view/browser/guest_view_base.h"
#include "components/guest_view/browser/guest_view_manager.h"
#include "content/public/browser/render_frame_host.h"

namespace guest_view {

// A GuestView is the templated base class for out-of-process frames in the
// chrome layer. GuestView is templated on its derived type to allow for type-
// safe access. See GuestViewBase for more information.
template <typename T>
class GuestView : public GuestViewBase {
 public:
  static T* From(int embedder_process_id, int guest_instance_id) {
    auto* guest = GuestViewBase::From(embedder_process_id, guest_instance_id);
    if (!guest)
      return nullptr;
    return guest->As<T>();
  }

  static T* FromWebContents(const content::WebContents* contents) {
    auto* guest = GuestViewBase::FromWebContents(contents);
    return guest ? guest->As<T>() : nullptr;
  }

  static T* FromFrameID(int render_process_id, int render_frame_id) {
    auto* render_frame_host =
        content::RenderFrameHost::FromID(render_process_id, render_frame_id);
    if (!render_frame_host)
      return nullptr;

    auto* web_contents =
        content::WebContents::FromRenderFrameHost(render_frame_host);
    return FromWebContents(web_contents);
  }

  T* GetOpener() const {
    GuestViewBase* guest = GuestViewBase::GetOpener();
    if (!guest)
      return nullptr;
    return guest->As<T>();
  }

  void SetOpener(T* opener) {
    GuestViewBase::SetOpener(opener);
  }

  // GuestViewBase implementation.
  const char* GetViewType() const final {
    return T::Type;
  }

 protected:
  explicit GuestView(content::WebContents* owner_web_contents)
      : GuestViewBase(owner_web_contents) {}
  ~GuestView() override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(GuestView);
};

}  // namespace guest_view

#endif  // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_