1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/utility/utility_thread_impl.h"
9 #include "base/command_line.h"
10 #include "content/child/blink_platform_impl.h"
11 #include "content/child/child_process.h"
12 #include "content/common/child_process_messages.h"
13 #include "content/common/utility_messages.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/utility/content_utility_client.h"
16 #include "content/utility/utility_blink_platform_impl.h"
17 #include "content/utility/utility_process_control_impl.h"
18 #include "ipc/ipc_sync_channel.h"
19 #include "third_party/WebKit/public/web/WebKit.h"
21 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
22 #include "base/files/file_path.h"
23 #include "content/common/plugin_list.h"
30 template<typename SRC
, typename DEST
>
31 void ConvertVector(const SRC
& src
, DEST
* dest
) {
32 dest
->reserve(src
.size());
33 for (typename
SRC::const_iterator i
= src
.begin(); i
!= src
.end(); ++i
)
34 dest
->push_back(typename
DEST::value_type(*i
));
39 UtilityThreadImpl::UtilityThreadImpl()
40 : ChildThreadImpl(ChildThreadImpl::Options::Builder().Build()) {
44 UtilityThreadImpl::UtilityThreadImpl(const InProcessChildThreadParams
& params
)
45 : ChildThreadImpl(ChildThreadImpl::Options::Builder()
46 .InBrowserProcess(params
)
51 UtilityThreadImpl::~UtilityThreadImpl() {
54 void UtilityThreadImpl::Shutdown() {
55 ChildThreadImpl::Shutdown();
57 if (blink_platform_impl_
)
58 blink::shutdownWithoutV8();
61 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
65 if (IsInBrowserProcess()) {
66 // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
67 // to take a different code path than the multi-process case because that
68 // depends on the child process going away to close the channel, but that
69 // can't happen when we're in single process mode.
72 ChildProcess::current()->ReleaseProcess();
76 void UtilityThreadImpl::EnsureBlinkInitialized() {
77 if (blink_platform_impl_
|| IsInBrowserProcess()) {
78 // We can only initialize WebKit on one thread, and in single process mode
79 // we run the utility thread on separate thread. This means that if any code
80 // needs WebKit initialized in the utility process, they need to have
81 // another path to support single process mode.
85 blink_platform_impl_
.reset(new UtilityBlinkPlatformImpl
);
86 blink::initializeWithoutV8(blink_platform_impl_
.get());
89 void UtilityThreadImpl::Init() {
91 ChildProcess::current()->AddRefProcess();
92 GetContentClient()->utility()->UtilityThreadStarted();
94 process_control_
.reset(new UtilityProcessControlImpl
);
95 service_registry()->AddService(base::Bind(
96 &UtilityThreadImpl::BindProcessControlRequest
, base::Unretained(this)));
98 GetContentClient()->utility()->RegisterMojoServices(service_registry());
101 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
102 if (GetContentClient()->utility()->OnMessageReceived(msg
))
106 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl
, msg
)
107 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started
, OnBatchModeStarted
)
108 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished
, OnBatchModeFinished
)
109 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
110 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins
, OnLoadPlugins
)
112 IPC_MESSAGE_UNHANDLED(handled
= false)
113 IPC_END_MESSAGE_MAP()
117 void UtilityThreadImpl::OnBatchModeStarted() {
121 void UtilityThreadImpl::OnBatchModeFinished() {
123 ReleaseProcessIfNeeded();
126 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
127 void UtilityThreadImpl::OnLoadPlugins(
128 const std::vector
<base::FilePath
>& plugin_paths
) {
129 PluginList
* plugin_list
= PluginList::Singleton();
131 std::vector
<WebPluginInfo
> plugins
;
132 // TODO(bauerb): If we restart loading plugins, we might mess up the logic in
133 // PluginList::ShouldLoadPlugin due to missing the previously loaded plugins
134 // in |plugin_groups|.
135 for (size_t i
= 0; i
< plugin_paths
.size(); ++i
) {
136 WebPluginInfo plugin
;
137 if (!plugin_list
->LoadPluginIntoPluginList(
138 plugin_paths
[i
], &plugins
, &plugin
))
139 Send(new UtilityHostMsg_LoadPluginFailed(i
, plugin_paths
[i
]));
141 Send(new UtilityHostMsg_LoadedPlugin(i
, plugin
));
144 ReleaseProcessIfNeeded();
148 void UtilityThreadImpl::BindProcessControlRequest(
149 mojo::InterfaceRequest
<ProcessControl
> request
) {
150 DCHECK(process_control_
);
151 process_control_bindings_
.AddBinding(process_control_
.get(), request
.Pass());
154 } // namespace content