OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "components/nacl/browser/nacl_file_host.h" | 5 #include "components/nacl/browser/nacl_file_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 // in file name limit error-handling-code-paths, etc. | 31 // in file name limit error-handling-code-paths, etc. |
32 const size_t kMaxFileLength = 40; | 32 const size_t kMaxFileLength = 40; |
33 | 33 |
34 void NotifyRendererOfError( | 34 void NotifyRendererOfError( |
35 nacl::NaClHostMessageFilter* nacl_host_message_filter, | 35 nacl::NaClHostMessageFilter* nacl_host_message_filter, |
36 IPC::Message* reply_msg) { | 36 IPC::Message* reply_msg) { |
37 reply_msg->set_reply_error(); | 37 reply_msg->set_reply_error(); |
38 nacl_host_message_filter->Send(reply_msg); | 38 nacl_host_message_filter->Send(reply_msg); |
39 } | 39 } |
40 | 40 |
41 base::File PnaclDoOpenFile(const base::FilePath& file_to_open) { | 41 // Make a wrapper function for the NaClHostMsg_GetReadonlyPnaclFD macro, |
42 return base::File(file_to_open, | 42 // so that there is a function pointer. |
teravest
2014/06/26 21:19:24
I'm confused. Why can't you make a function pointe
jvoung (off chromium)
2014/06/26 22:48:39
Hmm, Bind was failing to infer the template parame
| |
43 base::File::FLAG_OPEN | base::File::FLAG_READ); | 43 void WriteGetReadonlyPnaclFDReply(IPC::Message* reply_msg, |
44 IPC::PlatformFileForTransit file_desc, | |
45 uint64 file_token_lo, | |
46 uint64 file_token_hi) { | |
47 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(reply_msg, | |
48 file_desc, | |
49 file_token_lo, | |
50 file_token_hi); | |
51 } | |
52 | |
53 // Make a wrapper function for the NaClHostMsg_OpenNaClExecutable macro, | |
54 // so that there is a function pointer. | |
55 void WriteOpenNaClExecutableReply(IPC::Message* reply_msg, | |
56 IPC::PlatformFileForTransit file_desc, | |
57 uint64 file_token_lo, | |
58 uint64 file_token_hi) { | |
59 NaClHostMsg_OpenNaClExecutable::WriteReplyParams(reply_msg, | |
60 file_desc, | |
61 file_token_lo, | |
62 file_token_hi); | |
63 } | |
64 | |
65 void DoRegisterOpenedNaClExecutableFile( | |
66 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | |
67 base::File file, | |
teravest
2014/06/26 21:19:24
something is funky in the indentation for the argu
jvoung (off chromium)
2014/06/26 22:48:39
Done.
| |
68 base::FilePath file_path, | |
69 IPC::Message* reply_msg, | |
70 void (*WriteReplyParams)(IPC::Message* msg, | |
71 IPC::PlatformFileForTransit desc, | |
72 uint64 lo_token, | |
73 uint64 hi_token)) { | |
74 // IO thread owns the NaClBrowser singleton. | |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
76 | |
77 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | |
78 uint64 file_token_lo = 0; | |
79 uint64 file_token_hi = 0; | |
80 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | |
81 | |
82 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | |
83 file.Pass(), | |
84 nacl_host_message_filter->PeerHandle()); | |
85 | |
86 (*WriteReplyParams)(reply_msg, file_desc, file_token_lo, file_token_hi); | |
87 nacl_host_message_filter->Send(reply_msg); | |
44 } | 88 } |
45 | 89 |
46 void DoOpenPnaclFile( | 90 void DoOpenPnaclFile( |
47 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 91 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
48 const std::string& filename, | 92 const std::string& filename, |
93 bool is_executable, | |
49 IPC::Message* reply_msg) { | 94 IPC::Message* reply_msg) { |
50 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 95 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
51 base::FilePath full_filepath; | 96 base::FilePath full_filepath; |
52 | 97 |
53 // PNaCl must be installed. | 98 // PNaCl must be installed. |
54 base::FilePath pnacl_dir; | 99 base::FilePath pnacl_dir; |
55 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || | 100 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || |
56 !base::PathExists(pnacl_dir)) { | 101 !base::PathExists(pnacl_dir)) { |
57 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 102 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
58 return; | 103 return; |
59 } | 104 } |
60 | 105 |
61 // Do some validation. | 106 // Do some validation. |
62 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { | 107 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { |
63 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 108 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
64 return; | 109 return; |
65 } | 110 } |
66 | 111 |
67 base::File file_to_open = PnaclDoOpenFile(full_filepath); | 112 base::File file_to_open = nacl::OpenNaClReadExecImpl(full_filepath, |
113 is_executable); | |
68 if (!file_to_open.IsValid()) { | 114 if (!file_to_open.IsValid()) { |
69 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 115 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
70 return; | 116 return; |
71 } | 117 } |
72 | 118 |
73 // Send the reply! | 119 // This function is running on the blocking pool, but the path needs to be |
74 // Do any DuplicateHandle magic that is necessary first. | 120 // registered in a structure owned by the IO thread. |
75 IPC::PlatformFileForTransit target_desc = | 121 // Not all PNaCl files are executable. Only register those that are |
76 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | 122 // executable in the NaCl file_path cache. |
77 nacl_host_message_filter->PeerHandle()); | 123 if (is_executable) { |
78 if (target_desc == IPC::InvalidPlatformFileForTransit()) { | 124 BrowserThread::PostTask( |
79 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 125 BrowserThread::IO, FROM_HERE, |
80 return; | 126 base::Bind(&DoRegisterOpenedNaClExecutableFile, |
127 nacl_host_message_filter, | |
128 Passed(file_to_open.Pass()), full_filepath, reply_msg, | |
129 &WriteGetReadonlyPnaclFDReply)); | |
130 } else { | |
131 IPC::PlatformFileForTransit target_desc = | |
132 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | |
133 nacl_host_message_filter->PeerHandle()); | |
134 WriteGetReadonlyPnaclFDReply(reply_msg, target_desc, 0, 0); | |
135 nacl_host_message_filter->Send(reply_msg); | |
81 } | 136 } |
82 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | |
83 reply_msg, target_desc); | |
84 nacl_host_message_filter->Send(reply_msg); | |
85 } | |
86 | |
87 void DoRegisterOpenedNaClExecutableFile( | |
88 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | |
89 base::File file, | |
90 base::FilePath file_path, | |
91 IPC::Message* reply_msg) { | |
92 // IO thread owns the NaClBrowser singleton. | |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
94 | |
95 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | |
96 uint64 file_token_lo = 0; | |
97 uint64 file_token_hi = 0; | |
98 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | |
99 | |
100 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | |
101 file.Pass(), | |
102 nacl_host_message_filter->PeerHandle()); | |
103 | |
104 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | |
105 reply_msg, file_desc, file_token_lo, file_token_hi); | |
106 nacl_host_message_filter->Send(reply_msg); | |
107 } | 137 } |
108 | 138 |
109 // Convert the file URL into a file descriptor. | 139 // Convert the file URL into a file descriptor. |
110 // This function is security sensitive. Be sure to check with a security | 140 // This function is security sensitive. Be sure to check with a security |
111 // person before you modify it. | 141 // person before you modify it. |
112 void DoOpenNaClExecutableOnThreadPool( | 142 void DoOpenNaClExecutableOnThreadPool( |
113 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 143 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
114 const GURL& file_url, | 144 const GURL& file_url, |
115 IPC::Message* reply_msg) { | 145 IPC::Message* reply_msg) { |
116 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 146 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
117 | 147 |
118 base::FilePath file_path; | 148 base::FilePath file_path; |
119 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 149 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
120 file_url, | 150 file_url, |
121 true /* use_blocking_api */, | 151 true /* use_blocking_api */, |
122 nacl_host_message_filter->profile_directory(), | 152 nacl_host_message_filter->profile_directory(), |
123 &file_path)) { | 153 &file_path)) { |
124 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 154 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
125 return; | 155 return; |
126 } | 156 } |
127 | 157 |
128 base::File file = nacl::OpenNaClExecutableImpl(file_path); | 158 base::File file = nacl::OpenNaClReadExecImpl(file_path, |
159 true /* is_executable */); | |
129 if (file.IsValid()) { | 160 if (file.IsValid()) { |
130 // This function is running on the blocking pool, but the path needs to be | 161 // This function is running on the blocking pool, but the path needs to be |
131 // registered in a structure owned by the IO thread. | 162 // registered in a structure owned by the IO thread. |
132 BrowserThread::PostTask( | 163 BrowserThread::PostTask( |
133 BrowserThread::IO, FROM_HERE, | 164 BrowserThread::IO, FROM_HERE, |
134 base::Bind( | 165 base::Bind( |
135 &DoRegisterOpenedNaClExecutableFile, | 166 &DoRegisterOpenedNaClExecutableFile, |
136 nacl_host_message_filter, | 167 nacl_host_message_filter, |
137 Passed(file.Pass()), file_path, reply_msg)); | 168 Passed(file.Pass()), file_path, reply_msg, |
169 &WriteOpenNaClExecutableReply)); | |
138 } else { | 170 } else { |
139 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 171 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
140 return; | 172 return; |
141 } | 173 } |
142 } | 174 } |
143 | 175 |
144 } // namespace | 176 } // namespace |
145 | 177 |
146 namespace nacl_file_host { | 178 namespace nacl_file_host { |
147 | 179 |
148 void GetReadonlyPnaclFd( | 180 void GetReadonlyPnaclFd( |
149 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 181 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
150 const std::string& filename, | 182 const std::string& filename, |
183 bool is_executable, | |
151 IPC::Message* reply_msg) { | 184 IPC::Message* reply_msg) { |
152 if (!BrowserThread::PostBlockingPoolTask( | 185 if (!BrowserThread::PostBlockingPoolTask( |
153 FROM_HERE, | 186 FROM_HERE, |
154 base::Bind(&DoOpenPnaclFile, | 187 base::Bind(&DoOpenPnaclFile, |
155 nacl_host_message_filter, | 188 nacl_host_message_filter, |
156 filename, | 189 filename, |
190 is_executable, | |
157 reply_msg))) { | 191 reply_msg))) { |
158 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 192 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
159 } | 193 } |
160 } | 194 } |
161 | 195 |
162 // This function is security sensitive. Be sure to check with a security | 196 // This function is security sensitive. Be sure to check with a security |
163 // person before you modify it. | 197 // person before you modify it. |
164 bool PnaclCanOpenFile(const std::string& filename, | 198 bool PnaclCanOpenFile(const std::string& filename, |
165 base::FilePath* file_to_open) { | 199 base::FilePath* file_to_open) { |
166 if (filename.length() > kMaxFileLength) | 200 if (filename.length() > kMaxFileLength) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 FROM_HERE, | 265 FROM_HERE, |
232 base::Bind( | 266 base::Bind( |
233 &DoOpenNaClExecutableOnThreadPool, | 267 &DoOpenNaClExecutableOnThreadPool, |
234 nacl_host_message_filter, | 268 nacl_host_message_filter, |
235 file_url, reply_msg))) { | 269 file_url, reply_msg))) { |
236 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 270 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
237 } | 271 } |
238 } | 272 } |
239 | 273 |
240 } // namespace nacl_file_host | 274 } // namespace nacl_file_host |
OLD | NEW |