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#ifndef CHROME_NACL_NACL_LISTENER_H_
6#define CHROME_NACL_NACL_LISTENER_H_
7
8#include <vector>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/shared_memory.h"
12#include "base/synchronization/waitable_event.h"
13#include "base/threading/thread.h"
14#include "components/nacl/common/nacl_types.h"
15#include "components/nacl/loader/nacl_trusted_listener.h"
16#include "ipc/ipc_listener.h"
17
18namespace base {
19class MessageLoopProxy;
20}
21
22namespace IPC {
23class SyncChannel;
24class SyncMessageFilter;
25}
26
27// The NaClListener is an IPC channel listener that waits for a
28// request to start a NaCl module.
29class NaClListener : public IPC::Listener {
30 public:
31  NaClListener();
32  virtual ~NaClListener();
33  // Listen for a request to launch a NaCl module.
34  void Listen();
35
36  bool Send(IPC::Message* msg);
37
38#if defined(OS_LINUX)
39  void set_prereserved_sandbox_size(size_t prereserved_sandbox_size) {
40    prereserved_sandbox_size_ = prereserved_sandbox_size;
41  }
42#endif
43#if defined(OS_POSIX)
44  void set_number_of_cores(int number_of_cores) {
45    number_of_cores_ = number_of_cores;
46  }
47#endif
48
49  void* crash_info_shmem_memory() const { return crash_info_shmem_->memory(); }
50
51  typedef base::Callback<void(IPC::PlatformFileForTransit, base::FilePath)>
52      ResolveFileTokenCallback;
53  void ResolveFileToken(uint64_t token_lo,
54                        uint64_t token_hi,
55                        ResolveFileTokenCallback cb);
56  void OnFileTokenResolved(uint64_t token_lo,
57                           uint64_t token_hi,
58                           IPC::PlatformFileForTransit ipc_fd,
59                           base::FilePath file_path);
60
61 private:
62  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
63
64  void OnStart(const nacl::NaClStartParams& params);
65
66  // A channel back to the browser.
67  scoped_ptr<IPC::SyncChannel> channel_;
68
69  // A filter that allows other threads to use the channel.
70  scoped_refptr<IPC::SyncMessageFilter> filter_;
71
72  base::WaitableEvent shutdown_event_;
73  base::Thread io_thread_;
74
75#if defined(OS_LINUX)
76  size_t prereserved_sandbox_size_;
77#endif
78#if defined(OS_POSIX)
79  // The outer sandbox on Linux and OSX prevents
80  // sysconf(_SC_NPROCESSORS) from working; in Windows, there are no
81  // problems with invoking GetSystemInfo.  Therefore, only in
82  // OS_POSIX do we need to supply the number of cores into the
83  // NaClChromeMainArgs object.
84  int number_of_cores_;
85#endif
86
87  scoped_ptr<base::SharedMemory> crash_info_shmem_;
88
89  scoped_refptr<NaClTrustedListener> trusted_listener_;
90
91  ResolveFileTokenCallback resolved_cb_;
92
93  // Used to identify what thread we're on.
94  base::MessageLoop* main_loop_;
95
96  DISALLOW_COPY_AND_ASSIGN(NaClListener);
97};
98
99#endif  // CHROME_NACL_NACL_LISTENER_H_
100