1 // Copyright 2013 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/in_process_utility_thread.h" 6 7 #include "base/bind.h" 8 #include "base/lazy_instance.h" 9 #include "base/location.h" 10 #include "base/single_thread_task_runner.h" 11 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_task_runner_handle.h" 13 #include "content/child/child_process.h" 14 #include "content/utility/utility_thread_impl.h" 15 16 namespace content { 17 18 // We want to ensure there's only one utility thread running at a time, as there 19 // are many globals used in the utility process. 20 static base::LazyInstance<base::Lock>::DestructorAtExit 21 g_one_utility_thread_lock; 22 InProcessUtilityThread(const InProcessChildThreadParams & params)23InProcessUtilityThread::InProcessUtilityThread( 24 const InProcessChildThreadParams& params) 25 : Thread("Chrome_InProcUtilityThread"), params_(params) {} 26 ~InProcessUtilityThread()27InProcessUtilityThread::~InProcessUtilityThread() { 28 base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_thread_join; 29 Stop(); 30 } 31 Init()32void InProcessUtilityThread::Init() { 33 // We need to return right away or else the main thread that started us will 34 // hang. 35 base::ThreadTaskRunnerHandle::Get()->PostTask( 36 FROM_HERE, base::BindOnce(&InProcessUtilityThread::InitInternal, 37 base::Unretained(this))); 38 } 39 CleanUp()40void InProcessUtilityThread::CleanUp() 41 UNLOCK_FUNCTION(g_one_utility_thread_lock.Get()) { 42 child_process_.reset(); 43 44 // See comment in RendererMainThread. 45 SetThreadWasQuitProperly(true); 46 g_one_utility_thread_lock.Get().Release(); 47 } 48 InitInternal()49void InProcessUtilityThread::InitInternal() 50 EXCLUSIVE_LOCK_FUNCTION(g_one_utility_thread_lock.Get()) { 51 g_one_utility_thread_lock.Get().Acquire(); 52 child_process_.reset(new ChildProcess()); 53 child_process_->set_main_thread(new UtilityThreadImpl(params_)); 54 } 55 CreateInProcessUtilityThread(const InProcessChildThreadParams & params)56base::Thread* CreateInProcessUtilityThread( 57 const InProcessChildThreadParams& params) { 58 return new InProcessUtilityThread(params); 59 } 60 61 } // namespace content 62