[email protected] | 54e158ef | 2012-03-11 02:10:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 5 | #include "components/nacl/browser/nacl_broker_service_win.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 6 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 7 | #include "components/nacl/browser/nacl_process_host.h" |
[email protected] | d5d38325 | 2013-07-04 14:44:32 | [diff] [blame] | 8 | #include "components/nacl/common/nacl_process_type.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 9 | #include "content/public/browser/browser_child_process_host_iterator.h" |
10 | |||||
11 | using content::BrowserChildProcessHostIterator; | ||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 12 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 13 | namespace nacl { |
14 | |||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 15 | NaClBrokerService* NaClBrokerService::GetInstance() { |
16 | return Singleton<NaClBrokerService>::get(); | ||||
17 | } | ||||
18 | |||||
19 | NaClBrokerService::NaClBrokerService() | ||||
[email protected] | 1dbaaa70 | 2011-04-06 17:43:42 | [diff] [blame] | 20 | : loaders_running_(0) { |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 21 | } |
22 | |||||
23 | bool NaClBrokerService::StartBroker() { | ||||
[email protected] | 1dbaaa70 | 2011-04-06 17:43:42 | [diff] [blame] | 24 | NaClBrokerHost* broker_host = new NaClBrokerHost; |
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 25 | if (!broker_host->Init()) { |
26 | delete broker_host; | ||||
27 | return false; | ||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 28 | } |
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 29 | return true; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 30 | } |
31 | |||||
[email protected] | 26b3c00 | 2012-04-26 19:38:34 | [diff] [blame] | 32 | bool NaClBrokerService::LaunchLoader( |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 33 | base::WeakPtr<nacl::NaClProcessHost> nacl_process_host, |
[email protected] | 26b3c00 | 2012-04-26 19:38:34 | [diff] [blame] | 34 | const std::string& loader_channel_id) { |
[email protected] | c3f678fc | 2010-02-22 21:56:00 | [diff] [blame] | 35 | // Add task to the list |
36 | pending_launches_[loader_channel_id] = nacl_process_host; | ||||
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 37 | NaClBrokerHost* broker_host = GetBrokerHost(); |
38 | |||||
39 | if (!broker_host) { | ||||
40 | if (!StartBroker()) | ||||
41 | return false; | ||||
42 | broker_host = GetBrokerHost(); | ||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 43 | } |
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 44 | broker_host->LaunchLoader(loader_channel_id); |
45 | |||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 46 | return true; |
47 | } | ||||
48 | |||||
[email protected] | 644b2e2 | 2012-03-25 01:41:07 | [diff] [blame] | 49 | void NaClBrokerService::OnLoaderLaunched(const std::string& channel_id, |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 50 | base::ProcessHandle handle) { |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 51 | PendingLaunchesMap::iterator it = pending_launches_.find(channel_id); |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 52 | if (pending_launches_.end() == it) |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 53 | NOTREACHED(); |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 54 | |
[email protected] | f4605dc | 2013-06-05 13:07:11 | [diff] [blame] | 55 | NaClProcessHost* client = it->second.get(); |
[email protected] | 26b3c00 | 2012-04-26 19:38:34 | [diff] [blame] | 56 | if (client) |
57 | client->OnProcessLaunchedByBroker(handle); | ||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 58 | pending_launches_.erase(it); |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 59 | ++loaders_running_; |
60 | } | ||||
61 | |||||
62 | void NaClBrokerService::OnLoaderDied() { | ||||
[email protected] | 54e158ef | 2012-03-11 02:10:48 | [diff] [blame] | 63 | DCHECK(loaders_running_ > 0); |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 64 | --loaders_running_; |
65 | // Stop the broker only if there are no loaders running or being launched. | ||||
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 66 | NaClBrokerHost* broker_host = GetBrokerHost(); |
67 | if (loaders_running_ + pending_launches_.size() == 0 && broker_host != NULL) { | ||||
68 | broker_host->StopBroker(); | ||||
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 69 | } |
70 | } | ||||
71 | |||||
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 72 | bool NaClBrokerService::LaunchDebugExceptionHandler( |
[email protected] | 4c65fb63 | 2012-04-27 00:42:25 | [diff] [blame] | 73 | base::WeakPtr<NaClProcessHost> nacl_process_host, int32 pid, |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 74 | base::ProcessHandle process_handle, const std::string& startup_info) { |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 75 | pending_debuggers_[pid] = nacl_process_host; |
76 | NaClBrokerHost* broker_host = GetBrokerHost(); | ||||
77 | if (!broker_host) | ||||
78 | return false; | ||||
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 79 | return broker_host->LaunchDebugExceptionHandler(pid, process_handle, |
80 | startup_info); | ||||
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 81 | } |
82 | |||||
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 83 | void NaClBrokerService::OnDebugExceptionHandlerLaunched(int32 pid, |
84 | bool success) { | ||||
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 85 | PendingDebugExceptionHandlersMap::iterator it = pending_debuggers_.find(pid); |
86 | if (pending_debuggers_.end() == it) | ||||
87 | NOTREACHED(); | ||||
88 | |||||
[email protected] | f4605dc | 2013-06-05 13:07:11 | [diff] [blame] | 89 | NaClProcessHost* client = it->second.get(); |
[email protected] | 26b3c00 | 2012-04-26 19:38:34 | [diff] [blame] | 90 | if (client) |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 91 | client->OnDebugExceptionHandlerLaunchedByBroker(success); |
[email protected] | fb335fe | 2012-03-24 18:11:38 | [diff] [blame] | 92 | pending_debuggers_.erase(it); |
93 | } | ||||
94 | |||||
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 95 | NaClBrokerHost* NaClBrokerService::GetBrokerHost() { |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 96 | BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_BROKER); |
[email protected] | 15c532e | 2012-06-15 00:39:50 | [diff] [blame] | 97 | while (!iter.Done()) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 98 | NaClBrokerHost* host = static_cast<NaClBrokerHost*>( |
99 | iter.GetDelegate()); | ||||
[email protected] | 15c532e | 2012-06-15 00:39:50 | [diff] [blame] | 100 | if (!host->IsTerminating()) |
101 | return host; | ||||
102 | ++iter; | ||||
103 | } | ||||
104 | return NULL; | ||||
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 105 | } |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 106 | |
107 | } // namespace nacl |