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/files/file_path.h"
11 #include "base/memory/scoped_vector.h"
12 #include "content/child/child_process.h"
13 #include "content/child/webkitplatformsupport_impl.h"
14 #include "content/common/child_process_messages.h"
15 #include "content/common/plugin_list.h"
16 #include "content/common/utility_messages.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/utility/content_utility_client.h"
19 #include "ipc/ipc_sync_channel.h"
20 #include "third_party/WebKit/public/web/WebKit.h"
26 template<typename SRC
, typename DEST
>
27 void ConvertVector(const SRC
& src
, DEST
* dest
) {
28 dest
->reserve(src
.size());
29 for (typename
SRC::const_iterator i
= src
.begin(); i
!= src
.end(); ++i
)
30 dest
->push_back(typename
DEST::value_type(*i
));
35 UtilityThreadImpl::UtilityThreadImpl() : single_process_(false) {
39 UtilityThreadImpl::UtilityThreadImpl(const std::string
& channel_name
)
40 : ChildThread(channel_name
),
41 single_process_(true) {
45 UtilityThreadImpl::~UtilityThreadImpl() {
48 void UtilityThreadImpl::Shutdown() {
49 ChildThread::Shutdown();
55 bool UtilityThreadImpl::Send(IPC::Message
* msg
) {
56 return ChildThread::Send(msg
);
59 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
63 if (single_process_
) {
64 // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
65 // to take a different code path than the multi-process case because that
66 // depends on the child process going away to close the channel, but that
67 // can't happen when we're in single process mode.
70 ChildProcess::current()->ReleaseProcess();
76 void UtilityThreadImpl::PreCacheFont(const LOGFONT
& log_font
) {
77 Send(new ChildProcessHostMsg_PreCacheFont(log_font
));
80 void UtilityThreadImpl::ReleaseCachedFonts() {
81 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
86 void UtilityThreadImpl::Init() {
88 ChildProcess::current()->AddRefProcess();
89 if (!single_process_
) {
90 // We can only initialize WebKit on one thread, and in single process mode
91 // we run the utility thread on separate thread. This means that if any code
92 // needs WebKit initialized in the utility process, they need to have
93 // another path to support single process mode.
94 webkit_platform_support_
.reset(new WebKitPlatformSupportImpl
);
95 blink::initialize(webkit_platform_support_
.get());
97 GetContentClient()->utility()->UtilityThreadStarted();
100 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
101 if (GetContentClient()->utility()->OnMessageReceived(msg
))
105 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl
, msg
)
106 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started
, OnBatchModeStarted
)
107 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished
, OnBatchModeFinished
)
108 #if defined(OS_POSIX)
109 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins
, OnLoadPlugins
)
111 IPC_MESSAGE_UNHANDLED(handled
= false)
112 IPC_END_MESSAGE_MAP()
116 void UtilityThreadImpl::OnBatchModeStarted() {
120 void UtilityThreadImpl::OnBatchModeFinished() {
122 ReleaseProcessIfNeeded();
125 #if defined(OS_POSIX)
126 void UtilityThreadImpl::OnLoadPlugins(
127 const std::vector
<base::FilePath
>& plugin_paths
) {
128 PluginList
* plugin_list
= PluginList::Singleton();
130 std::vector
<WebPluginInfo
> plugins
;
131 // TODO(bauerb): If we restart loading plug-ins, we might mess up the logic in
132 // PluginList::ShouldLoadPlugin due to missing the previously loaded plug-ins
133 // in |plugin_groups|.
134 for (size_t i
= 0; i
< plugin_paths
.size(); ++i
) {
135 WebPluginInfo plugin
;
136 if (!plugin_list
->LoadPluginIntoPluginList(
137 plugin_paths
[i
], &plugins
, &plugin
))
138 Send(new UtilityHostMsg_LoadPluginFailed(i
, plugin_paths
[i
]));
140 Send(new UtilityHostMsg_LoadedPlugin(i
, plugin
));
143 ReleaseProcessIfNeeded();
147 } // namespace content