xref: /aosp_15_r20/external/cronet/components/nacl/loader/nacl_listener.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 COMPONENTS_NACL_LOADER_NACL_LISTENER_H_
6 #define COMPONENTS_NACL_LOADER_NACL_LISTENER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <memory>
13 
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory_mapping.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/task/single_thread_task_runner.h"
18 #include "base/threading/thread.h"
19 #include "build/build_config.h"
20 #include "components/nacl/common/nacl_types.h"
21 #include "components/nacl/loader/nacl_ipc_adapter.h"
22 #include "components/nacl/loader/nacl_trusted_listener.h"
23 #include "ipc/ipc_listener.h"
24 
25 namespace IPC {
26 class SyncChannel;
27 class SyncMessageFilter;
28 }
29 
30 // The NaClListener is an IPC channel listener that waits for a
31 // request to start a NaCl module.
32 class NaClListener : public IPC::Listener {
33  public:
34   NaClListener();
35 
36   NaClListener(const NaClListener&) = delete;
37   NaClListener& operator=(const NaClListener&) = delete;
38 
39   ~NaClListener() override;
40   // Listen for a request to launch a NaCl module.
41   void Listen();
42 
43   bool Send(IPC::Message* msg);
44 
45 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
set_prereserved_sandbox_size(size_t prereserved_sandbox_size)46   void set_prereserved_sandbox_size(size_t prereserved_sandbox_size) {
47     prereserved_sandbox_size_ = prereserved_sandbox_size;
48   }
49 #endif
50 #if BUILDFLAG(IS_POSIX)
set_number_of_cores(int number_of_cores)51   void set_number_of_cores(int number_of_cores) {
52     number_of_cores_ = number_of_cores;
53   }
54 #endif
55 
crash_info_shmem_memory()56   void* crash_info_shmem_memory() const {
57     return crash_info_shmem_mapping_.memory();
58   }
59 
trusted_listener()60   NaClTrustedListener* trusted_listener() const {
61     return trusted_listener_.get();
62   }
63 
64   void ResolveFileToken(uint64_t token_lo,
65                         uint64_t token_hi,
66                         NaClIPCAdapter::ResolveFileTokenReplyCallback cb);
67   void OnFileTokenResolved(uint64_t token_lo,
68                            uint64_t token_hi,
69                            IPC::PlatformFileForTransit ipc_fd,
70                            base::FilePath file_path);
71 
72  private:
73 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
74   static int MakeSharedMemorySegment(size_t length, int executable);
75 #endif
76 
77   bool OnMessageReceived(const IPC::Message& msg) override;
78 
79   bool OnOpenResource(const IPC::Message& msg,
80                       const std::string& key,
81                       NaClIPCAdapter::OpenResourceReplyCallback cb);
82 
83   void OnAddPrefetchedResource(
84       const nacl::NaClResourcePrefetchResult& prefetched_resource_file);
85   void OnStart(nacl::NaClStartParams params);
86 
87   // A channel back to the browser.
88   std::unique_ptr<IPC::SyncChannel> channel_;
89 
90   // A filter that allows other threads to use the channel.
91   scoped_refptr<IPC::SyncMessageFilter> filter_;
92 
93   base::WaitableEvent shutdown_event_;
94   base::Thread io_thread_;
95 
96 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
97   size_t prereserved_sandbox_size_;
98 #endif
99 #if BUILDFLAG(IS_POSIX)
100   // The outer sandbox on Linux and OSX prevents
101   // sysconf(_SC_NPROCESSORS) from working; in Windows, there are no
102   // problems with invoking GetSystemInfo.  Therefore, only in
103   // OS_POSIX do we need to supply the number of cores into the
104   // NaClChromeMainArgs object.
105   int number_of_cores_;
106 #endif
107 
108   base::WritableSharedMemoryMapping crash_info_shmem_mapping_;
109 
110   std::unique_ptr<NaClTrustedListener> trusted_listener_;
111 
112   NaClIPCAdapter::ResolveFileTokenReplyCallback resolved_cb_;
113 
114   // Used to identify what thread we're on.
115   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
116 
117   typedef std::map<
118     std::string,  // manifest key
119     std::pair<IPC::PlatformFileForTransit,
120               base::FilePath> > PrefetchedResourceFilesMap;
121   PrefetchedResourceFilesMap prefetched_resource_files_;
122 
123   bool is_started_;
124 };
125 
126 #endif  // COMPONENTS_NACL_LOADER_NACL_LISTENER_H_
127