• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/utility/utility_thread_impl.h"
6 
7 #include <stddef.h>
8 
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"
21 
22 namespace content {
23 
24 namespace {
25 
26 template<typename SRC, typename DEST>
ConvertVector(const SRC & src,DEST * 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));
31 }
32 
33 }  // namespace
34 
UtilityThreadImpl()35 UtilityThreadImpl::UtilityThreadImpl() : single_process_(false) {
36   Init();
37 }
38 
UtilityThreadImpl(const std::string & channel_name)39 UtilityThreadImpl::UtilityThreadImpl(const std::string& channel_name)
40     : ChildThread(channel_name),
41       single_process_(true) {
42   Init();
43 }
44 
~UtilityThreadImpl()45 UtilityThreadImpl::~UtilityThreadImpl() {
46 }
47 
Shutdown()48 void UtilityThreadImpl::Shutdown() {
49   ChildThread::Shutdown();
50 
51   if (!single_process_)
52     blink::shutdown();
53 }
54 
Send(IPC::Message * msg)55 bool UtilityThreadImpl::Send(IPC::Message* msg) {
56   return ChildThread::Send(msg);
57 }
58 
ReleaseProcessIfNeeded()59 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
60   if (batch_mode_)
61     return;
62 
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.
68     channel()->Close();
69   } else {
70     ChildProcess::current()->ReleaseProcess();
71   }
72 }
73 
74 #if defined(OS_WIN)
75 
PreCacheFont(const LOGFONT & log_font)76 void UtilityThreadImpl::PreCacheFont(const LOGFONT& log_font) {
77   Send(new ChildProcessHostMsg_PreCacheFont(log_font));
78 }
79 
ReleaseCachedFonts()80 void UtilityThreadImpl::ReleaseCachedFonts() {
81   Send(new ChildProcessHostMsg_ReleaseCachedFonts());
82 }
83 
84 #endif  // OS_WIN
85 
Init()86 void UtilityThreadImpl::Init() {
87   batch_mode_ = false;
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());
96   }
97   GetContentClient()->utility()->UtilityThreadStarted();
98 }
99 
OnControlMessageReceived(const IPC::Message & msg)100 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
101   if (GetContentClient()->utility()->OnMessageReceived(msg))
102     return true;
103 
104   bool handled = true;
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)
110 #endif  // OS_POSIX
111     IPC_MESSAGE_UNHANDLED(handled = false)
112   IPC_END_MESSAGE_MAP()
113   return handled;
114 }
115 
OnBatchModeStarted()116 void UtilityThreadImpl::OnBatchModeStarted() {
117   batch_mode_ = true;
118 }
119 
OnBatchModeFinished()120 void UtilityThreadImpl::OnBatchModeFinished() {
121   ChildProcess::current()->ReleaseProcess();
122 }
123 
124 #if defined(OS_POSIX)
OnLoadPlugins(const std::vector<base::FilePath> & plugin_paths)125 void UtilityThreadImpl::OnLoadPlugins(
126     const std::vector<base::FilePath>& plugin_paths) {
127   PluginList* plugin_list = PluginList::Singleton();
128 
129   std::vector<WebPluginInfo> plugins;
130   // TODO(bauerb): If we restart loading plug-ins, we might mess up the logic in
131   // PluginList::ShouldLoadPlugin due to missing the previously loaded plug-ins
132   // in |plugin_groups|.
133   for (size_t i = 0; i < plugin_paths.size(); ++i) {
134     WebPluginInfo plugin;
135     if (!plugin_list->LoadPluginIntoPluginList(
136         plugin_paths[i], &plugins, &plugin))
137       Send(new UtilityHostMsg_LoadPluginFailed(i, plugin_paths[i]));
138     else
139       Send(new UtilityHostMsg_LoadedPlugin(i, plugin));
140   }
141 
142   ReleaseProcessIfNeeded();
143 }
144 #endif
145 
146 }  // namespace content
147