Chromium Code Reviews| Index: chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_win.cc |
| diff --git a/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_win.cc b/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_win.cc |
| index b5a047d769f58e33580a76e9f581845f1292c9f9..592a67d9b72dd53e75d4f6b2fe9b87d90855a3d9 100644 |
| --- a/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_win.cc |
| +++ b/chrome/browser/extensions/api/networking_private/networking_private_credentials_getter_win.cc |
| @@ -10,81 +10,105 @@ |
| #include "base/bind.h" |
| #include "base/macros.h" |
| #include "base/strings/string_piece.h" |
| -#include "base/strings/stringprintf.h" |
| -#include "base/threading/sequenced_worker_pool.h" |
| -#include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/extensions/api/networking_private/networking_private_crypto.h" |
| -#include "chrome/common/extensions/chrome_utility_extensions_messages.h" |
| +#include "chrome/common/extensions/wifi_credentials.mojom.h" |
| #include "chrome/grit/generated_resources.h" |
| #include "content/public/browser/browser_thread.h" |
| -#include "content/public/browser/utility_process_host.h" |
| -#include "content/public/browser/utility_process_host_client.h" |
| +#include "content/public/browser/utility_process_mojo_client.h" |
| #include "ui/base/l10n/l10n_util.h" |
| -using content::BrowserThread; |
| -using content::UtilityProcessHost; |
| -using content::UtilityProcessHostClient; |
| -using extensions::NetworkingPrivateCredentialsGetter; |
| - |
| namespace { |
| -class CredentialsGetterHostClient : public UtilityProcessHostClient { |
| - public: |
| - explicit CredentialsGetterHostClient(const std::string& public_key); |
| - |
| - // UtilityProcessHostClient |
| - bool OnMessageReceived(const IPC::Message& message) override; |
| - void OnProcessCrashed(int exit_code) override; |
| - void OnProcessLaunchFailed(int error_code) override; |
| +using extensions::NetworkingPrivateCredentialsGetter; |
| - // IPC message handlers. |
| - void OnGotCredentials(const std::string& key_data, bool success); |
| +class CredentialsGetterHostClient { |
| + public: |
| + static CredentialsGetterHostClient* Create(const std::string& public_key) { |
| + return new CredentialsGetterHostClient(public_key); |
| + } |
| - // Starts the utility process that gets wifi passphrase from system. |
| - void StartProcessOnIOThread( |
| + void GetWiFiCredentialsOnIOThread( |
| const std::string& network_guid, |
| const NetworkingPrivateCredentialsGetter::CredentialsCallback& callback); |
| private: |
| - ~CredentialsGetterHostClient() override; |
| + explicit CredentialsGetterHostClient(const std::string& public_key) |
| + : public_key_(public_key.begin(), public_key.end()) {} |
| + |
| + ~CredentialsGetterHostClient() = default; |
| + |
| + // Credentials result handler. |
| + void GetWiFiCredentialsDone(bool success, const std::string& key_data); |
| - // Public key used to encrypt results |
| + // Report the result to |callback_|. |
| + void ReportResult(bool success, const std::string& key_data); |
| + |
| + // Public key used to encrypt the result. |
| std::vector<uint8_t> public_key_; |
| - // Callback for reporting the result. |
| + // Callback for reporting the encrypted result. |
| NetworkingPrivateCredentialsGetter::CredentialsCallback callback_; |
| + // Utility process used to get the credentials. |
| + std::unique_ptr< |
| + content::UtilityProcessMojoClient<extensions::mojom::WiFiCredentials>> |
| + utility_process_mojo_client_; |
| + |
| + // WiFi network to get the credentials from. |
| + std::string wifi_network_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(CredentialsGetterHostClient); |
| }; |
| -CredentialsGetterHostClient::CredentialsGetterHostClient( |
| - const std::string& public_key) |
| - : public_key_(public_key.begin(), public_key.end()) { |
| -} |
| +void CredentialsGetterHostClient::GetWiFiCredentialsOnIOThread( |
| + const std::string& network_guid, |
| + const NetworkingPrivateCredentialsGetter::CredentialsCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + DCHECK(!utility_process_mojo_client_); |
| + DCHECK(!callback.is_null()); |
| -bool CredentialsGetterHostClient::OnMessageReceived( |
| - const IPC::Message& message) { |
| - bool handled = true; |
| - IPC_BEGIN_MESSAGE_MAP(CredentialsGetterHostClient, message) |
| - IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GotWiFiCredentials, OnGotCredentials) |
| - IPC_MESSAGE_UNHANDLED(handled = false) |
| - IPC_END_MESSAGE_MAP() |
| - return handled; |
| -} |
| + wifi_network_ = network_guid; |
| + callback_ = callback; |
| + |
| + const base::string16 utility_process_name = l10n_util::GetStringUTF16( |
| + IDS_UTILITY_PROCESS_WIFI_CREDENTIALS_GETTER_NAME); |
| + |
| + utility_process_mojo_client_.reset( |
| + new content::UtilityProcessMojoClient<extensions::mojom::WiFiCredentials>( |
| + utility_process_name)); |
| + utility_process_mojo_client_->set_error_callback( |
| + base::Bind(&CredentialsGetterHostClient::GetWiFiCredentialsDone, |
| + base::Unretained(this), false, std::string())); |
| + |
| + utility_process_mojo_client_->set_run_elevated(); |
| + |
| + utility_process_mojo_client_->Start(); // Start the utility process. |
| -void CredentialsGetterHostClient::OnProcessCrashed(int exit_code) { |
| - callback_.Run( |
| - "", base::StringPrintf("Process Crashed with code %08x.", exit_code)); |
| + utility_process_mojo_client_->service()->GetWiFiCredentials( |
| + wifi_network_, |
| + base::Bind(&CredentialsGetterHostClient::GetWiFiCredentialsDone, |
| + base::Unretained(this))); |
| } |
| -void CredentialsGetterHostClient::OnProcessLaunchFailed(int error_code) { |
| - callback_.Run("", base::StringPrintf("Process Launch Failed with code %08x.", |
| - error_code)); |
| +void CredentialsGetterHostClient::GetWiFiCredentialsDone( |
| + bool success, |
| + const std::string& key_data) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + utility_process_mojo_client_.reset(); // Terminate the utility process. |
| + ReportResult(success, key_data); |
| + delete this; |
| } |
| -void CredentialsGetterHostClient::OnGotCredentials(const std::string& key_data, |
| - bool success) { |
| +void CredentialsGetterHostClient::ReportResult(bool success, |
| + const std::string& key_data) { |
| if (success) { |
| + if (wifi_network_ == "chrome://test-wifi-get-key-from-system") { |
| + DCHECK_EQ(wifi_network_, key_data); |
| + callback_.Run(key_data, ""); |
| + return; |
| + } |
| + |
| std::vector<uint8_t> ciphertext; |
| if (!networking_private_crypto::EncryptByteString( |
| public_key_, key_data, &ciphertext)) { |
| @@ -103,22 +127,6 @@ void CredentialsGetterHostClient::OnGotCredentials(const std::string& key_data, |
| } |
| } |
| -void CredentialsGetterHostClient::StartProcessOnIOThread( |
| - const std::string& network_guid, |
| - const NetworkingPrivateCredentialsGetter::CredentialsCallback& callback) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - callback_ = callback; |
| - UtilityProcessHost* host = |
| - UtilityProcessHost::Create(this, base::ThreadTaskRunnerHandle::Get()); |
| - host->SetName(l10n_util::GetStringUTF16( |
| - IDS_UTILITY_PROCESS_WIFI_CREDENTIALS_GETTER_NAME)); |
| - host->ElevatePrivileges(); |
| - host->Send(new ChromeUtilityHostMsg_GetWiFiCredentials(network_guid)); |
| -} |
| - |
| -CredentialsGetterHostClient::~CredentialsGetterHostClient() { |
| -} |
| - |
| } // namespace |
| namespace extensions { |
| @@ -126,37 +134,35 @@ namespace extensions { |
| class NetworkingPrivateCredentialsGetterWin |
| : public NetworkingPrivateCredentialsGetter { |
| public: |
| - NetworkingPrivateCredentialsGetterWin(); |
| + NetworkingPrivateCredentialsGetterWin() = default; |
| void Start(const std::string& network_guid, |
| const std::string& public_key, |
| - const CredentialsCallback& callback) override; |
| + const CredentialsCallback& callback) override { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind( |
| + &NetworkingPrivateCredentialsGetterWin::GetCredentialsOnIOThread, |
| + network_guid, public_key, callback)); |
| + } |
| private: |
| - ~NetworkingPrivateCredentialsGetterWin() override; |
| + static void GetCredentialsOnIOThread(const std::string& network_guid, |
| + const std::string& public_key, |
| + const CredentialsCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + // CredentialsGetterHostClient is self deleting. |
| + CredentialsGetterHostClient* client = |
| + CredentialsGetterHostClient::Create(public_key); |
| + client->GetWiFiCredentialsOnIOThread(network_guid, callback); |
| + } |
| + |
| + ~NetworkingPrivateCredentialsGetterWin() override = default; |
|
Sam McNally
2017/01/16 04:16:20
Destructors before methods.
Noel Gordon
2017/01/16 11:09:58
Done.
|
| DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin); |
| }; |
| -NetworkingPrivateCredentialsGetterWin::NetworkingPrivateCredentialsGetterWin() { |
| -} |
| - |
| -void NetworkingPrivateCredentialsGetterWin::Start( |
| - const std::string& network_guid, |
| - const std::string& public_key, |
| - const CredentialsCallback& callback) { |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(&CredentialsGetterHostClient::StartProcessOnIOThread, |
| - new CredentialsGetterHostClient(public_key), |
| - network_guid, |
| - callback)); |
| -} |
| - |
| -NetworkingPrivateCredentialsGetterWin:: |
| - ~NetworkingPrivateCredentialsGetterWin() {} |
| - |
| NetworkingPrivateCredentialsGetter* |
| NetworkingPrivateCredentialsGetter::Create() { |
| return new NetworkingPrivateCredentialsGetterWin(); |