Home | History | Annotate | Download | only in guest_view
      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 EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_H_
      6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_H_
      7 
      8 #include "base/bind.h"
      9 #include "content/public/browser/render_frame_host.h"
     10 #include "extensions/browser/guest_view/guest_view_base.h"
     11 
     12 namespace extensions {
     13 
     14 // A GuestView is the templated base class for out-of-process frames in the
     15 // chrome layer. GuestView is templated on its derived type to allow for type-
     16 // safe access. See GuestViewBase for more information.
     17 template <typename T>
     18 class GuestView : public GuestViewBase {
     19  public:
     20   static void Register() {
     21     GuestViewBase::RegisterGuestViewType(T::Type, base::Bind(&T::Create));
     22   }
     23 
     24   static T* From(int embedder_process_id, int guest_instance_id) {
     25     GuestViewBase* guest =
     26         GuestViewBase::From(embedder_process_id, guest_instance_id);
     27     if (!guest)
     28       return NULL;
     29     return guest->As<T>();
     30   }
     31 
     32   static T* FromWebContents(content::WebContents* contents) {
     33     GuestViewBase* guest = GuestViewBase::FromWebContents(contents);
     34     return guest ? guest->As<T>() : NULL;
     35   }
     36 
     37   static T* FromFrameID(int render_process_id, int render_frame_id) {
     38     content::RenderFrameHost* render_frame_host =
     39         content::RenderFrameHost::FromID(render_process_id, render_frame_id);
     40     if (!render_frame_host) {
     41       return NULL;
     42     }
     43     content::WebContents* web_contents =
     44         content::WebContents::FromRenderFrameHost(render_frame_host);
     45     return FromWebContents(web_contents);
     46   }
     47 
     48   T* GetOpener() const {
     49     GuestViewBase* guest = GuestViewBase::GetOpener();
     50     if (!guest)
     51       return NULL;
     52     return guest->As<T>();
     53   }
     54 
     55   void SetOpener(T* opener) {
     56     GuestViewBase::SetOpener(opener);
     57   }
     58 
     59   // GuestViewBase implementation.
     60   virtual const char* GetViewType() const OVERRIDE {
     61     return T::Type;
     62   }
     63 
     64  protected:
     65   GuestView(content::BrowserContext* browser_context,
     66             int guest_instance_id)
     67       : GuestViewBase(browser_context, guest_instance_id) {}
     68   virtual ~GuestView() {}
     69 
     70  private:
     71   DISALLOW_COPY_AND_ASSIGN(GuestView);
     72 };
     73 
     74 }  // namespace extensions
     75 
     76 #endif  // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_H_
     77