Fix race condition in WebstoreInstallHelper.
[chromium-blink-merge.git] / content / utility / utility_thread_impl.cc
blob1a020ea31ce81d22c83fb6b2eaeca21ffd77b480
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 "ipc/ipc_sync_channel.h"
19 #include "third_party/WebKit/public/web/WebKit.h"
21 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
22 #include "base/files/file_path.h"
23 #include "content/common/plugin_list.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 : ChildThreadImpl(ChildThreadImpl::Options::Builder().Build()) {
41 Init();
44 UtilityThreadImpl::UtilityThreadImpl(const InProcessChildThreadParams& params)
45 : ChildThreadImpl(ChildThreadImpl::Options::Builder()
46 .InBrowserProcess(params)
47 .Build()) {
48 Init();
51 UtilityThreadImpl::~UtilityThreadImpl() {
54 void UtilityThreadImpl::Shutdown() {
55 ChildThreadImpl::Shutdown();
57 if (!IsInBrowserProcess())
58 blink::shutdown();
61 void UtilityThreadImpl::ReleaseProcessIfNeeded() {
62 if (batch_mode_)
63 return;
65 if (IsInBrowserProcess()) {
66 // Close the channel to cause UtilityProcessHostImpl to be deleted. We need
67 // to take a different code path than the multi-process case because that
68 // depends on the child process going away to close the channel, but that
69 // can't happen when we're in single process mode.
70 channel()->Close();
71 } else {
72 ChildProcess::current()->ReleaseProcess();
76 void UtilityThreadImpl::Init() {
77 batch_mode_ = false;
78 ChildProcess::current()->AddRefProcess();
79 if (!IsInBrowserProcess()) {
80 // We can only initialize WebKit on one thread, and in single process mode
81 // we run the utility thread on separate thread. This means that if any code
82 // needs WebKit initialized in the utility process, they need to have
83 // another path to support single process mode.
84 blink_platform_impl_.reset(new UtilityBlinkPlatformImpl);
85 blink::initialize(blink_platform_impl_.get());
87 GetContentClient()->utility()->UtilityThreadStarted();
88 GetContentClient()->utility()->RegisterMojoServices(service_registry());
91 bool UtilityThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
92 if (GetContentClient()->utility()->OnMessageReceived(msg))
93 return true;
95 bool handled = true;
96 IPC_BEGIN_MESSAGE_MAP(UtilityThreadImpl, msg)
97 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Started, OnBatchModeStarted)
98 IPC_MESSAGE_HANDLER(UtilityMsg_BatchMode_Finished, OnBatchModeFinished)
99 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
100 IPC_MESSAGE_HANDLER(UtilityMsg_LoadPlugins, OnLoadPlugins)
101 #endif
102 IPC_MESSAGE_UNHANDLED(handled = false)
103 IPC_END_MESSAGE_MAP()
104 return handled;
107 void UtilityThreadImpl::OnBatchModeStarted() {
108 batch_mode_ = true;
111 void UtilityThreadImpl::OnBatchModeFinished() {
112 batch_mode_ = false;
113 ReleaseProcessIfNeeded();
116 #if defined(OS_POSIX) && defined(ENABLE_PLUGINS)
117 void UtilityThreadImpl::OnLoadPlugins(
118 const std::vector<base::FilePath>& plugin_paths) {
119 PluginList* plugin_list = PluginList::Singleton();
121 std::vector<WebPluginInfo> plugins;
122 // TODO(bauerb): If we restart loading plugins, we might mess up the logic in
123 // PluginList::ShouldLoadPlugin due to missing the previously loaded plugins
124 // in |plugin_groups|.
125 for (size_t i = 0; i < plugin_paths.size(); ++i) {
126 WebPluginInfo plugin;
127 if (!plugin_list->LoadPluginIntoPluginList(
128 plugin_paths[i], &plugins, &plugin))
129 Send(new UtilityHostMsg_LoadPluginFailed(i, plugin_paths[i]));
130 else
131 Send(new UtilityHostMsg_LoadedPlugin(i, plugin));
134 ReleaseProcessIfNeeded();
136 #endif
138 } // namespace content