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/common/child_process.h"
13 #include "content/common/child_process_messages.h"
14 #include "content/common/utility_messages.h"
15 #include "content/common/webkitplatformsupport_impl.h"
16 #include "content/public/utility/content_utility_client.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
18 #include "webkit/plugins/npapi/plugin_list.h"
20 #if defined(TOOLKIT_GTK)
23 #include "ui/gfx/gtk_util.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 : batch_mode_(false) {
41 ChildProcess::current()->AddRefProcess();
42 webkit_platform_support_
.reset(new WebKitPlatformSupportImpl
);
43 WebKit::initialize(webkit_platform_support_
.get());
44 GetContentClient()->utility()->UtilityThreadStarted();
47 UtilityThreadImpl::~UtilityThreadImpl() {
51 bool UtilityThreadImpl::Send(IPC::Message
* msg
) {
52 return ChildThread::Send(msg
);
55 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
57 ChildProcess::current()->ReleaseProcess();
62 void UtilityThreadImpl::PreCacheFont(const LOGFONT
& log_font
) {
63 Send(new ChildProcessHostMsg_PreCacheFont(log_font
));
66 void UtilityThreadImpl::ReleaseCachedFonts() {
67 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
73 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
74 if (GetContentClient()->utility()->OnMessageReceived(msg
))
78 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl
, msg
)
79 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started
, OnBatchModeStarted
)
80 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished
, OnBatchModeFinished
)
82 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins
, OnLoadPlugins
)
84 IPC_MESSAGE_UNHANDLED(handled
= false)
89 void UtilityThreadImpl::OnBatchModeStarted() {
93 void UtilityThreadImpl::OnBatchModeFinished() {
94 ChildProcess::current()->ReleaseProcess();
98 void UtilityThreadImpl::OnLoadPlugins(
99 const std::vector
<base::FilePath
>& plugin_paths
) {
100 webkit::npapi::PluginList
* plugin_list
=
101 webkit::npapi::PluginList::Singleton();
103 // On Linux, some plugins expect the browser to have loaded glib/gtk. Do that
104 // before attempting to call into the plugin.
105 // g_thread_init API is deprecated since glib 2.31.0, please see release note:
106 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
107 #if defined(TOOLKIT_GTK)
108 #if !(GLIB_CHECK_VERSION(2, 31, 0))
109 if (!g_thread_get_initialized()) {
113 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
116 std::vector
<webkit::WebPluginInfo
> plugins
;
117 // TODO(bauerb): If we restart loading plug-ins, we might mess up the logic in
118 // PluginList::ShouldLoadPlugin due to missing the previously loaded plug-ins
119 // in |plugin_groups|.
120 for (size_t i
= 0; i
< plugin_paths
.size(); ++i
) {
121 webkit::WebPluginInfo plugin
;
122 if (!plugin_list
->LoadPluginIntoPluginList(
123 plugin_paths
[i
], &plugins
, &plugin
))
124 Send(new UtilityHostMsg_LoadPluginFailed(i
, plugin_paths
[i
]));
126 Send(new UtilityHostMsg_LoadedPlugin(i
, plugin
));
129 ReleaseProcessIfNeeded();
133 } // namespace content