Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / nacl / browser / nacl_broker_service_win.cc
blob3052e94a8513311b42d1d7657d4104b4b0c1eab3
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 "components/nacl/browser/nacl_broker_service_win.h"
7 #include "components/nacl/browser/nacl_process_host.h"
8 #include "components/nacl/common/nacl_process_type.h"
9 #include "content/public/browser/browser_child_process_host_iterator.h"
11 using content::BrowserChildProcessHostIterator;
13 namespace nacl {
15 NaClBrokerService* NaClBrokerService::GetInstance() {
16 return base::Singleton<NaClBrokerService>::get();
19 NaClBrokerService::NaClBrokerService()
20 : loaders_running_(0) {
23 NaClBrokerService::~NaClBrokerService() {
26 bool NaClBrokerService::StartBroker() {
27 NaClBrokerHost* broker_host = new NaClBrokerHost;
28 if (!broker_host->Init()) {
29 delete broker_host;
30 return false;
32 return true;
35 bool NaClBrokerService::LaunchLoader(
36 base::WeakPtr<nacl::NaClProcessHost> nacl_process_host,
37 const std::string& loader_channel_id) {
38 // Add task to the list
39 pending_launches_[loader_channel_id] = nacl_process_host;
40 NaClBrokerHost* broker_host = GetBrokerHost();
42 if (!broker_host) {
43 if (!StartBroker())
44 return false;
45 broker_host = GetBrokerHost();
47 broker_host->LaunchLoader(loader_channel_id);
49 return true;
52 void NaClBrokerService::OnLoaderLaunched(const std::string& channel_id,
53 base::ProcessHandle handle) {
54 PendingLaunchesMap::iterator it = pending_launches_.find(channel_id);
55 if (pending_launches_.end() == it)
56 NOTREACHED();
58 NaClProcessHost* client = it->second.get();
59 if (client)
60 client->OnProcessLaunchedByBroker(handle);
61 pending_launches_.erase(it);
62 ++loaders_running_;
65 void NaClBrokerService::OnLoaderDied() {
66 DCHECK(loaders_running_ > 0);
67 --loaders_running_;
68 // Stop the broker only if there are no loaders running or being launched.
69 NaClBrokerHost* broker_host = GetBrokerHost();
70 if (loaders_running_ + pending_launches_.size() == 0 && broker_host != NULL) {
71 broker_host->StopBroker();
75 bool NaClBrokerService::LaunchDebugExceptionHandler(
76 base::WeakPtr<NaClProcessHost> nacl_process_host, int32 pid,
77 base::ProcessHandle process_handle, const std::string& startup_info) {
78 pending_debuggers_[pid] = nacl_process_host;
79 NaClBrokerHost* broker_host = GetBrokerHost();
80 if (!broker_host)
81 return false;
82 return broker_host->LaunchDebugExceptionHandler(pid, process_handle,
83 startup_info);
86 void NaClBrokerService::OnDebugExceptionHandlerLaunched(int32 pid,
87 bool success) {
88 PendingDebugExceptionHandlersMap::iterator it = pending_debuggers_.find(pid);
89 if (pending_debuggers_.end() == it)
90 NOTREACHED();
92 NaClProcessHost* client = it->second.get();
93 if (client)
94 client->OnDebugExceptionHandlerLaunchedByBroker(success);
95 pending_debuggers_.erase(it);
98 NaClBrokerHost* NaClBrokerService::GetBrokerHost() {
99 BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_BROKER);
100 while (!iter.Done()) {
101 NaClBrokerHost* host = static_cast<NaClBrokerHost*>(iter.GetDelegate());
102 if (!host->IsTerminating())
103 return host;
104 ++iter;
106 return NULL;
109 } // namespace nacl