blob: 2093b44cc12d13fce04a1936ee96d7731a073e9a [file] [log] [blame]
Ben Murdochca12bfa2013-07-23 11:17:05 +01001// 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_BROWSER_NACL_HOST_PNACL_HOST_H_
6#define CHROME_BROWSER_NACL_HOST_PNACL_HOST_H_
7
8#include <map>
9
10#include "base/callback_forward.h"
11#include "base/memory/singleton.h"
12#include "base/memory/weak_ptr.h"
13#include "base/threading/thread_checker.h"
14#include "chrome/browser/nacl_host/nacl_file_host.h"
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010015#include "components/nacl/common/pnacl_types.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010016#include "ipc/ipc_platform_file.h"
17
18namespace pnacl {
19class PnaclHostTest;
20class PnaclTranslationCache;
21}
22
23// Shared state (translation cache) and common utilities (temp file creation)
24// for all PNaCl translations. Unless otherwise specified, all methods should be
25// called on the IO thread.
26class PnaclHost {
27 public:
28 typedef base::Callback<void(IPC::PlatformFileForTransit)> TempFileCallback;
29 typedef base::Callback<void(IPC::PlatformFileForTransit, bool is_hit)>
30 NexeFdCallback;
31
32 static PnaclHost* GetInstance();
33
34 PnaclHost();
35 ~PnaclHost();
36 void Init();
37
38 // Creates a temporary file that will be deleted when the last handle
39 // is closed, or earlier. Returns a PlatformFileForTransit usable by the
40 // process identified by |process_handle|.
41 void CreateTemporaryFile(base::ProcessHandle process_handle,
42 TempFileCallback cb);
43
44 // Create a temporary file, which will be deleted by the time the last
45 // handle is closed (or earlier on POSIX systems), to use for the nexe
46 // with the cache information given in |cache_info|. The specific instance
47 // is identified by the combination of |render_process_id| and |pp_instance|.
48 // Returns by calling |cb| with a PlatformFileForTransit usable by the process
49 // identified by |process_handle|. If the nexe is already present
50 // in the cache, |is_hit| is set to true and the contents of the nexe
51 // have been copied into the temporary file. Otherwise |is_hit| is set to
52 // false and the temporary file will be writeable.
53 // Currently the implementation is a stub, which always sets is_hit to false
54 // and calls the implementation of CreateTemporaryFile.
55 // If the cache request was a miss, the caller is expected to call
56 // TranslationFinished after it finishes translation to allow the nexe to be
57 // stored in the cache.
58 void GetNexeFd(int render_process_id,
59 base::ProcessHandle process_handle,
60 int render_view_id,
61 int pp_instance,
62 const nacl::PnaclCacheInfo& cache_info,
63 const NexeFdCallback& cb);
64
65 // Called after the translation of a pexe instance identified by
Ben Murdochbb1529c2013-08-08 10:24:53 +010066 // |render_process_id| and |pp_instance| finishes. If |success| is true,
67 // store the nexe translated for the instance in the cache.
68 void TranslationFinished(int render_process_id,
69 int pp_instance,
70 bool success);
Ben Murdochca12bfa2013-07-23 11:17:05 +010071
72 // Called when the renderer identified by |render_process_id| is closing.
73 // Clean up any outstanding translations for that renderer.
74 void RendererClosing(int render_process_id);
75
76 private:
77 // PnaclHost is a singleton because there is only one translation cache, and
78 // so that the BrowsingDataRemover can clear it even if no translation has
79 // ever been started.
80 friend struct DefaultSingletonTraits<PnaclHost>;
81 friend class pnacl::PnaclHostTest;
82 enum CacheState {
83 CacheUninitialized,
84 CacheInitializing,
85 CacheReady
86 };
87 struct PendingTranslation {
88 public:
89 PendingTranslation();
90 ~PendingTranslation();
91 int render_view_id;
92 IPC::PlatformFileForTransit nexe_fd;
93 NexeFdCallback callback;
94 nacl::PnaclCacheInfo cache_info;
95 };
96
97 typedef std::pair<int, int> TranslationID;
98 typedef std::map<TranslationID, PendingTranslation> PendingTranslationMap;
99
100 void InitForTest(base::FilePath temp_dir);
101 void OnCacheInitialized(int error);
102 static IPC::PlatformFileForTransit DoCreateTemporaryFile(
103 base::ProcessHandle process_handle,
104 base::FilePath temp_dir_);
105 void ReturnMiss(TranslationID id, IPC::PlatformFileForTransit fd);
106
107 CacheState cache_state_;
108 base::FilePath temp_dir_;
109 scoped_ptr<pnacl::PnaclTranslationCache> disk_cache_;
110 PendingTranslationMap pending_translations_;
111 base::ThreadChecker thread_checker_;
112 base::WeakPtrFactory<PnaclHost> weak_factory_;
113 DISALLOW_COPY_AND_ASSIGN(PnaclHost);
114};
115
116#endif // CHROME_BROWSER_NACL_HOST_PNACL_HOST_H_