| 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 "chrome/utility/importer/firefox_importer_unittest_utils.h" | 5 #include "chrome/utility/importer/firefox_importer_unittest_utils.h" |
| 6 | 6 |
| 7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 listener_->QuitClient(); | 172 listener_->QuitClient(); |
| 173 channel_->Close(); | 173 channel_->Close(); |
| 174 | 174 |
| 175 if (child_process_.IsValid()) { | 175 if (child_process_.IsValid()) { |
| 176 int exit_code; | 176 int exit_code; |
| 177 child_process_.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(5), | 177 child_process_.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(5), |
| 178 &exit_code); | 178 &exit_code); |
| 179 } | 179 } |
| 180 } | 180 } |
| 181 | 181 |
| 182 // A message_loop task that quits the message loop when invoked, setting cancel | |
| 183 // causes the task to do nothing when invoked. | |
| 184 class CancellableQuitMsgLoop : public base::RefCounted<CancellableQuitMsgLoop> { | |
| 185 public: | |
| 186 CancellableQuitMsgLoop() : cancelled_(false) {} | |
| 187 void QuitNow() { | |
| 188 if (!cancelled_) | |
| 189 base::MessageLoop::current()->QuitWhenIdle(); | |
| 190 } | |
| 191 bool cancelled_; | |
| 192 | |
| 193 private: | |
| 194 friend class base::RefCounted<CancellableQuitMsgLoop>; | |
| 195 ~CancellableQuitMsgLoop() {} | |
| 196 }; | |
| 197 | |
| 198 // Spin until either a client response arrives or a timeout occurs. | 182 // Spin until either a client response arrives or a timeout occurs. |
| 199 bool FFUnitTestDecryptorProxy::WaitForClientResponse() { | 183 void FFUnitTestDecryptorProxy::WaitForClientResponse() { |
| 200 // What we're trying to do here is to wait for an RPC message to go over the | 184 // What we're trying to do here is to wait for an RPC message to go over the |
| 201 // wire and the client to reply. If the client does not reply by a given | 185 // wire and the client to reply. If the client does not reply by a given |
| 202 // timeout we kill the message loop. | 186 // timeout we kill the message loop. |
| 203 // The way we do this is to post a CancellableQuitMsgLoop for 3 seconds in | |
| 204 // the future and cancel it if an RPC message comes back earlier. | |
| 205 // This relies on the IPC listener class to quit the message loop itself when | 187 // This relies on the IPC listener class to quit the message loop itself when |
| 206 // a message comes in. | 188 // a message comes in. |
| 207 scoped_refptr<CancellableQuitMsgLoop> quit_task( | 189 base::RunLoop run_loop; |
| 208 new CancellableQuitMsgLoop()); | |
| 209 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 190 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 210 FROM_HERE, base::Bind(&CancellableQuitMsgLoop::QuitNow, quit_task.get()), | 191 FROM_HERE, run_loop.QuitWhenIdleClosure(), |
| 211 TestTimeouts::action_max_timeout()); | 192 TestTimeouts::action_max_timeout()); |
| 212 | 193 run_loop.Run(); |
| 213 message_loop_->Run(); | |
| 214 bool ret = !quit_task->cancelled_; | |
| 215 quit_task->cancelled_ = false; | |
| 216 return ret; | |
| 217 } | 194 } |
| 218 | 195 |
| 219 bool FFUnitTestDecryptorProxy::DecryptorInit(const base::FilePath& dll_path, | 196 bool FFUnitTestDecryptorProxy::DecryptorInit(const base::FilePath& dll_path, |
| 220 const base::FilePath& db_path) { | 197 const base::FilePath& db_path) { |
| 221 channel_->Send(new Msg_Decryptor_Init(dll_path, db_path)); | 198 channel_->Send(new Msg_Decryptor_Init(dll_path, db_path)); |
| 222 bool ok = WaitForClientResponse(); | 199 WaitForClientResponse(); |
| 223 if (ok && listener_->got_result) { | 200 if (listener_->got_result) { |
| 224 listener_->got_result = false; | 201 listener_->got_result = false; |
| 225 return listener_->result_bool; | 202 return listener_->result_bool; |
| 226 } | 203 } |
| 227 return false; | 204 return false; |
| 228 } | 205 } |
| 229 | 206 |
| 230 base::string16 FFUnitTestDecryptorProxy::Decrypt(const std::string& crypt) { | 207 base::string16 FFUnitTestDecryptorProxy::Decrypt(const std::string& crypt) { |
| 231 channel_->Send(new Msg_Decrypt(crypt)); | 208 channel_->Send(new Msg_Decrypt(crypt)); |
| 232 bool ok = WaitForClientResponse(); | 209 WaitForClientResponse(); |
| 233 if (ok && listener_->got_result) { | 210 if (listener_->got_result) { |
| 234 listener_->got_result = false; | 211 listener_->got_result = false; |
| 235 return listener_->result_string; | 212 return listener_->result_string; |
| 236 } | 213 } |
| 237 return base::string16(); | 214 return base::string16(); |
| 238 } | 215 } |
| 239 | 216 |
| 240 std::vector<autofill::PasswordForm> FFUnitTestDecryptorProxy::ParseSignons( | 217 std::vector<autofill::PasswordForm> FFUnitTestDecryptorProxy::ParseSignons( |
| 241 const base::FilePath& signons_path) { | 218 const base::FilePath& signons_path) { |
| 242 channel_->Send(new Msg_ParseSignons(signons_path)); | 219 channel_->Send(new Msg_ParseSignons(signons_path)); |
| 243 bool ok = WaitForClientResponse(); | 220 WaitForClientResponse(); |
| 244 if (ok && listener_->got_result) { | 221 if (listener_->got_result) { |
| 245 listener_->got_result = false; | 222 listener_->got_result = false; |
| 246 return listener_->result_vector; | 223 return listener_->result_vector; |
| 247 } | 224 } |
| 248 return std::vector<autofill::PasswordForm>(); | 225 return std::vector<autofill::PasswordForm>(); |
| 249 } | 226 } |
| 250 | 227 |
| 251 //---------------------------- Child Process ----------------------- | 228 //---------------------------- Child Process ----------------------- |
| 252 | 229 |
| 253 // Class to listen on the client side of the ipc channel, it calls through | 230 // Class to listen on the client side of the ipc channel, it calls through |
| 254 // to the NSSDecryptor and sends back a reply. | 231 // to the NSSDecryptor and sends back a reply. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 std::unique_ptr<IPC::Channel> channel = | 293 std::unique_ptr<IPC::Channel> channel = |
| 317 IPC::Channel::CreateClient(mojo_handle.release(), &listener); | 294 IPC::Channel::CreateClient(mojo_handle.release(), &listener); |
| 318 CHECK(channel->Connect()); | 295 CHECK(channel->Connect()); |
| 319 listener.SetSender(channel.get()); | 296 listener.SetSender(channel.get()); |
| 320 | 297 |
| 321 // run message loop | 298 // run message loop |
| 322 base::RunLoop().Run(); | 299 base::RunLoop().Run(); |
| 323 | 300 |
| 324 return 0; | 301 return 0; |
| 325 } | 302 } |
| OLD | NEW |