forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_content_browser_client.cc
More file actions
304 lines (267 loc) · 11.8 KB
/
shell_content_browser_client.cc
File metadata and controls
304 lines (267 loc) · 11.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2012 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "content/nw/src/shell_content_browser_client.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "content/public/browser/browser_url_handler.h"
#include "content/nw/src/browser/printing/printing_message_filter.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_dispatcher_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/renderer_preferences.h"
#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/common/shell_switches.h"
#include "content/nw/src/browser/printing/print_job_manager.h"
#include "content/nw/src/browser/shell_devtools_delegate.h"
#include "content/nw/src/browser/shell_resource_dispatcher_host_delegate.h"
#include "content/nw/src/media/media_internals.h"
#include "content/nw/src/nw_package.h"
#include "content/nw/src/nw_shell.h"
#include "content/nw/src/nw_version.h"
#include "content/nw/src/shell_browser_context.h"
#include "content/nw/src/shell_browser_main_parts.h"
#include "geolocation/shell_access_token_store.h"
#include "googleurl/src/gurl.h"
#include "webkit/glue/webpreferences.h"
#include "webkit/user_agent/user_agent_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "webkit/plugins/npapi/plugin_list.h"
namespace content {
ShellContentBrowserClient::ShellContentBrowserClient()
: shell_browser_main_parts_(NULL),
master_rph_(NULL) {
}
ShellContentBrowserClient::~ShellContentBrowserClient() {
}
BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
const MainFunctionParams& parameters) {
shell_browser_main_parts_ = new ShellBrowserMainParts(parameters);
return shell_browser_main_parts_;
}
WebContentsViewPort* ShellContentBrowserClient::OverrideCreateWebContentsView(
WebContents* web_contents,
RenderViewHostDelegateView** render_view_host_delegate_view) {
std::string user_agent, rules;
nw::Package* package = shell_browser_main_parts()->package();
content::RendererPreferences* prefs = web_contents->GetMutableRendererPrefs();
if (package->root()->GetString(switches::kmUserAgent, &user_agent)) {
std::string name, version;
package->root()->GetString(switches::kmName, &name);
package->root()->GetString("version", &version);
ReplaceSubstringsAfterOffset(&user_agent, 0, "%name", name);
ReplaceSubstringsAfterOffset(&user_agent, 0, "%ver", version);
ReplaceSubstringsAfterOffset(&user_agent, 0, "%nwver", NW_VERSION_STRING);
ReplaceSubstringsAfterOffset(&user_agent, 0, "%webkit_ver", webkit_glue::GetWebKitVersion());
ReplaceSubstringsAfterOffset(&user_agent, 0, "%osinfo", webkit_glue::BuildOSInfo());
prefs->user_agent_override = user_agent;
}
if (package->root()->GetString(switches::kmRemotePages, &rules))
prefs->nw_remote_page_rules = rules;
return NULL;
}
void ShellContentBrowserClient::RenderViewHostCreated(
RenderViewHost* render_view_host) {
new api::DispatcherHost(render_view_host);
}
std::string ShellContentBrowserClient::GetApplicationLocale() {
return l10n_util::GetApplicationLocale("en-US");
}
void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
CommandLine* command_line,
int child_process_id) {
if (command_line->GetSwitchValueASCII("type") != "renderer")
return;
if (child_process_id > 0) {
content::RenderProcessHost* rph =
content::RenderProcessHost::FromID(child_process_id);
content::RenderProcessHost::RenderWidgetHostsIterator iter(
rph->GetRenderWidgetHostsIterator());
for (; !iter.IsAtEnd(); iter.Advance()) {
const content::RenderWidgetHost* widget = iter.GetCurrentValue();
DCHECK(widget);
if (!widget || !widget->IsRenderView())
continue;
content::RenderViewHost* host = content::RenderViewHost::From(
const_cast<content::RenderWidgetHost*>(widget));
content::Shell* shell = content::Shell::FromRenderViewHost(host);
if (shell && (shell->is_devtools() || !shell->nodejs()))
return;
}
}
nw::Package* package = shell_browser_main_parts()->package();
if (package && package->GetUseNode()) {
// Allow node.js
command_line->AppendSwitch(switches::kNodejs);
// Set cwd
command_line->AppendSwitchPath(switches::kWorkingDirectory,
package->path());
// Check if we have 'node-main'.
std::string node_main;
if (package->root()->GetString(switches::kNodeMain, &node_main))
command_line->AppendSwitchASCII(switches::kNodeMain, node_main);
std::string snapshot_path;
if (package->root()->GetString(switches::kSnapshot, &snapshot_path))
command_line->AppendSwitchASCII(switches::kSnapshot, snapshot_path);
}
// without the switch, the destructor of the shell object will
// shutdown renderwidgethost (RenderWidgetHostImpl::Shutdown) and
// destory rph immediately. Then the channel error msg is caught by
// SuicideOnChannelErrorFilter and the renderer is killed
// immediately
#if defined(OS_POSIX)
command_line->AppendSwitch(switches::kChildCleanExit);
#endif
}
void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
resource_dispatcher_host_delegate_.reset(
new ShellResourceDispatcherHostDelegate());
ResourceDispatcherHost::Get()->SetDelegate(
resource_dispatcher_host_delegate_.get());
}
std::string ShellContentBrowserClient::GetDefaultDownloadName() {
return "download";
}
MediaObserver* ShellContentBrowserClient::GetMediaObserver() {
return MediaInternals::GetInstance();
}
void ShellContentBrowserClient::BrowserURLHandlerCreated(
BrowserURLHandler* handler) {
}
ShellBrowserContext* ShellContentBrowserClient::browser_context() {
return shell_browser_main_parts_->browser_context();
}
ShellBrowserContext*
ShellContentBrowserClient::off_the_record_browser_context() {
return shell_browser_main_parts_->off_the_record_browser_context();
}
AccessTokenStore* ShellContentBrowserClient::CreateAccessTokenStore() {
return new ShellAccessTokenStore(browser_context()->GetRequestContext());
}
void ShellContentBrowserClient::OverrideWebkitPrefs(
RenderViewHost* render_view_host,
const GURL& url,
webkit_glue::WebPreferences* prefs) {
nw::Package* package = shell_browser_main_parts()->package();
// Disable web security.
prefs->dom_paste_enabled = true;
prefs->javascript_can_access_clipboard = true;
prefs->web_security_enabled = true;
prefs->allow_file_access_from_file_urls = true;
// Open experimental features.
prefs->css_sticky_position_enabled = true;
prefs->css_shaders_enabled = true;
prefs->css_variables_enabled = true;
// Disable plugins and cache by default.
prefs->plugins_enabled = false;
prefs->java_enabled = false;
prefs->uses_page_cache = false;
base::DictionaryValue* webkit;
if (package->root()->GetDictionary(switches::kmWebkit, &webkit)) {
webkit->GetBoolean(switches::kmJava, &prefs->java_enabled);
webkit->GetBoolean(switches::kmPlugin, &prefs->plugins_enabled);
webkit->GetBoolean(switches::kmPageCache, &prefs->uses_page_cache);
FilePath plugins_dir = package->path();
//PathService::Get(base::DIR_CURRENT, &plugins_dir);
plugins_dir = plugins_dir.AppendASCII("plugins");
webkit::npapi::PluginList::Singleton()->AddExtraPluginDir(plugins_dir);
}
}
bool ShellContentBrowserClient::ShouldTryToUseExistingProcessHost(
BrowserContext* browser_context, const GURL& url) {
ShellBrowserContext* shell_browser_context =
static_cast<ShellBrowserContext*>(browser_context);
if (shell_browser_context->pinning_renderer())
return true;
else
return false;
}
bool ShellContentBrowserClient::IsSuitableHost(RenderProcessHost* process_host,
const GURL& site_url) {
return process_host == master_rph_;
}
net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
BrowserContext* content_browser_context,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
blob_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
file_system_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
developer_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
chrome_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
chrome_devtools_protocol_handler) {
ShellBrowserContext* shell_browser_context =
ShellBrowserContextForBrowserContext(content_browser_context);
return shell_browser_context->CreateRequestContext(
blob_protocol_handler.Pass(), file_system_protocol_handler.Pass(),
developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(),
chrome_devtools_protocol_handler.Pass());
}
net::URLRequestContextGetter*
ShellContentBrowserClient::CreateRequestContextForStoragePartition(
BrowserContext* content_browser_context,
const base::FilePath& partition_path,
bool in_memory,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
blob_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
file_system_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
developer_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
chrome_protocol_handler,
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
chrome_devtools_protocol_handler) {
ShellBrowserContext* shell_browser_context =
ShellBrowserContextForBrowserContext(content_browser_context);
return shell_browser_context->CreateRequestContextForStoragePartition(
partition_path, in_memory, blob_protocol_handler.Pass(),
file_system_protocol_handler.Pass(),
developer_protocol_handler.Pass(), chrome_protocol_handler.Pass(),
chrome_devtools_protocol_handler.Pass());
}
ShellBrowserContext*
ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
BrowserContext* content_browser_context) {
if (content_browser_context == browser_context())
return browser_context();
DCHECK_EQ(content_browser_context, off_the_record_browser_context());
return off_the_record_browser_context();
}
printing::PrintJobManager* ShellContentBrowserClient::print_job_manager() {
return shell_browser_main_parts_->print_job_manager();
}
void ShellContentBrowserClient::RenderProcessHostCreated(
RenderProcessHost* host) {
int id = host->GetID();
if (!master_rph_)
master_rph_ = host;
#if defined(ENABLE_PRINTING)
host->GetChannel()->AddFilter(new PrintingMessageFilter(id));
#endif
}
} // namespace content