-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathnw_content_browser_hooks.cc
More file actions
224 lines (182 loc) · 6.42 KB
/
Copy pathnw_content_browser_hooks.cc
File metadata and controls
224 lines (182 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "nw_content_browser_hooks.h"
// base
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
// content
#include "content/public/browser/child_process_security_policy.h"
//#include "content/public/browser/notification_service.h"
//#include "content/public/browser/notification_types.h"
// #include "content/public/browser/plugin_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/renderer/v8_value_converter.h"
#include "content/public/browser/child_process_termination_info.h"
// content/nw
#include "content/nw/src/nw_base.h"
#include "content/nw/src/common/shell_switches.h"
#include "net/cert/x509_certificate.h"
// ui
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/codec/png_codec.h"
// gen
#include "nw/id/commit.h"
#include "v8/include/v8-callbacks.h"
#include "third_party/node-nw/src/node_webkit.h"
#if defined(OS_WIN)
#define _USE_MATH_DEFINES
#include <math.h>
#endif
using content::RenderProcessHost;
namespace content {
class WebContents;
}
namespace nw {
namespace {
bool g_pinning_renderer = true;
bool g_mixed_context = false;
bool g_in_webview_apply_attr = false;
bool g_in_webview_apply_attr_allow_nw = false;
RelaunchCallback g_relaunch_on_shutdown = nullptr;
} //namespace
#if defined(OS_MAC)
typedef void (*SendEventToAppFn)(const std::string& event_name, const base::ListValue& event_args);
CONTENT_EXPORT SendEventToAppFn gSendEventToApp = nullptr;
bool GetDirUserData(base::FilePath*);
void SetTrustAnchors(net::CertificateList&);
#endif
// browser
bool GetPackageImage(nw::Package* package,
const FilePath& icon_path,
gfx::Image* image) {
FilePath path = package->ConvertToAbsoutePath(icon_path);
if (path.empty())
return false;
// Read the file from disk.
std::optional<std::vector<uint8_t>> file_contents =
base::ReadFileToBytes(path);
if (!file_contents)
return false;
// Note: This class only decodes bitmaps from extension resources. Chrome
// doesn't (for security reasons) directly load extension resources provided
// by the extension author, but instead decodes them in a separate
// locked-down utility process. Only if the decoding succeeds is the image
// saved from memory to disk and subsequently used in the Chrome UI.
// Chrome is therefore decoding bitmaps here that were generated by Chrome.
SkBitmap bitmap = gfx::PNGCodec::Decode(file_contents.value());
if (bitmap.isNull())
return false; // Unable to decode.
*image = gfx::Image::CreateFrom1xBitmap(bitmap);
return true;
}
void MainPartsPostDestroyThreadsHook() {
if (g_relaunch_on_shutdown)
g_relaunch_on_shutdown();
ReleaseNWPackage();
}
void ScheduleRelaunchOnShutdown(RelaunchCallback relaunch) {
g_relaunch_on_shutdown = relaunch;
}
void RendererProcessTerminatedHook(content::RenderProcessHost* process,
const content::ChildProcessTerminationInfo& info) {
int exit_code = info.exit_code;
#if defined(OS_POSIX)
if (WIFEXITED(exit_code))
exit_code = WEXITSTATUS(exit_code);
#endif
SetExitCode(exit_code);
}
bool GetImage(Package* package, const FilePath& icon_path, gfx::Image* image) {
FilePath path = package->ConvertToAbsoutePath(icon_path);
if (path.empty())
return false;
// Read the file from disk.
std::optional<std::vector<uint8_t>> file_contents =
base::ReadFileToBytes(path);
if (!file_contents)
return false;
// Note: This class only decodes bitmaps from extension resources. Chrome
// doesn't (for security reasons) directly load extension resources provided
// by the extension author, but instead decodes them in a separate
// locked-down utility process. Only if the decoding succeeds is the image
// saved from memory to disk and subsequently used in the Chrome UI.
// Chrome is therefore decoding bitmaps here that were generated by Chrome.
SkBitmap bitmap = gfx::PNGCodec::Decode(file_contents.value());
if (bitmap.isNull())
return false; // Unable to decode.
*image = gfx::Image::CreateFrom1xBitmap(bitmap);
return true;
}
#if defined(OS_MAC)
CONTENT_EXPORT bool ApplicationShouldHandleReopenHook(bool hasVisibleWindows) {
base::ListValue arguments;
if (gSendEventToApp)
gSendEventToApp("nw.App.onReopen", arguments);
return true;
}
CONTENT_EXPORT void OSXOpenURLsHook(const std::vector<GURL>& startup_urls) {
base::ListValue arguments;
for (size_t i = 0; i < startup_urls.size(); i++)
arguments.Append(startup_urls[i].spec());
if (gSendEventToApp)
gSendEventToApp("nw.App.onOpen", arguments);
}
#endif
void OverrideWebkitPrefsHook(content::WebContents* web_contents, blink::web_pref::WebPreferences* web_prefs) {
nw::Package* package = nw::package();
if (!package)
return;
base::DictValue* webkit = package->root()->FindDict(switches::kmWebkit);
web_prefs->plugins_enabled = true;
if (webkit) {
std::optional<bool> flag = webkit->FindBool("double_tap_to_zoom_enabled");
if (flag)
web_prefs->double_tap_to_zoom_enabled = *flag;
flag = webkit->FindBool("plugin");
if (flag)
web_prefs->plugins_enabled = *flag;
}
#if 0
FilePath plugins_dir = package->path();
plugins_dir = plugins_dir.AppendASCII("plugins");
content::PluginService* plugin_service = content::PluginService::GetInstance();
plugin_service->AddExtraPluginDir(plugins_dir);
#endif
}
#if 0
bool ShouldServiceRequestHook(int child_id, const GURL& url) {
RenderProcessHost* rph = RenderProcessHost::FromID(child_id);
if (!rph)
return false;
return RphGuestFilterURLHook(rph, &url);
}
#endif
bool PinningRenderer() {
return g_pinning_renderer;
}
void SetPinningRenderer(bool pin) {
g_pinning_renderer = pin;
}
bool MixedContext() {
return g_mixed_context;
}
void SetMixedContext(bool mixed) {
g_mixed_context = mixed;
}
void SetInWebViewApplyAttr(bool flag, bool allow_nw) {
g_in_webview_apply_attr = flag;
g_in_webview_apply_attr_allow_nw = allow_nw;
}
bool GetInWebViewApplyAttr(bool* allow_nw) {
if (allow_nw)
*allow_nw = g_in_webview_apply_attr_allow_nw;
return g_in_webview_apply_attr;
}
} //namespace nw