Home | History | Annotate | Download | only in utility
      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/memory/scoped_vector.h"
     11 #include "content/child/blink_platform_impl.h"
     12 #include "content/child/child_process.h"
     13 #include "content/common/child_process_messages.h"
     14 #include "content/common/utility_messages.h"
     15 #include "content/public/common/content_switches.h"
     16 #include "content/public/utility/content_utility_client.h"
     17 #include "ipc/ipc_sync_channel.h"
     18 #include "third_party/WebKit/public/web/WebKit.h"
     19 
     20 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
     21 #include "base/files/file_path.h"
     22 #include "content/common/plugin_list.h"
     23 #endif
     24 
     25 namespace content {
     26 
     27 namespace {
     28 
     29 template<typename SRC, typename DEST>
     30 void ConvertVector(const SRC& src, DEST* dest) {
     31   dest->reserve(src.size());
     32   for (typename SRC::const_iterator i = src.begin(); i != src.end(); ++i)
     33     dest->push_back(typename DEST::value_type(*i));
     34 }
     35 
     36 }  // namespace
     37 
     38 UtilityThreadImpl::UtilityThreadImpl() : single_process_(false) {
     39   Init();
     40 }
     41 
     42 UtilityThreadImpl::UtilityThreadImpl(const std::string& channel_name)
     43     : ChildThread(Options(channel_name, false)),
     44       single_process_(true) {
     45   Init();
     46 }
     47 
     48 UtilityThreadImpl::~UtilityThreadImpl() {
     49 }
     50 
     51 void UtilityThreadImpl::Shutdown() {
     52   ChildThread::Shutdown();
     53 
     54   if (!single_process_)
     55     blink::shutdown();
     56 }
     57 
     58 bool UtilityThreadImpl::Send(IPC::Message* msg) {
     59   return ChildThread::Send(msg);
     60 }
     61 
     62 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
     63   if (batch_mode_)
     64     return;
     65 
     66   if (single_process_) {
     67     // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
     68     // to take a different code path than the multi-process case because that
     69     // depends on the child process going away to close the channel, but that
     70     // can't happen when we're in single process mode.
     71     channel()->Close();
     72   } else {
     73     ChildProcess::current()->ReleaseProcess();
     74   }
     75 }
     76 
     77 #if defined(OS_WIN)
     78 
     79 void UtilityThreadImpl::PreCacheFont(const LOGFONT& log_font) {
     80   Send(new ChildProcessHostMsg_PreCacheFont(log_font));
     81 }
     82 
     83 void UtilityThreadImpl::ReleaseCachedFonts() {
     84   Send(new ChildProcessHostMsg_ReleaseCachedFonts());
     85 }
     86 
     87 #endif  // OS_WIN
     88 
     89 void UtilityThreadImpl::Init() {
     90   batch_mode_ = false;
     91   ChildProcess::current()->AddRefProcess();
     92   if (!single_process_) {
     93     // We can only initialize WebKit on one thread, and in single process mode
     94     // we run the utility thread on separate thread. This means that if any code
     95     // needs WebKit initialized in the utility process, they need to have
     96     // another path to support single process mode.
     97     webkit_platform_support_.reset(new BlinkPlatformImpl);
     98     blink::initialize(webkit_platform_support_.get());
     99   }
    100   GetContentClient()->utility()->UtilityThreadStarted();
    101 }
    102 
    103 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
    104   if (GetContentClient()->utility()->OnMessageReceived(msg))
    105     return true;
    106 
    107   bool handled = true;
    108   IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl, msg)
    109     IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started, OnBatchModeStarted)
    110     IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished, OnBatchModeFinished)
    111 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
    112     IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins, OnLoadPlugins)
    113 #endif
    114     IPC_MESSAGE_UNHANDLED(handled = false)
    115   IPC_END_MESSAGE_MAP()
    116   return handled;
    117 }
    118 
    119 void UtilityThreadImpl::OnBatchModeStarted() {
    120   batch_mode_ = true;
    121 }
    122 
    123 void UtilityThreadImpl::OnBatchModeFinished() {
    124   batch_mode_ = false;
    125   ReleaseProcessIfNeeded();
    126 }
    127 
    128 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
    129 void UtilityThreadImpl::OnLoadPlugins(
    130     const std::vector<base::FilePath>& plugin_paths) {
    131   PluginList* plugin_list = PluginList::Singleton();
    132 
    133   std::vector<WebPluginInfo> plugins;
    134   // TODO(bauerb): If we restart loading plug-ins, we might mess up the logic in
    135   // PluginList::ShouldLoadPlugin due to missing the previously loaded plug-ins
    136   // in |plugin_groups|.
    137   for (size_t i = 0; i < plugin_paths.size(); ++i) {
    138     WebPluginInfo plugin;
    139     if (!plugin_list->LoadPluginIntoPluginList(
    140         plugin_paths[i], &plugins, &plugin))
    141       Send(new UtilityHostMsg_LoadPluginFailed(i, plugin_paths[i]));
    142     else
    143       Send(new UtilityHostMsg_LoadedPlugin(i, plugin));
    144   }
    145 
    146   ReleaseProcessIfNeeded();
    147 }
    148 #endif
    149 
    150 }  // namespace content
    151