nacl_listener.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef COMPONENTS_NACL_LOADER_NACL_LISTENER_H_
  5. #define COMPONENTS_NACL_LOADER_NACL_LISTENER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <map>
  9. #include <memory>
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/shared_memory_mapping.h"
  12. #include "base/synchronization/waitable_event.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "base/threading/thread.h"
  15. #include "build/build_config.h"
  16. #include "components/nacl/common/nacl_types.h"
  17. #include "components/nacl/loader/nacl_ipc_adapter.h"
  18. #include "components/nacl/loader/nacl_trusted_listener.h"
  19. #include "ipc/ipc_listener.h"
  20. namespace IPC {
  21. class SyncChannel;
  22. class SyncMessageFilter;
  23. }
  24. // The NaClListener is an IPC channel listener that waits for a
  25. // request to start a NaCl module.
  26. class NaClListener : public IPC::Listener {
  27. public:
  28. NaClListener();
  29. NaClListener(const NaClListener&) = delete;
  30. NaClListener& operator=(const NaClListener&) = delete;
  31. ~NaClListener() override;
  32. // Listen for a request to launch a NaCl module.
  33. void Listen();
  34. bool Send(IPC::Message* msg);
  35. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  36. void set_prereserved_sandbox_size(size_t prereserved_sandbox_size) {
  37. prereserved_sandbox_size_ = prereserved_sandbox_size;
  38. }
  39. #endif
  40. #if BUILDFLAG(IS_POSIX)
  41. void set_number_of_cores(int number_of_cores) {
  42. number_of_cores_ = number_of_cores;
  43. }
  44. #endif
  45. void* crash_info_shmem_memory() const {
  46. return crash_info_shmem_mapping_.memory();
  47. }
  48. NaClTrustedListener* trusted_listener() const {
  49. return trusted_listener_.get();
  50. }
  51. void ResolveFileToken(uint64_t token_lo,
  52. uint64_t token_hi,
  53. NaClIPCAdapter::ResolveFileTokenReplyCallback cb);
  54. void OnFileTokenResolved(uint64_t token_lo,
  55. uint64_t token_hi,
  56. IPC::PlatformFileForTransit ipc_fd,
  57. base::FilePath file_path);
  58. private:
  59. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  60. static int MakeSharedMemorySegment(size_t length, int executable);
  61. #endif
  62. bool OnMessageReceived(const IPC::Message& msg) override;
  63. bool OnOpenResource(const IPC::Message& msg,
  64. const std::string& key,
  65. NaClIPCAdapter::OpenResourceReplyCallback cb);
  66. void OnAddPrefetchedResource(
  67. const nacl::NaClResourcePrefetchResult& prefetched_resource_file);
  68. void OnStart(nacl::NaClStartParams params);
  69. // A channel back to the browser.
  70. std::unique_ptr<IPC::SyncChannel> channel_;
  71. // A filter that allows other threads to use the channel.
  72. scoped_refptr<IPC::SyncMessageFilter> filter_;
  73. base::WaitableEvent shutdown_event_;
  74. base::Thread io_thread_;
  75. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  76. size_t prereserved_sandbox_size_;
  77. #endif
  78. #if BUILDFLAG(IS_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. base::WritableSharedMemoryMapping crash_info_shmem_mapping_;
  87. std::unique_ptr<NaClTrustedListener> trusted_listener_;
  88. NaClIPCAdapter::ResolveFileTokenReplyCallback resolved_cb_;
  89. // Used to identify what thread we're on.
  90. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  91. typedef std::map<
  92. std::string, // manifest key
  93. std::pair<IPC::PlatformFileForTransit,
  94. base::FilePath> > PrefetchedResourceFilesMap;
  95. PrefetchedResourceFilesMap prefetched_resource_files_;
  96. bool is_started_;
  97. };
  98. #endif // COMPONENTS_NACL_LOADER_NACL_LISTENER_H_