Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / utility / importer / firefox_importer_unittest_utils.h
1 // Copyright (c) 2011 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_UTILITY_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_
6 #define CHROME_UTILITY_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_
7
8 #include "base/basictypes.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/process/process_handle.h"
12 #include "chrome/utility/importer/nss_decryptor.h"
13 #include "components/autofill/core/common/password_form.h"
14
15 class FFDecryptorServerChannelListener;
16
17 namespace base {
18 class MessageLoopForIO;
19 }
20
21 namespace IPC {
22 class Channel;
23 }  // namespace IPC
24
25 // On OS X NSSDecryptor needs to run in a separate process. To allow us to use
26 // the same unit test on all platforms we use a proxy class which spawns a
27 // child process to do decryption on OS X, and calls through directly
28 // to NSSDecryptor on other platforms.
29 // On OS X:
30 // 2 IPC messages are sent for every method of NSSDecryptor, one containing the
31 // input arguments sent from Server->Child and one coming back containing
32 // the return argument e.g.
33 //
34 // -> Msg_Decryptor_Init(dll_path, db_path)
35 // <- Msg_Decryptor_InitReturnCode(bool)
36 class FFUnitTestDecryptorProxy {
37  public:
38   FFUnitTestDecryptorProxy();
39   virtual ~FFUnitTestDecryptorProxy();
40
41   // Initialize a decryptor, returns true if the object was
42   // constructed successfully.
43   bool Setup(const base::FilePath& nss_path);
44
45   // This match the parallel functions in NSSDecryptor.
46   bool DecryptorInit(const base::FilePath& dll_path,
47                      const base::FilePath& db_path);
48   base::string16 Decrypt(const std::string& crypt);
49   std::vector<autofill::PasswordForm> ParseSignons(
50       const base::FilePath& signons_path);
51
52  private:
53 #if defined(OS_MACOSX)
54   // Blocks until either a timeout is reached, or until the client process
55   // responds to an IPC message.
56   // Returns true if a reply was received successfully and false if the
57   // the operation timed out.
58   bool WaitForClientResponse();
59
60   base::ProcessHandle child_process_;
61   scoped_ptr<IPC::Channel> channel_;
62   scoped_ptr<FFDecryptorServerChannelListener> listener_;
63   scoped_ptr<base::MessageLoopForIO> message_loop_;
64 #else
65   NSSDecryptor decryptor_;
66 #endif  // !OS_MACOSX
67   DISALLOW_COPY_AND_ASSIGN(FFUnitTestDecryptorProxy);
68 };
69
70 // On Non-OSX platforms FFUnitTestDecryptorProxy simply calls through to
71 // NSSDecryptor.
72 #if !defined(OS_MACOSX)
73 FFUnitTestDecryptorProxy::FFUnitTestDecryptorProxy() {
74 }
75
76 FFUnitTestDecryptorProxy::~FFUnitTestDecryptorProxy() {
77 }
78
79 bool FFUnitTestDecryptorProxy::Setup(const base::FilePath& nss_path) {
80   return true;
81 }
82
83 bool FFUnitTestDecryptorProxy::DecryptorInit(const base::FilePath& dll_path,
84                                              const base::FilePath& db_path) {
85   return decryptor_.Init(dll_path, db_path);
86 }
87
88 base::string16 FFUnitTestDecryptorProxy::Decrypt(const std::string& crypt) {
89   return decryptor_.Decrypt(crypt);
90 }
91
92 std::vector<autofill::PasswordForm> FFUnitTestDecryptorProxy::ParseSignons(
93     const base::FilePath& signons_path) {
94   std::vector<autofill::PasswordForm> signons;
95   if (decryptor_.ReadAndParseSignons(signons_path, &signons))
96     return signons;
97
98   return std::vector<autofill::PasswordForm>();
99 }
100
101 #endif  // !OS_MACOSX
102
103 #endif  // CHROME_UTILITY_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_