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 "base/memory/scoped_vector.h"
11 #include "content/child/blink_platform_impl.h"
12 #include "content/child/child_process.h"
13 #include "content/common/child_process_messages.h"
14 #include "content/common/utility_messages.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/utility/content_utility_client.h"
17 #include "ipc/ipc_sync_channel.h"
18 #include "third_party/WebKit/public/web/WebKit.h"
20 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
21 #include "base/files/file_path.h"
22 #include "content/common/plugin_list.h"
29 template<typename SRC
, typename DEST
>
30 void ConvertVector(const SRC
& src
, DEST
* dest
) {
31 dest
->reserve(src
.size());
32 for (typename
SRC::const_iterator i
= src
.begin(); i
!= src
.end(); ++i
)
33 dest
->push_back(typename
DEST::value_type(*i
));
38 UtilityThreadImpl::UtilityThreadImpl()
39 : ChildThreadImpl(ChildThreadImpl::Options::Builder().Build()) {
43 UtilityThreadImpl::UtilityThreadImpl(const InProcessChildThreadParams
& params
)
44 : ChildThreadImpl(ChildThreadImpl::Options::Builder()
45 .InBrowserProcess(params
)
50 UtilityThreadImpl::~UtilityThreadImpl() {
53 void UtilityThreadImpl::Shutdown() {
54 ChildThreadImpl::Shutdown();
56 if (!IsInBrowserProcess())
60 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
64 if (IsInBrowserProcess()) {
65 // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
66 // to take a different code path than the multi-process case because that
67 // depends on the child process going away to close the channel, but that
68 // can't happen when we're in single process mode.
71 ChildProcess::current()->ReleaseProcess();
75 void UtilityThreadImpl::Init() {
77 ChildProcess::current()->AddRefProcess();
78 if (!IsInBrowserProcess()) {
79 // We can only initialize WebKit on one thread, and in single process mode
80 // we run the utility thread on separate thread. This means that if any code
81 // needs WebKit initialized in the utility process, they need to have
82 // another path to support single process mode.
83 blink_platform_impl_
.reset(new BlinkPlatformImpl
);
84 blink::initialize(blink_platform_impl_
.get());
86 GetContentClient()->utility()->UtilityThreadStarted();
87 GetContentClient()->utility()->RegisterMojoServices(service_registry());
90 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
91 if (GetContentClient()->utility()->OnMessageReceived(msg
))
95 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl
, msg
)
96 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started
, OnBatchModeStarted
)
97 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished
, OnBatchModeFinished
)
98 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
99 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins
, OnLoadPlugins
)
101 IPC_MESSAGE_UNHANDLED(handled
= false)
102 IPC_END_MESSAGE_MAP()
106 void UtilityThreadImpl::OnBatchModeStarted() {
110 void UtilityThreadImpl::OnBatchModeFinished() {
112 ReleaseProcessIfNeeded();
115 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
116 void UtilityThreadImpl::OnLoadPlugins(
117 const std::vector
<base::FilePath
>& plugin_paths
) {
118 PluginList
* plugin_list
= PluginList::Singleton();
120 std::vector
<WebPluginInfo
> plugins
;
121 // TODO(bauerb): If we restart loading plugins, we might mess up the logic in
122 // PluginList::ShouldLoadPlugin due to missing the previously loaded plugins
123 // in |plugin_groups|.
124 for (size_t i
= 0; i
< plugin_paths
.size(); ++i
) {
125 WebPluginInfo plugin
;
126 if (!plugin_list
->LoadPluginIntoPluginList(
127 plugin_paths
[i
], &plugins
, &plugin
))
128 Send(new UtilityHostMsg_LoadPluginFailed(i
, plugin_paths
[i
]));
130 Send(new UtilityHostMsg_LoadedPlugin(i
, plugin
));
133 ReleaseProcessIfNeeded();
137 } // namespace content