Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ | 5 #ifndef COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ |
| 6 #define COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ | 6 #define COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "base/callback_forward.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/singleton.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 19 #include "components/nacl/browser/nacl_file_host.h" | 17 #include "components/nacl/browser/nacl_file_host.h" |
| 20 #include "components/nacl/common/pnacl_types.h" | 18 #include "components/nacl/common/pnacl_types.h" |
| 21 #include "ipc/ipc_platform_file.h" | 19 #include "ipc/ipc_platform_file.h" |
| 22 | 20 |
| 23 namespace net { | 21 namespace net { |
| 24 class DrainableIOBuffer; | 22 class DrainableIOBuffer; |
| 25 } | 23 } |
| 26 | 24 |
| 27 namespace pnacl { | 25 namespace pnacl { |
| 28 | 26 |
| 29 class PnaclHostTest; | 27 class PnaclHostTest; |
| 30 class PnaclHostTestDisk; | 28 class PnaclHostTestDisk; |
| 31 class PnaclTranslationCache; | 29 class PnaclTranslationCache; |
| 32 | 30 |
| 33 // Shared state (translation cache) and common utilities (temp file creation) | 31 // Shared state (translation cache) and common utilities (temp file creation) |
| 34 // for all PNaCl translations. Unless otherwise specified, all methods should be | 32 // for all PNaCl translations. Unless otherwise specified, all methods should be |
| 35 // called on the IO thread. | 33 // called on the IO thread. |
| 36 class PnaclHost { | 34 class PnaclHost { |
| 37 public: | 35 public: |
| 38 typedef base::Callback<void(base::File)> TempFileCallback; | 36 typedef base::Callback<void(base::File)> TempFileCallback; |
| 39 typedef base::Callback<void(const base::File&, bool is_hit)> NexeFdCallback; | 37 typedef base::Callback<void(const base::File&, bool is_hit)> NexeFdCallback; |
| 40 | 38 |
| 39 // Gets the PnaclHost singleton instance (creating it if necessary). | |
| 40 // PnaclHost is a singleton because there is only one translation cache, and | |
| 41 // so that the BrowsingDataRemover can clear it even if no translation has | |
| 42 // ever been started. | |
| 41 static PnaclHost* GetInstance(); | 43 static PnaclHost* GetInstance(); |
| 42 | 44 |
| 43 PnaclHost(); | 45 // The PnaclHost instance is intentionally leaked on shutdown. DeInitIfSafe() |
| 44 ~PnaclHost(); | 46 // attempts to cleanup |disk_cache_| earlier, but if it fails to do so in |
| 47 // time, it will be too late when AtExitManager kicks in anways so subscribing | |
|
Mark Seaborn
2016/08/02 21:47:31
Typo: "anyway"
gab
2016/08/02 21:57:42
Done.
| |
| 48 // to it is useless. | |
| 49 ~PnaclHost() = delete; | |
| 45 | 50 |
| 46 // Initialize cache backend. GetNexeFd will also initialize the backend if | 51 // Initialize cache backend. GetNexeFd will also initialize the backend if |
| 47 // necessary, but calling Init ahead of time will minimize the latency. | 52 // necessary, but calling Init ahead of time will minimize the latency. |
| 48 void Init(); | 53 void Init(); |
| 49 | 54 |
| 50 // Creates a temporary file that will be deleted when the last handle | 55 // Creates a temporary file that will be deleted when the last handle |
| 51 // is closed, or earlier. Returns a PlatformFile handle. | 56 // is closed, or earlier. Returns a PlatformFile handle. |
| 52 void CreateTemporaryFile(TempFileCallback cb); | 57 void CreateTemporaryFile(TempFileCallback cb); |
| 53 | 58 |
| 54 // Create a temporary file, which will be deleted by the time the last | 59 // Create a temporary file, which will be deleted by the time the last |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 97 |
| 93 // Doom all entries between |initial_time| and |end_time|. Like disk_cache_, | 98 // Doom all entries between |initial_time| and |end_time|. Like disk_cache_, |
| 94 // PnaclHost supports supports unbounded deletes in either direction by using | 99 // PnaclHost supports supports unbounded deletes in either direction by using |
| 95 // null Time values for either argument. |callback| will be called on the UI | 100 // null Time values for either argument. |callback| will be called on the UI |
| 96 // thread when finished. | 101 // thread when finished. |
| 97 void ClearTranslationCacheEntriesBetween(base::Time initial_time, | 102 void ClearTranslationCacheEntriesBetween(base::Time initial_time, |
| 98 base::Time end_time, | 103 base::Time end_time, |
| 99 const base::Closure& callback); | 104 const base::Closure& callback); |
| 100 | 105 |
| 101 // Return the number of tracked translations or FD requests currently pending. | 106 // Return the number of tracked translations or FD requests currently pending. |
| 102 size_t pending_translations() { return pending_translations_.size(); } | 107 size_t pending_translations() { |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 109 return pending_translations_.size(); | |
| 110 } | |
| 103 | 111 |
| 104 private: | 112 private: |
| 105 // PnaclHost is a singleton because there is only one translation cache, and | |
| 106 // so that the BrowsingDataRemover can clear it even if no translation has | |
| 107 // ever been started. | |
| 108 friend struct base::DefaultSingletonTraits<PnaclHost>; | |
| 109 friend class FileProxy; | 113 friend class FileProxy; |
| 110 friend class pnacl::PnaclHostTest; | 114 friend class PnaclHostTest; |
| 111 friend class pnacl::PnaclHostTestDisk; | 115 friend class PnaclHostTestDisk; |
| 112 enum CacheState { | 116 enum CacheState { |
| 113 CacheUninitialized, | 117 CacheUninitialized, |
| 114 CacheInitializing, | 118 CacheInitializing, |
| 115 CacheReady | 119 CacheReady |
| 116 }; | 120 }; |
| 117 class PendingTranslation { | 121 class PendingTranslation { |
| 118 public: | 122 public: |
| 119 PendingTranslation(); | 123 PendingTranslation(); |
| 120 PendingTranslation(const PendingTranslation& other); | 124 PendingTranslation(const PendingTranslation& other); |
| 121 ~PendingTranslation(); | 125 ~PendingTranslation(); |
| 122 base::ProcessHandle process_handle; | 126 base::ProcessHandle process_handle; |
| 123 int render_view_id; | 127 int render_view_id; |
| 124 base::File* nexe_fd; | 128 base::File* nexe_fd; |
| 125 bool got_nexe_fd; | 129 bool got_nexe_fd; |
| 126 bool got_cache_reply; | 130 bool got_cache_reply; |
| 127 bool got_cache_hit; | 131 bool got_cache_hit; |
| 128 bool is_incognito; | 132 bool is_incognito; |
| 129 scoped_refptr<net::DrainableIOBuffer> nexe_read_buffer; | 133 scoped_refptr<net::DrainableIOBuffer> nexe_read_buffer; |
| 130 NexeFdCallback callback; | 134 NexeFdCallback callback; |
| 131 std::string cache_key; | 135 std::string cache_key; |
| 132 nacl::PnaclCacheInfo cache_info; | 136 nacl::PnaclCacheInfo cache_info; |
| 133 }; | 137 }; |
| 134 | 138 |
| 135 typedef std::pair<int, int> TranslationID; | 139 typedef std::pair<int, int> TranslationID; |
| 136 typedef std::map<TranslationID, PendingTranslation> PendingTranslationMap; | 140 typedef std::map<TranslationID, PendingTranslation> PendingTranslationMap; |
| 141 | |
| 142 PnaclHost(); | |
| 143 | |
| 137 static bool TranslationMayBeCached( | 144 static bool TranslationMayBeCached( |
| 138 const PendingTranslationMap::iterator& entry); | 145 const PendingTranslationMap::iterator& entry); |
| 139 | 146 |
| 140 void InitForTest(base::FilePath temp_dir, bool in_memory); | 147 void InitForTest(base::FilePath temp_dir, bool in_memory); |
| 141 void OnCacheInitialized(int net_error); | 148 void OnCacheInitialized(int net_error); |
| 142 | 149 |
| 143 static void DoCreateTemporaryFile(base::FilePath temp_dir_, | 150 static void DoCreateTemporaryFile(base::FilePath temp_dir_, |
| 144 TempFileCallback cb); | 151 TempFileCallback cb); |
| 145 | 152 |
| 146 // GetNexeFd common steps | 153 // GetNexeFd common steps |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 165 void OnBufferCopiedToTempFile(const TranslationID& id, | 172 void OnBufferCopiedToTempFile(const TranslationID& id, |
| 166 std::unique_ptr<base::File> file, | 173 std::unique_ptr<base::File> file, |
| 167 int file_error); | 174 int file_error); |
| 168 | 175 |
| 169 void OnEntriesDoomed(const base::Closure& callback, int net_error); | 176 void OnEntriesDoomed(const base::Closure& callback, int net_error); |
| 170 | 177 |
| 171 void DeInitIfSafe(); | 178 void DeInitIfSafe(); |
| 172 | 179 |
| 173 // Operations which are pending with the cache backend, which we should | 180 // Operations which are pending with the cache backend, which we should |
| 174 // wait for before destroying it (see comment on DeInitIfSafe). | 181 // wait for before destroying it (see comment on DeInitIfSafe). |
| 175 int pending_backend_operations_; | 182 int pending_backend_operations_ = 0; |
| 176 CacheState cache_state_; | 183 CacheState cache_state_ = CacheUninitialized; |
| 177 base::FilePath temp_dir_; | 184 base::FilePath temp_dir_; |
| 178 std::unique_ptr<pnacl::PnaclTranslationCache> disk_cache_; | 185 std::unique_ptr<PnaclTranslationCache> disk_cache_; |
| 179 PendingTranslationMap pending_translations_; | 186 PendingTranslationMap pending_translations_; |
| 180 base::ThreadChecker thread_checker_; | 187 base::ThreadChecker thread_checker_; |
| 181 base::WeakPtrFactory<PnaclHost> weak_factory_; | |
| 182 DISALLOW_COPY_AND_ASSIGN(PnaclHost); | 188 DISALLOW_COPY_AND_ASSIGN(PnaclHost); |
| 183 }; | 189 }; |
| 184 | 190 |
| 185 } // namespace pnacl | 191 } // namespace pnacl |
| 186 | 192 |
| 187 #endif // COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ | 193 #endif // COMPONENTS_NACL_BROWSER_PNACL_HOST_H_ |
| OLD | NEW |