Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / nacl / browser / nacl_browser.h
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 COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_
6 #define COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_
7
8 #include <deque>
9
10 #include "base/bind.h"
11 #include "base/containers/mru_cache.h"
12 #include "base/files/file.h"
13 #include "base/memory/singleton.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "components/nacl/browser/nacl_browser_delegate.h"
17 #include "components/nacl/browser/nacl_validation_cache.h"
18
19 class URLPattern;
20 class GURL;
21
22 namespace base {
23 class FileProxy;
24 }
25
26 namespace nacl {
27
28 // Open an immutable executable file that can be mmapped.
29 // This function should only be called on a thread that can perform file IO.
30 base::File OpenNaClExecutableImpl(const base::FilePath& file_path);
31
32 // Represents shared state for all NaClProcessHost objects in the browser.
33 class NaClBrowser {
34  public:
35   static NaClBrowser* GetInstance();
36
37   // Will it be possible to launch a NaCl process, eventually?
38   bool IsOk() const;
39
40   // Are we ready to launch a NaCl process now?  Implies IsOk().
41   bool IsReady() const;
42
43   // Attempt to asynchronously acquire all resources needed to start a process.
44   // This method is idempotent - it is safe to call multiple times.
45   void EnsureAllResourcesAvailable();
46
47   // Enqueues reply() in the message loop when all the resources needed to start
48   // a process have been acquired.
49   void WaitForResources(const base::Closure& reply);
50
51   // Asynchronously attempt to get the IRT open.
52   // This is entailed by EnsureInitialized.  This method is exposed as part of
53   // the public interface, however, so the IRT can be explicitly opened as
54   // early as possible to prevent autoupdate issues.
55   void EnsureIrtAvailable();
56
57   // Path to IRT. Available even before IRT is loaded.
58   const base::FilePath& GetIrtFilePath();
59
60   // IRT file handle, only available when IsReady().
61   const base::File& IrtFile() const;
62
63   // Methods for testing GDB debug stub in browser. If test adds debug stub
64   // port listener, Chrome will allocate a currently-unused TCP port number for
65   // debug stub server instead of a fixed one.
66
67   // Notify listener that new debug stub TCP port is allocated.
68   void FireGdbDebugStubPortOpened(int port);
69   bool HasGdbDebugStubPortListener();
70   void SetGdbDebugStubPortListener(base::Callback<void(int)> listener);
71   void ClearGdbDebugStubPortListener();
72
73   bool ValidationCacheIsEnabled() const {
74     return validation_cache_is_enabled_;
75   }
76
77   const std::string& GetValidationCacheKey() const {
78     return validation_cache_.GetValidationCacheKey();
79   }
80
81   // The NaCl singleton keeps information about NaCl executable files opened via
82   // PPAPI.  This allows the NaCl process to get trusted information about the
83   // file directly from the browser process.  In theory, a compromised renderer
84   // could provide a writable file handle or lie about the file's path.  If we
85   // trusted the handle was read only but it was not, an mmapped file could be
86   // modified after validation, allowing an escape from the NaCl sandbox.
87   // Similarly, if we trusted the file path corresponded to the file handle but
88   // it did not, the validation cache could be tricked into bypassing validation
89   // for bad code.
90   // Instead of allowing these attacks, the NaCl process only trusts information
91   // it gets directly from the browser process.  Because the information is
92   // stored in a cache of bounded size, it is not guaranteed the browser process
93   // will be able to provide the requested information.  In these cases, the
94   // NaCl process must make conservative assumptions about the origin of the
95   // file.
96   // In theory, a compromised renderer could guess file tokens in an attempt to
97   // read files it normally doesn't have access to.  This would not compromise
98   // the NaCl sandbox, however, and only has a 1 in ~2**120 chance of success
99   // per guess.
100   // TODO(ncbray): move the cache onto NaClProcessHost so that we don't need to
101   // rely on tokens being unguessable by another process.
102   void PutFilePath(const base::FilePath& path, uint64* file_token_lo,
103                    uint64* file_token_hi);
104   bool GetFilePath(uint64 file_token_lo, uint64 file_token_hi,
105                    base::FilePath* path);
106
107   bool QueryKnownToValidate(const std::string& signature, bool off_the_record);
108   void SetKnownToValidate(const std::string& signature, bool off_the_record);
109   void ClearValidationCache(const base::Closure& callback);
110 #if defined(OS_WIN)
111   // Get path to NaCl loader on the filesystem if possible.
112   // |exe_path| does not change if the method fails.
113   bool GetNaCl64ExePath(base::FilePath* exe_path);
114 #endif
115
116   void EarlyStartup();
117   static void SetDelegate(NaClBrowserDelegate* delegate);
118   static NaClBrowserDelegate* GetDelegate();
119
120   // Support for NaCl crash throttling.
121   // Each time a NaCl module crashes, the browser is notified.
122   void OnProcessCrashed();
123   // If "too many" crashes occur within a given time period, NaCl is throttled
124   // until the rate again drops below the threshold.
125   bool IsThrottled();
126
127  private:
128   friend struct DefaultSingletonTraits<NaClBrowser>;
129
130   enum NaClResourceState {
131     NaClResourceUninitialized,
132     NaClResourceRequested,
133     NaClResourceReady
134   };
135
136   NaClBrowser();
137   ~NaClBrowser();
138
139   void InitIrtFilePath();
140
141   void OpenIrtLibraryFile();
142
143   void OnIrtOpened(scoped_ptr<base::FileProxy> file_proxy,
144                    base::File::Error error_code);
145
146   void InitValidationCacheFilePath();
147   void EnsureValidationCacheAvailable();
148   void OnValidationCacheLoaded(const std::string* data);
149   void RunWithoutValidationCache();
150
151   // Dispatch waiting tasks if we are ready, or if we know we'll never be ready.
152   void CheckWaiting();
153
154   // Indicate that it is impossible to launch a NaCl process.
155   void MarkAsFailed();
156
157   void MarkValidationCacheAsModified();
158   void PersistValidationCache();
159
160   // Singletons get destroyed at shutdown.
161   base::WeakPtrFactory<NaClBrowser> weak_factory_;
162
163   base::File irt_file_;
164   base::FilePath irt_filepath_;
165   NaClResourceState irt_state_;
166   NaClValidationCache validation_cache_;
167   NaClValidationCache off_the_record_validation_cache_;
168   base::FilePath validation_cache_file_path_;
169   bool validation_cache_is_enabled_;
170   bool validation_cache_is_modified_;
171   NaClResourceState validation_cache_state_;
172   base::Callback<void(int)> debug_stub_port_listener_;
173
174   typedef base::HashingMRUCache<std::string, base::FilePath> PathCacheType;
175   PathCacheType path_cache_;
176
177   bool ok_;
178
179   // A list of pending tasks to start NaCl processes.
180   std::vector<base::Closure> waiting_;
181
182   scoped_ptr<NaClBrowserDelegate> browser_delegate_;
183
184   std::deque<base::Time> crash_times_;
185
186   DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
187 };
188
189 } // namespace nacl
190
191 #endif  // COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_