2016-01-19 21:09:01 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
|
|
|
// in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/views/layout_util.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "libcef/browser/views/layout_adapter.h"
|
|
|
|
|
|
|
|
#include "ui/views/view.h"
|
|
|
|
|
|
|
|
namespace layout_util {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Manages the association between views::View and CefLayout instances.
|
|
|
|
class UserData : public base::SupportsUserData::Data {
|
|
|
|
public:
|
|
|
|
static CefRefPtr<CefLayout> GetFor(const views::View* view) {
|
|
|
|
UserData* data = static_cast<UserData*>(view->GetUserData(UserDataKey()));
|
|
|
|
if (data)
|
|
|
|
return data->layout_;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign ownership of the underlying views::LayoutManager to |owner_view|.
|
|
|
|
// The views::View that owns the views::LayoutManager will gain a ref-counted
|
|
|
|
// reference to the CefLayout and the CefLayout will keep an unowned reference
|
|
|
|
// to the views::LayoutManager. Destruction of the views::View will release
|
|
|
|
// the reference to the CefLayout.
|
2017-05-17 11:29:28 +02:00
|
|
|
static void Assign(CefRefPtr<CefLayout> cef_layout, views::View* owner_view) {
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(owner_view);
|
|
|
|
DCHECK(cef_layout->IsValid());
|
|
|
|
|
|
|
|
views::LayoutManager* layout = CefLayoutAdapter::GetFor(cef_layout)->Get();
|
|
|
|
DCHECK(layout);
|
|
|
|
|
|
|
|
// The CefLayout previously associated with |owner_view|, if any, will be
|
|
|
|
// destroyed by this call.
|
2017-05-31 17:33:30 +02:00
|
|
|
owner_view->SetUserData(UserDataKey(),
|
|
|
|
base::WrapUnique(new UserData(cef_layout)));
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-31 17:33:30 +02:00
|
|
|
friend std::default_delete<UserData>;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit UserData(CefRefPtr<CefLayout> cef_layout) : layout_(cef_layout) {
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(layout_);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
~UserData() override { CefLayoutAdapter::GetFor(layout_)->Detach(); }
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
static void* UserDataKey() {
|
|
|
|
// We just need a unique constant. Use the address of a static that
|
|
|
|
// COMDAT folding won't touch in an optimizing linker.
|
|
|
|
static int data_key = 0;
|
|
|
|
return reinterpret_cast<void*>(&data_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefLayout> layout_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefRefPtr<CefLayout> GetFor(const views::View* view) {
|
|
|
|
return UserData::GetFor(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assign(CefRefPtr<CefLayout> layout, views::View* owner_view) {
|
|
|
|
return UserData::Assign(layout, owner_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace layout_util
|