1 // Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "libcef/browser/extension_impl.h"
6
7 #include "libcef/browser/request_context_impl.h"
8 #include "libcef/browser/thread_util.h"
9 #include "libcef/common/values_impl.h"
10
11 #include "extensions/common/extension.h"
12
CefExtensionImpl(const extensions::Extension * extension,CefRequestContext * loader_context,CefRefPtr<CefExtensionHandler> handler)13 CefExtensionImpl::CefExtensionImpl(const extensions::Extension* extension,
14 CefRequestContext* loader_context,
15 CefRefPtr<CefExtensionHandler> handler)
16 : id_(extension->id()),
17 path_(extension->path().value()),
18 manifest_(
19 new CefDictionaryValueImpl(extension->manifest()->value()->DeepCopy(),
20 true,
21 true)),
22 loader_context_(loader_context),
23 handler_(handler) {}
24
GetIdentifier()25 CefString CefExtensionImpl::GetIdentifier() {
26 return id_;
27 }
28
GetPath()29 CefString CefExtensionImpl::GetPath() {
30 return path_;
31 }
32
GetManifest()33 CefRefPtr<CefDictionaryValue> CefExtensionImpl::GetManifest() {
34 return manifest_;
35 }
36
IsSame(CefRefPtr<CefExtension> that)37 bool CefExtensionImpl::IsSame(CefRefPtr<CefExtension> that) {
38 CefExtensionImpl* that_impl = static_cast<CefExtensionImpl*>(that.get());
39 if (!that_impl)
40 return false;
41
42 // Maybe the same object.
43 if (this == that_impl)
44 return true;
45
46 return id_ == that_impl->id_ && path_ == that_impl->path_ &&
47 loader_context_ == that_impl->loader_context_;
48 }
49
GetHandler()50 CefRefPtr<CefExtensionHandler> CefExtensionImpl::GetHandler() {
51 return handler_;
52 }
53
GetLoaderContext()54 CefRefPtr<CefRequestContext> CefExtensionImpl::GetLoaderContext() {
55 if (!CEF_CURRENTLY_ON_UIT()) {
56 NOTREACHED() << "called on invalid thread";
57 return nullptr;
58 }
59
60 return loader_context_;
61 }
62
IsLoaded()63 bool CefExtensionImpl::IsLoaded() {
64 if (!CEF_CURRENTLY_ON_UIT()) {
65 NOTREACHED() << "called on invalid thread";
66 return false;
67 }
68
69 return !unloaded_;
70 }
71
Unload()72 void CefExtensionImpl::Unload() {
73 if (!CEF_CURRENTLY_ON_UIT()) {
74 CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefExtensionImpl::Unload, this));
75 return;
76 }
77
78 // Will be NULL for internal extensions. They can't be unloaded.
79 if (!loader_context_)
80 return;
81
82 if (unloaded_)
83 return;
84
85 // CefExtensionHandler callbacks triggered by UnloadExtension may check this
86 // flag, so set it here.
87 unloaded_ = true;
88
89 const bool result = static_cast<CefRequestContextImpl*>(loader_context_)
90 ->GetBrowserContext()
91 ->UnloadExtension(id_);
92 ALLOW_UNUSED_LOCAL(result);
93 DCHECK(result);
94 }
95
OnExtensionLoaded()96 void CefExtensionImpl::OnExtensionLoaded() {
97 CEF_REQUIRE_UIT();
98 if (handler_)
99 handler_->OnExtensionLoaded(this);
100 }
101
OnExtensionUnloaded()102 void CefExtensionImpl::OnExtensionUnloaded() {
103 CEF_REQUIRE_UIT();
104 // Should not be called for internal extensions.
105 DCHECK(loader_context_);
106
107 unloaded_ = true;
108 loader_context_ = nullptr;
109
110 if (handler_)
111 handler_->OnExtensionUnloaded(this);
112 }
113