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"
22 #if defined(TOOLKIT_GTK)
25 #include "ui/gfx/gtk_util.h"
32 template<typename SRC
, typename DEST
>
33 void ConvertVector(const SRC
& src
, DEST
* dest
) {
34 dest
->reserve(src
.size());
35 for (typename
SRC::const_iterator i
= src
.begin(); i
!= src
.end(); ++i
)
36 dest
->push_back(typename
DEST::value_type(*i
));
41 UtilityThreadImpl::UtilityThreadImpl() : single_process_(false) {
45 UtilityThreadImpl::UtilityThreadImpl(const std::string
& channel_name
)
46 : ChildThread(channel_name
),
47 single_process_(true) {
51 UtilityThreadImpl::~UtilityThreadImpl() {
54 void UtilityThreadImpl::Shutdown() {
55 ChildThread::Shutdown();
61 bool UtilityThreadImpl::Send(IPC::Message
* msg
) {
62 return ChildThread::Send(msg
);
65 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
69 if (single_process_
) {
70 // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
71 // to take a different code path than the multi-process case because that
72 // depends on the child process going away to close the channel, but that
73 // can't happen when we're in single process mode.
76 ChildProcess::current()->ReleaseProcess();
82 void UtilityThreadImpl::PreCacheFont(const LOGFONT
& log_font
) {
83 Send(new ChildProcessHostMsg_PreCacheFont(log_font
));
86 void UtilityThreadImpl::ReleaseCachedFonts() {
87 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
92 void UtilityThreadImpl::Init() {
94 ChildProcess::current()->AddRefProcess();
95 if (!single_process_
) {
96 // We can only initialize WebKit on one thread, and in single process mode
97 // we run the utility thread on separate thread. This means that if any code
98 // needs WebKit initialized in the utility process, they need to have
99 // another path to support single process mode.
100 webkit_platform_support_
.reset(new WebKitPlatformSupportImpl
);
101 WebKit::initialize(webkit_platform_support_
.get());
103 GetContentClient()->utility()->UtilityThreadStarted();
106 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
107 if (GetContentClient()->utility()->OnMessageReceived(msg
))
111 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl
, msg
)
112 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started
, OnBatchModeStarted
)
113 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished
, OnBatchModeFinished
)
114 #if defined(OS_POSIX)
115 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins
, OnLoadPlugins
)
117 IPC_MESSAGE_UNHANDLED(handled
= false)
118 IPC_END_MESSAGE_MAP()
122 void UtilityThreadImpl::OnBatchModeStarted() {
126 void UtilityThreadImpl::OnBatchModeFinished() {
127 ChildProcess::current()->ReleaseProcess();
130 #if defined(OS_POSIX)
131 void UtilityThreadImpl::OnLoadPlugins(
132 const std::vector
<base::FilePath
>& plugin_paths
) {
133 PluginList
* plugin_list
= PluginList::Singleton();
135 // On Linux, some plugins expect the browser to have loaded glib/gtk. Do that
136 // before attempting to call into the plugin.
137 // g_thread_init API is deprecated since glib 2.31.0, please see release note:
138 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
139 #if defined(TOOLKIT_GTK)
140 #if !(GLIB_CHECK_VERSION(2, 31, 0))
141 if (!g_thread_get_initialized()) {
145 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
148 std::vector
<WebPluginInfo
> plugins
;
149 // TODO(bauerb): If we restart loading plug-ins, we might mess up the logic in
150 // PluginList::ShouldLoadPlugin due to missing the previously loaded plug-ins
151 // in |plugin_groups|.
152 for (size_t i
= 0; i
< plugin_paths
.size(); ++i
) {
153 WebPluginInfo plugin
;
154 if (!plugin_list
->LoadPluginIntoPluginList(
155 plugin_paths
[i
], &plugins
, &plugin
))
156 Send(new UtilityHostMsg_LoadPluginFailed(i
, plugin_paths
[i
]));
158 Send(new UtilityHostMsg_LoadedPlugin(i
, plugin
));
161 ReleaseProcessIfNeeded();
165 } // namespace content