1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
7 
8 #include "base/macros.h"
9 #include "components/guest_view/browser/guest_view_base.h"
10 #include "components/guest_view/browser/guest_view_manager.h"
11 #include "content/public/browser/render_frame_host.h"
12 
13 namespace guest_view {
14 
15 // A GuestView is the templated base class for out-of-process frames in the
16 // chrome layer. GuestView is templated on its derived type to allow for type-
17 // safe access. See GuestViewBase for more information.
18 template <typename T>
19 class GuestView : public GuestViewBase {
20  public:
From(int embedder_process_id,int guest_instance_id)21   static T* From(int embedder_process_id, int guest_instance_id) {
22     auto* guest = GuestViewBase::From(embedder_process_id, guest_instance_id);
23     if (!guest)
24       return nullptr;
25     return guest->As<T>();
26   }
27 
FromWebContents(const content::WebContents * contents)28   static T* FromWebContents(const content::WebContents* contents) {
29     auto* guest = GuestViewBase::FromWebContents(contents);
30     return guest ? guest->As<T>() : nullptr;
31   }
32 
FromFrameID(int render_process_id,int render_frame_id)33   static T* FromFrameID(int render_process_id, int render_frame_id) {
34     auto* render_frame_host =
35         content::RenderFrameHost::FromID(render_process_id, render_frame_id);
36     if (!render_frame_host)
37       return nullptr;
38 
39     auto* web_contents =
40         content::WebContents::FromRenderFrameHost(render_frame_host);
41     return FromWebContents(web_contents);
42   }
43 
GetOpener()44   T* GetOpener() const {
45     GuestViewBase* guest = GuestViewBase::GetOpener();
46     if (!guest)
47       return nullptr;
48     return guest->As<T>();
49   }
50 
SetOpener(T * opener)51   void SetOpener(T* opener) {
52     GuestViewBase::SetOpener(opener);
53   }
54 
55   // GuestViewBase implementation.
GetViewType()56   const char* GetViewType() const final {
57     return T::Type;
58   }
59 
60  protected:
GuestView(content::WebContents * owner_web_contents)61   explicit GuestView(content::WebContents* owner_web_contents)
62       : GuestViewBase(owner_web_contents) {}
~GuestView()63   ~GuestView() override {}
64 
65  private:
66   DISALLOW_COPY_AND_ASSIGN(GuestView);
67 };
68 
69 }  // namespace guest_view
70 
71 #endif  // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
72