guest_view.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
  5. #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_
  6. #include "components/guest_view/browser/guest_view_base.h"
  7. #include "components/guest_view/browser/guest_view_manager.h"
  8. #include "content/public/browser/render_frame_host.h"
  9. namespace guest_view {
  10. // A GuestView is the templated base class for out-of-process frames in the
  11. // chrome layer. GuestView is templated on its derived type to allow for type-
  12. // safe access. See GuestViewBase for more information.
  13. template <typename T>
  14. class GuestView : public GuestViewBase {
  15. public:
  16. static T* From(int embedder_process_id, int guest_instance_id) {
  17. return AsDerivedGuest(
  18. GuestViewBase::From(embedder_process_id, guest_instance_id));
  19. }
  20. static T* FromWebContents(const content::WebContents* contents) {
  21. return AsDerivedGuest(GuestViewBase::FromWebContents(contents));
  22. }
  23. static T* FromFrameID(int render_process_id, int render_frame_id) {
  24. auto* render_frame_host =
  25. content::RenderFrameHost::FromID(render_process_id, render_frame_id);
  26. if (!render_frame_host)
  27. return nullptr;
  28. auto* web_contents =
  29. content::WebContents::FromRenderFrameHost(render_frame_host);
  30. return FromWebContents(web_contents);
  31. }
  32. GuestView(const GuestView&) = delete;
  33. GuestView& operator=(const GuestView&) = delete;
  34. // GuestViewBase implementation.
  35. const char* GetViewType() const final {
  36. return T::Type;
  37. }
  38. protected:
  39. explicit GuestView(content::WebContents* owner_web_contents)
  40. : GuestViewBase(owner_web_contents) {}
  41. ~GuestView() override {}
  42. T* GetOpener() const { return AsDerivedGuest(GuestViewBase::GetOpener()); }
  43. void SetOpener(T* opener) { GuestViewBase::SetOpener(opener); }
  44. private:
  45. // Downcasts to a *ViewGuest if the GuestViewBase is of the derived view type.
  46. // Otherwise, returns nullptr.
  47. static T* AsDerivedGuest(GuestViewBase* guest) {
  48. if (!guest)
  49. return nullptr;
  50. const bool same_type = !strcmp(guest->GetViewType(), T::Type);
  51. if (!same_type)
  52. return nullptr;
  53. return static_cast<T*>(guest);
  54. }
  55. };
  56. } // namespace guest_view
  57. #endif // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_H_