1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be found 3 // in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_VIEWS_LAYOUT_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_VIEWS_LAYOUT_IMPL_H_ 7 #pragma once 8 9 #include "include/views/cef_box_layout.h" 10 #include "include/views/cef_fill_layout.h" 11 #include "include/views/cef_layout.h" 12 13 #include "libcef/browser/thread_util.h" 14 #include "libcef/browser/views/layout_adapter.h" 15 #include "libcef/browser/views/layout_util.h" 16 17 #include "base/logging.h" 18 #include "ui/views/layout/layout_manager.h" 19 #include "ui/views/view.h" 20 21 // Base template for implementing CefLayout-derived classes. See comments in 22 // view_impl.h for a usage overview. 23 template <class ViewsLayoutClass, class CefLayoutClass> 24 class CefLayoutImpl : public CefLayoutAdapter, public CefLayoutClass { 25 public: 26 // Returns the underlying views::LayoutManager object as the derived type. 27 // Does not transfer ownership. layout()28 ViewsLayoutClass* layout() const { return layout_ref_; } 29 30 // Returns the views::View that owns this object. owner_view()31 views::View* owner_view() const { return owner_view_; } 32 33 // CefLayoutAdapter methods: Get()34 views::LayoutManager* Get() const override { return layout(); } Detach()35 void Detach() override { 36 owner_view_ = nullptr; 37 layout_ref_ = nullptr; 38 } 39 40 // CefLayout methods. When adding new As*() methods make sure to update 41 // CefLayoutAdapter::GetFor() in layout_adapter.cc. AsBoxLayout()42 CefRefPtr<CefBoxLayout> AsBoxLayout() override { return nullptr; } AsFillLayout()43 CefRefPtr<CefFillLayout> AsFillLayout() override { return nullptr; } IsValid()44 bool IsValid() override { 45 CEF_REQUIRE_UIT_RETURN(false); 46 return !!layout_ref_; 47 } 48 49 protected: 50 // Create a new implementation object. 51 // Always call Initialize() after creation. CefLayoutImpl()52 CefLayoutImpl() : layout_ref_(nullptr), owner_view_(nullptr) {} 53 54 // Initialize this object and assign ownership to |owner_view|. Initialize(views::View * owner_view)55 void Initialize(views::View* owner_view) { 56 DCHECK(owner_view); 57 owner_view_ = owner_view; 58 layout_ref_ = CreateLayout(); 59 DCHECK(layout_ref_); 60 owner_view->SetLayoutManager(base::WrapUnique(layout_ref_)); 61 layout_util::Assign(this, owner_view); 62 } 63 64 // Create the views::LayoutManager object. 65 virtual ViewsLayoutClass* CreateLayout() = 0; 66 67 private: 68 // Unowned reference to the views::LayoutManager wrapped by this object. Will 69 // be nullptr after the views::LayoutManager is destroyed. 70 ViewsLayoutClass* layout_ref_; 71 72 // Unowned reference to the views::View that owns this object. Will be nullptr 73 // after the views::LayoutManager is destroyed. 74 views::View* owner_view_; 75 }; 76 77 #endif // CEF_LIBCEF_BROWSER_VIEWS_LAYOUT_IMPL_H_ 78