2017-08-04 00:55:19 +02:00
|
|
|
// 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 "libcef/browser/extension_impl.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/request_context_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "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()),
|
2023-01-30 22:42:40 +01:00
|
|
|
manifest_(
|
|
|
|
new CefDictionaryValueImpl(extension->manifest()->value()->Clone(),
|
|
|
|
/*read_only=*/true)),
|
2017-08-04 00:55:19 +02:00
|
|
|
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) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
// Maybe the same object.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (this == that_impl) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02: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()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2017-08-04 00:55:19 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loader_context_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefExtensionImpl::IsLoaded() {
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2017-08-04 00:55:19 +02:00
|
|
|
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_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (unloaded_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
// CefExtensionHandler callbacks triggered by UnloadExtension may check this
|
|
|
|
// flag, so set it here.
|
|
|
|
unloaded_ = true;
|
|
|
|
|
2022-02-21 23:23:40 +01:00
|
|
|
[[maybe_unused]] const bool result =
|
|
|
|
static_cast<CefRequestContextImpl*>(loader_context_)
|
|
|
|
->GetBrowserContext()
|
|
|
|
->UnloadExtension(id_);
|
2017-08-04 00:55:19 +02:00
|
|
|
DCHECK(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefExtensionImpl::OnExtensionLoaded() {
|
|
|
|
CEF_REQUIRE_UIT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handler_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
handler_->OnExtensionLoaded(this);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02: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_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
handler_->OnExtensionUnloaded(this);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|