Update broken references to image assets
[chromium-blink-merge.git] / content / utility / utility_thread_impl.cc
blob9e5e33ea574fab8764282a8965cfc305e705a03f
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/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 "content/utility/utility_blink_platform_impl.h"
18 #include "content/utility/utility_process_control_impl.h"
19 #include "ipc/ipc_sync_channel.h"
20 #include "third_party/WebKit/public/web/WebKit.h"
22 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
23 #include "base/files/file_path.h"
24 #include "content/common/plugin_list.h"
25 #endif
27 namespace content {
29 namespace {
31 template<typename SRC, typename DEST>
32 void ConvertVector(const SRC& src, DEST* dest) {
33 dest->reserve(src.size());
34 for (typename SRC::const_iterator i = src.begin(); i != src.end(); ++i)
35 dest->push_back(typename DEST::value_type(*i));
38 } // namespace
40 UtilityThreadImpl::UtilityThreadImpl()
41 : ChildThreadImpl(ChildThreadImpl::Options::Builder().Build()) {
42 Init();
45 UtilityThreadImpl::UtilityThreadImpl(const InProcessChildThreadParams& params)
46 : ChildThreadImpl(ChildThreadImpl::Options::Builder()
47 .InBrowserProcess(params)
48 .Build()) {
49 Init();
52 UtilityThreadImpl::~UtilityThreadImpl() {
55 void UtilityThreadImpl::Shutdown() {
56 ChildThreadImpl::Shutdown();
58 if (blink_platform_impl_)
59 blink::shutdownWithoutV8();
62 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
63 if (batch_mode_)
64 return;
66 if (IsInBrowserProcess()) {
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();
77 void UtilityThreadImpl::EnsureBlinkInitialized() {
78 if (blink_platform_impl_ || IsInBrowserProcess()) {
79 // We can only initialize WebKit on one thread, and in single process mode
80 // we run the utility thread on separate thread. This means that if any code
81 // needs WebKit initialized in the utility process, they need to have
82 // another path to support single process mode.
83 return;
86 blink_platform_impl_.reset(new UtilityBlinkPlatformImpl);
87 blink::initializeWithoutV8(blink_platform_impl_.get());
90 void UtilityThreadImpl::Init() {
91 batch_mode_ = false;
92 ChildProcess::current()->AddRefProcess();
93 GetContentClient()->utility()->UtilityThreadStarted();
95 process_control_.reset(new UtilityProcessControlImpl);
96 service_registry()->AddService(base::Bind(
97 &UtilityThreadImpl::BindProcessControlRequest, base::Unretained(this)));
99 GetContentClient()->utility()->RegisterMojoServices(service_registry());
102 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
103 if (GetContentClient()->utility()->OnMessageReceived(msg))
104 return true;
106 bool handled = true;
107 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl, msg)
108 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started, OnBatchModeStarted)
109 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished, OnBatchModeFinished)
110 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
111 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins, OnLoadPlugins)
112 #endif
113 IPC_MESSAGE_UNHANDLED(handled = false)
114 IPC_END_MESSAGE_MAP()
115 return handled;
118 void UtilityThreadImpl::OnBatchModeStarted() {
119 batch_mode_ = true;
122 void UtilityThreadImpl::OnBatchModeFinished() {
123 batch_mode_ = false;
124 ReleaseProcessIfNeeded();
127 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
128 void UtilityThreadImpl::OnLoadPlugins(
129 const std::vector<base::FilePath>& plugin_paths) {
130 PluginList* plugin_list = PluginList::Singleton();
132 std::vector<WebPluginInfo> plugins;
133 // TODO(bauerb): If we restart loading plugins, we might mess up the logic in
134 // PluginList::ShouldLoadPlugin due to missing the previously loaded plugins
135 // in |plugin_groups|.
136 for (size_t i = 0; i < plugin_paths.size(); ++i) {
137 WebPluginInfo plugin;
138 if (!plugin_list->LoadPluginIntoPluginList(
139 plugin_paths[i], &plugins, &plugin))
140 Send(new UtilityHostMsg_LoadPluginFailed(i, plugin_paths[i]));
141 else
142 Send(new UtilityHostMsg_LoadedPlugin(i, plugin));
145 ReleaseProcessIfNeeded();
147 #endif
149 void UtilityThreadImpl::BindProcessControlRequest(
150 mojo::InterfaceRequest<ProcessControl> request) {
151 DCHECK(process_control_);
152 process_control_bindings_.AddBinding(process_control_.get(), request.Pass());
155 } // namespace content