Upstream version 7.36.149.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/memory/scoped_handle.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "chrome/common/chrome_utility_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   CredentialsGetterHostClient();
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 OnGotEncryptedCredentials(const std::vector<uint8>& key_data,
35                                  bool success);
36
37   // Starts the utility process that gets wifi passphrase from system.
38   void StartProcessOnIOThread(
39       const std::string& network_guid,
40       const std::string& public_key,
41       const extensions::NetworkingPrivateServiceClient::CryptoVerify::
42           VerifyAndEncryptCredentialsCallback& callback);
43
44  private:
45   // Callback for reporting the result.
46   extensions::NetworkingPrivateServiceClient::CryptoVerify::
47       VerifyAndEncryptCredentialsCallback callback_;
48
49   DISALLOW_COPY_AND_ASSIGN(CredentialsGetterHostClient);
50 };
51
52 CredentialsGetterHostClient::CredentialsGetterHostClient() {}
53
54 CredentialsGetterHostClient::~CredentialsGetterHostClient() {}
55
56 bool CredentialsGetterHostClient::OnMessageReceived(
57     const IPC::Message& message) {
58   bool handled = true;
59   IPC_BEGIN_MESSAGE_MAP(CredentialsGetterHostClient, message)
60   IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotEncryptedWiFiCredentials,
61                       OnGotEncryptedCredentials)
62   IPC_MESSAGE_UNHANDLED(handled = false)
63   IPC_END_MESSAGE_MAP()
64   return handled;
65 }
66
67 void CredentialsGetterHostClient::OnProcessCrashed(int exit_code) {
68   callback_.Run("", "Process Crashed");
69 }
70
71 void CredentialsGetterHostClient::OnProcessLaunchFailed() {
72   callback_.Run("", "Process Launch Failed");
73 }
74
75 void CredentialsGetterHostClient::OnGotEncryptedCredentials(
76     const std::vector<uint8>& key_data,
77     bool success) {
78   if (success) {
79     std::string base64_encoded_key_data;
80     base::Base64Encode(std::string(key_data.begin(), key_data.end()),
81                        &base64_encoded_key_data);
82     callback_.Run(base64_encoded_key_data, "");
83   } else {
84     callback_.Run("", "Get Credentials Failed");
85   }
86 }
87
88 void CredentialsGetterHostClient::StartProcessOnIOThread(
89     const std::string& network_guid,
90     const std::string& public_key,
91     const extensions::NetworkingPrivateServiceClient::CryptoVerify::
92         VerifyAndEncryptCredentialsCallback& callback) {
93   DCHECK_CURRENTLY_ON(BrowserThread::IO);
94   std::vector<uint8> public_key_data(public_key.begin(), public_key.end());
95   UtilityProcessHost* host =
96       UtilityProcessHost::Create(this, base::MessageLoopProxy::current());
97   callback_ = callback;
98   host->ElevatePrivileges();
99   host->Send(new ChromeUtilityHostMsg_GetAndEncryptWiFiCredentials(
100       network_guid, public_key_data));
101 }
102
103 }  // namespace
104
105 namespace extensions {
106
107 class NetworkingPrivateCredentialsGetterWin
108     : public NetworkingPrivateCredentialsGetter {
109  public:
110   NetworkingPrivateCredentialsGetterWin();
111
112   virtual void Start(
113       const std::string& network_guid,
114       const std::string& public_key,
115       const extensions::NetworkingPrivateServiceClient::CryptoVerify::
116           VerifyAndEncryptCredentialsCallback& callback) OVERRIDE;
117
118  private:
119   virtual ~NetworkingPrivateCredentialsGetterWin();
120
121   DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin);
122 };
123
124 NetworkingPrivateCredentialsGetterWin::NetworkingPrivateCredentialsGetterWin() {
125 }
126
127 void NetworkingPrivateCredentialsGetterWin::Start(
128     const std::string& network_guid,
129     const std::string& public_key,
130     const extensions::NetworkingPrivateServiceClient::CryptoVerify::
131         VerifyAndEncryptCredentialsCallback& callback) {
132   BrowserThread::PostTask(
133       BrowserThread::IO,
134       FROM_HERE,
135       base::Bind(&CredentialsGetterHostClient::StartProcessOnIOThread,
136                  new CredentialsGetterHostClient(),
137                  network_guid,
138                  public_key,
139                  callback));
140 }
141
142 NetworkingPrivateCredentialsGetterWin::
143     ~NetworkingPrivateCredentialsGetterWin() {}
144
145 NetworkingPrivateCredentialsGetter*
146 NetworkingPrivateCredentialsGetter::Create() {
147   return new NetworkingPrivateCredentialsGetterWin();
148 }
149
150 }  // namespace extensions