cef/libcef/browser/extension_impl.cc

117 lines
2.9 KiB
C++
Raw Normal View History

// Copyright (c) 2017 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 "cef/libcef/browser/extension_impl.h"
#include "cef/libcef/browser/request_context_impl.h"
#include "cef/libcef/browser/thread_util.h"
#include "cef/libcef/common/values_impl.h"
#include "extensions/common/extension.h"
CefExtensionImpl::CefExtensionImpl(const extensions::Extension* extension,
CefRequestContext* loader_context,
CefRefPtr<CefExtensionHandler> handler)
: id_(extension->id()),
path_(extension->path().value()),
manifest_(
new CefDictionaryValueImpl(extension->manifest()->value()->Clone(),
/*read_only=*/true)),
loader_context_(loader_context),
handler_(handler) {}
CefString CefExtensionImpl::GetIdentifier() {
return id_;
}
CefString CefExtensionImpl::GetPath() {
return path_;
}
CefRefPtr<CefDictionaryValue> CefExtensionImpl::GetManifest() {
return manifest_;
}
bool CefExtensionImpl::IsSame(CefRefPtr<CefExtension> that) {
CefExtensionImpl* that_impl = static_cast<CefExtensionImpl*>(that.get());
2023-01-02 23:59:03 +01:00
if (!that_impl) {
return false;
2023-01-02 23:59:03 +01:00
}
// Maybe the same object.
2023-01-02 23:59:03 +01:00
if (this == that_impl) {
return true;
2023-01-02 23:59:03 +01:00
}
return id_ == that_impl->id_ && path_ == that_impl->path_ &&
loader_context_ == that_impl->loader_context_;
}
CefRefPtr<CefExtensionHandler> CefExtensionImpl::GetHandler() {
return handler_;
}
CefRefPtr<CefRequestContext> CefExtensionImpl::GetLoaderContext() {
if (!CEF_CURRENTLY_ON_UIT()) {
DCHECK(false) << "called on invalid thread";
return nullptr;
}
return loader_context_;
}
bool CefExtensionImpl::IsLoaded() {
if (!CEF_CURRENTLY_ON_UIT()) {
DCHECK(false) << "called on invalid thread";
return false;
}
return !unloaded_;
}
void CefExtensionImpl::Unload() {
if (!CEF_CURRENTLY_ON_UIT()) {
CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefExtensionImpl::Unload, this));
return;
}
// Will be NULL for internal extensions. They can't be unloaded.
2023-01-02 23:59:03 +01:00
if (!loader_context_) {
return;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (unloaded_) {
return;
2023-01-02 23:59:03 +01:00
}
// CefExtensionHandler callbacks triggered by UnloadExtension may check this
// flag, so set it here.
unloaded_ = true;
[[maybe_unused]] const bool result =
static_cast<CefRequestContextImpl*>(loader_context_)
->GetBrowserContext()
->UnloadExtension(id_);
DCHECK(result);
}
void CefExtensionImpl::OnExtensionLoaded() {
CEF_REQUIRE_UIT();
2023-01-02 23:59:03 +01:00
if (handler_) {
handler_->OnExtensionLoaded(this);
2023-01-02 23:59:03 +01:00
}
}
void CefExtensionImpl::OnExtensionUnloaded() {
CEF_REQUIRE_UIT();
// Should not be called for internal extensions.
DCHECK(loader_context_);
unloaded_ = true;
loader_context_ = nullptr;
2023-01-02 23:59:03 +01:00
if (handler_) {
handler_->OnExtensionUnloaded(this);
2023-01-02 23:59:03 +01:00
}
}