summaryrefslogtreecommitdiffstats
path: root/chrome/browser/nacl_host/nacl_broker_service_win.cc
blob: d5524ad98c6c3ae25e03ab03a922ff8c4ebdf2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/nacl_host/nacl_broker_service_win.h"

#include "chrome/browser/browser_process.h"
#include "chrome/browser/nacl_host/nacl_process_host.h"
#include "chrome/common/chrome_switches.h"

NaClBrokerService* NaClBrokerService::GetInstance() {
  return Singleton<NaClBrokerService>::get();
}

NaClBrokerService::NaClBrokerService()
    : loaders_running_(0),
      resource_dispatcher_host_(NULL),
      initialized_(false) {
}

void NaClBrokerService::Init(ResourceDispatcherHost* resource_dispatcher_host) {
  if (!initialized_)
    resource_dispatcher_host_ = resource_dispatcher_host;
  initialized_ = true;
}

bool NaClBrokerService::StartBroker() {
  NaClBrokerHost* broker_host = new NaClBrokerHost(resource_dispatcher_host_);
  if (!broker_host->Init()) {
    delete broker_host;
    return false;
  }
  return true;
}

bool NaClBrokerService::LaunchLoader(NaClProcessHost* nacl_process_host,
                                     const std::wstring& loader_channel_id) {
  // Add task to the list
  pending_launches_[loader_channel_id] = nacl_process_host;
  NaClBrokerHost* broker_host = GetBrokerHost();

  if (!broker_host) {
    if (!StartBroker())
      return false;
    broker_host = GetBrokerHost();
  }
  broker_host->LaunchLoader(loader_channel_id);

  return true;
}

void NaClBrokerService::OnLoaderLaunched(const std::wstring& channel_id,
                                         base::ProcessHandle handle) {
  NaClProcessHost* client;
  PendingLaunchesMap::iterator it = pending_launches_.find(channel_id);
  if (pending_launches_.end() == it)
    NOTREACHED();

  client = it->second;
  client->OnProcessLaunchedByBroker(handle);
  pending_launches_.erase(it);
  ++loaders_running_;
}

void NaClBrokerService::OnLoaderDied() {
  --loaders_running_;
  // Stop the broker only if there are no loaders running or being launched.
  NaClBrokerHost* broker_host = GetBrokerHost();
  if (loaders_running_ + pending_launches_.size() == 0 && broker_host != NULL) {
    broker_host->StopBroker();
  }
}

NaClBrokerHost* NaClBrokerService::GetBrokerHost() {
  for (BrowserChildProcessHost::Iterator iter(
           ChildProcessInfo::NACL_BROKER_PROCESS);
       !iter.Done();
       ++iter) {
    NaClBrokerHost* broker_host = static_cast<NaClBrokerHost*>(*iter);
    return broker_host;
  }
  return NULL;
}