Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / networking_private / networking_private_credentials_getter_win.cc
1 // Copyright 2014 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 #include "chrome/browser/extensions/api/networking_private/networking_private_credentials_getter.h"
6
7 #include "base/base64.h"
8 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
12 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/utility_process_host.h"
15
16 using content::BrowserThread;
17 using content::UtilityProcessHost;
18 using extensions::NetworkingPrivateCredentialsGetter;
19
20 namespace {
21
22 class CredentialsGetterHostClient : public content::UtilityProcessHostClient {
23  public:
24   explicit CredentialsGetterHostClient(const std::string& public_key);
25
26   virtual ~CredentialsGetterHostClient();
27
28   // UtilityProcessHostClient
29   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
30   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
31   virtual void OnProcessLaunchFailed() OVERRIDE;
32
33   // IPC message handlers.
34   void OnGotCredentials(const std::string& key_data, bool success);
35
36   // Starts the utility process that gets wifi passphrase from system.
37   void StartProcessOnIOThread(
38       const std::string& network_guid,
39       const extensions::NetworkingPrivateServiceClient::CryptoVerify::
40           VerifyAndEncryptCredentialsCallback& callback);
41
42  private:
43   // Public key used to encrypt results
44   std::vector<uint8> public_key_;
45
46   // Callback for reporting the result.
47   extensions::NetworkingPrivateServiceClient::CryptoVerify::
48       VerifyAndEncryptCredentialsCallback callback_;
49
50   DISALLOW_COPY_AND_ASSIGN(CredentialsGetterHostClient);
51 };
52
53 CredentialsGetterHostClient::CredentialsGetterHostClient(
54     const std::string& public_key)
55     : public_key_(public_key.begin(), public_key.end()) {
56 }
57
58 CredentialsGetterHostClient::~CredentialsGetterHostClient() {}
59
60 bool CredentialsGetterHostClient::OnMessageReceived(
61     const IPC::Message& message) {
62   bool handled = true;
63   IPC_BEGIN_MESSAGE_MAP(CredentialsGetterHostClient, message)
64   IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotWiFiCredentials, OnGotCredentials)
65   IPC_MESSAGE_UNHANDLED(handled = false)
66   IPC_END_MESSAGE_MAP()
67   return handled;
68 }
69
70 void CredentialsGetterHostClient::OnProcessCrashed(int exit_code) {
71   callback_.Run("", "Process Crashed");
72 }
73
74 void CredentialsGetterHostClient::OnProcessLaunchFailed() {
75   callback_.Run("", "Process Launch Failed");
76 }
77
78 void CredentialsGetterHostClient::OnGotCredentials(const std::string& key_data,
79                                                    bool success) {
80   if (success) {
81     std::vector<uint8> ciphertext;
82     if (!networking_private_crypto::EncryptByteString(
83             public_key_, key_data, &ciphertext)) {
84       callback_.Run("", "Encrypt Credentials Failed");
85       return;
86     }
87
88     std::string base64_encoded_key_data;
89     base::Base64Encode(std::string(ciphertext.begin(), ciphertext.end()),
90                        &base64_encoded_key_data);
91     callback_.Run(base64_encoded_key_data, "");
92   } else {
93     callback_.Run("", "Get Credentials Failed");
94   }
95 }
96
97 void CredentialsGetterHostClient::StartProcessOnIOThread(
98     const std::string& network_guid,
99     const extensions::NetworkingPrivateServiceClient::CryptoVerify::
100         VerifyAndEncryptCredentialsCallback& callback) {
101   DCHECK_CURRENTLY_ON(BrowserThread::IO);
102   UtilityProcessHost* host =
103       UtilityProcessHost::Create(this, base::MessageLoopProxy::current());
104   callback_ = callback;
105   host->ElevatePrivileges();
106   host->Send(new ChromeUtilityHostMsg_GetWiFiCredentials(network_guid));
107 }
108
109 }  // namespace
110
111 namespace extensions {
112
113 class NetworkingPrivateCredentialsGetterWin
114     : public NetworkingPrivateCredentialsGetter {
115  public:
116   NetworkingPrivateCredentialsGetterWin();
117
118   virtual void Start(
119       const std::string& network_guid,
120       const std::string& public_key,
121       const extensions::NetworkingPrivateServiceClient::CryptoVerify::
122           VerifyAndEncryptCredentialsCallback& callback) OVERRIDE;
123
124  private:
125   virtual ~NetworkingPrivateCredentialsGetterWin();
126
127   DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin);
128 };
129
130 NetworkingPrivateCredentialsGetterWin::NetworkingPrivateCredentialsGetterWin() {
131 }
132
133 void NetworkingPrivateCredentialsGetterWin::Start(
134     const std::string& network_guid,
135     const std::string& public_key,
136     const extensions::NetworkingPrivateServiceClient::CryptoVerify::
137         VerifyAndEncryptCredentialsCallback& callback) {
138   BrowserThread::PostTask(
139       BrowserThread::IO,
140       FROM_HERE,
141       base::Bind(&CredentialsGetterHostClient::StartProcessOnIOThread,
142                  new CredentialsGetterHostClient(public_key),
143                  network_guid,
144                  callback));
145 }
146
147 NetworkingPrivateCredentialsGetterWin::
148     ~NetworkingPrivateCredentialsGetterWin() {}
149
150 NetworkingPrivateCredentialsGetter*
151 NetworkingPrivateCredentialsGetter::Create() {
152   return new NetworkingPrivateCredentialsGetterWin();
153 }
154
155 }  // namespace extensions