Fix build break
[chromium-blink-merge.git] / content / utility / utility_thread_impl.cc
blob4febffa623219122359fdf4da0f1fc2d736c6de4
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"
7 #include <stddef.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)
21 #include <gtk/gtk.h>
23 #include "ui/gfx/gtk_util.h"
24 #endif
26 namespace content {
28 namespace {
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));
37 } // namespace
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() {
48 WebKit::shutdown();
51 bool UtilityThreadImpl::Send(IPC::Message* msg) {
52 return ChildThread::Send(msg);
55 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
56 if (!batch_mode_)
57 ChildProcess::current()->ReleaseProcess();
60 #if defined(OS_WIN)
62 void UtilityThreadImpl::PreCacheFont(const LOGFONT& log_font) {
63 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
66 void UtilityThreadImpl::ReleaseCachedFonts() {
67 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
70 #endif // OS_WIN
73 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
74 if (GetContentClient()->utility()->OnMessageReceived(msg))
75 return true;
77 bool handled = true;
78 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl, msg)
79 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started, OnBatchModeStarted)
80 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished, OnBatchModeFinished)
81 #if defined(OS_POSIX)
82 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins, OnLoadPlugins)
83 #endif // OS_POSIX
84 IPC_MESSAGE_UNHANDLED(handled = false)
85 IPC_END_MESSAGE_MAP()
86 return handled;
89 void UtilityThreadImpl::OnBatchModeStarted() {
90 batch_mode_ = true;
93 void UtilityThreadImpl::OnBatchModeFinished() {
94 ChildProcess::current()->ReleaseProcess();
97 #if defined(OS_POSIX)
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()) {
110 g_thread_init(NULL);
112 #endif
113 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
114 #endif
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]));
125 else
126 Send(new UtilityHostMsg_LoadedPlugin(i, plugin));
129 ReleaseProcessIfNeeded();
131 #endif
133 } // namespace content