| 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..824a4a217bdacea0fe1793c25c0fbd21d5ec2e1b 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,113 +10,123 @@
|
| #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_getter.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);
|
| +
|
| + // Report the result to |callback_|.
|
| + void ReportResult(bool success, const std::string& key_data);
|
|
|
| - // Public key used to encrypt results
|
| + // 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::WiFiCredentialsGetter>>
|
| + 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::WiFiCredentialsGetter>(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();
|
|
|
| -void CredentialsGetterHostClient::OnProcessCrashed(int exit_code) {
|
| - callback_.Run(
|
| - "", base::StringPrintf("Process Crashed with code %08x.", exit_code));
|
| + utility_process_mojo_client_->Start(); // Start the utility process.
|
| +
|
| + 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) {
|
| - if (success) {
|
| - std::vector<uint8_t> ciphertext;
|
| - if (!networking_private_crypto::EncryptByteString(
|
| - public_key_, key_data, &ciphertext)) {
|
| - callback_.Run("", "Encrypt Credentials Failed");
|
| - return;
|
| - }
|
| -
|
| - std::string base64_encoded_key_data;
|
| - base::Base64Encode(
|
| - base::StringPiece(reinterpret_cast<const char*>(ciphertext.data()),
|
| - ciphertext.size()),
|
| - &base64_encoded_key_data);
|
| - callback_.Run(base64_encoded_key_data, "");
|
| - } else {
|
| - callback_.Run("", "Get Credentials Failed");
|
| +void CredentialsGetterHostClient::ReportResult(bool success,
|
| + const std::string& key_data) {
|
| + if (!success) {
|
| + callback_.Run(std::string(), "Get Credentials Failed");
|
| + return;
|
| }
|
| -}
|
|
|
| -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));
|
| -}
|
| + if (wifi_network_ ==
|
| + extensions::mojom::WiFiCredentialsGetter::kWiFiTestNetwork) {
|
| + DCHECK_EQ(wifi_network_, key_data);
|
| + callback_.Run(key_data, std::string());
|
| + return;
|
| + }
|
| +
|
| + std::vector<uint8_t> ciphertext;
|
| + if (!networking_private_crypto::EncryptByteString(public_key_, key_data,
|
| + &ciphertext)) {
|
| + callback_.Run(std::string(), "Encrypt Credentials Failed");
|
| + return;
|
| + }
|
|
|
| -CredentialsGetterHostClient::~CredentialsGetterHostClient() {
|
| + std::string base64_encoded_key_data;
|
| + base::Base64Encode(
|
| + base::StringPiece(reinterpret_cast<const char*>(ciphertext.data()),
|
| + ciphertext.size()),
|
| + &base64_encoded_key_data);
|
| + callback_.Run(base64_encoded_key_data, std::string());
|
| }
|
|
|
| } // namespace
|
| @@ -126,36 +136,34 @@ 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;
|
| + ~NetworkingPrivateCredentialsGetterWin() override = default;
|
|
|
| - DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin);
|
| -};
|
| -
|
| -NetworkingPrivateCredentialsGetterWin::NetworkingPrivateCredentialsGetterWin() {
|
| -}
|
| + static void GetCredentialsOnIOThread(const std::string& network_guid,
|
| + const std::string& public_key,
|
| + const CredentialsCallback& callback) {
|
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
| -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));
|
| -}
|
| + // CredentialsGetterHostClient is self deleting.
|
| + CredentialsGetterHostClient* client =
|
| + CredentialsGetterHostClient::Create(public_key);
|
| + client->GetWiFiCredentialsOnIOThread(network_guid, callback);
|
| + }
|
|
|
| -NetworkingPrivateCredentialsGetterWin::
|
| - ~NetworkingPrivateCredentialsGetterWin() {}
|
| + DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateCredentialsGetterWin);
|
| +};
|
|
|
| NetworkingPrivateCredentialsGetter*
|
| NetworkingPrivateCredentialsGetter::Create() {
|
|
|