Chromium Code Reviews| Index: chrome/browser/ui/webui/chromeos/cryptohome_ui.cc |
| diff --git a/chrome/browser/ui/webui/chromeos/cryptohome_ui.cc b/chrome/browser/ui/webui/chromeos/cryptohome_ui.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0ff28afc43068833ac59e487003d1941d1c0a153 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/chromeos/cryptohome_ui.cc |
| @@ -0,0 +1,139 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/chromeos/cryptohome_ui.h" |
| + |
| +#include "base/bind.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "chrome/browser/chromeos/cros/cryptohome_library.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chromeos/dbus/cryptohome_client.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "crypto/nss_util.h" |
| +#include "grit/browser_resources.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +// Returns HTML data source for chrome://cryptohome. |
| +ChromeWebUIDataSource* CreateCryptohomeUIHTMLSource() { |
| + ChromeWebUIDataSource* source = |
| + new ChromeWebUIDataSource(chrome::kChromeUICryptohomeHost); |
| + source->add_resource_path("cryptohome.js", IDR_CRYPTOHOME_JS); |
| + source->set_default_resource(IDR_CRYPTOHOME_HTML); |
| + return source; |
| +} |
| + |
| +// Class to handle messages from chrome://cryptohome. |
| +class CryptohomeWebUIHandler : public content::WebUIMessageHandler { |
| + public: |
| + CryptohomeWebUIHandler() : weak_ptr_factory_(this) {} |
|
Evan Stade
2012/07/19 01:53:16
this gets its own file please
hashimoto
2012/07/19 07:14:54
Done.
|
| + |
| + virtual ~CryptohomeWebUIHandler() {} |
| + |
| + // WebUIMessageHandler override. |
| + virtual void RegisterMessages() OVERRIDE { |
| + web_ui()->RegisterMessageCallback( |
| + "requestCryptohomeProperty", |
| + base::Bind(&CryptohomeWebUIHandler::OnRequestCryptohomeProperty, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + |
| + private: |
| + // This method is called from JavaScript. |
| + void OnRequestCryptohomeProperty(const base::ListValue* args) { |
| + std::string destination_id; |
| + if (!args->GetString(0, &destination_id)) { |
| + DLOG(ERROR) << "Invalid arguments."; |
| + return; |
| + } |
| + CryptohomeClient* cryptohome_client = |
| + DBusThreadManager::Get()->GetCryptohomeClient(); |
| + CryptohomeLibrary* cryptohome_library = |
| + CrosLibrary::Get()->GetCryptohomeLibrary(); |
| + if (destination_id == "is-mounted") { |
| + SetCryptohomeBoolProperty(destination_id, |
|
Evan Stade
2012/07/19 01:53:16
all this should just be stuffed into loadTimeData.
hashimoto
2012/07/19 07:14:54
The problem with setting values to loadTimeData is
|
| + cryptohome_library->IsMounted()); |
| + } else if (destination_id == "tpm-is-ready") { |
| + cryptohome_client->TpmIsReady(GetCryptohomeBoolCallback(destination_id)); |
| + } else if (destination_id == "tpm-is-enabled") { |
| + SetCryptohomeBoolProperty(destination_id, |
| + cryptohome_library->TpmIsEnabled()); |
| + } else if (destination_id == "tpm-is-owned") { |
| + SetCryptohomeBoolProperty(destination_id, |
| + cryptohome_library->TpmIsOwned()); |
| + } else if (destination_id == "tpm-is-being-owned") { |
| + SetCryptohomeBoolProperty(destination_id, |
| + cryptohome_library->TpmIsBeingOwned()); |
| + } else if (destination_id == "pkcs11-is-tpm-token-ready") { |
| + cryptohome_client->Pkcs11IsTpmTokenReady( |
| + GetCryptohomeBoolCallback(destination_id)); |
| + } else if (destination_id == "is-tpm-token-ready") { |
| + SetCryptohomeBoolProperty(destination_id, crypto::IsTPMTokenReady()); |
| + } else if (destination_id == "token-name") { |
| + std::string token_name; |
| + if (crypto::IsTPMTokenReady()) |
| + crypto::GetTPMTokenInfo(&token_name, NULL); |
| + SetCryptohomeStringProperty(destination_id, token_name); |
| + } else if (destination_id == "user-pin") { |
| + std::string user_pin; |
| + if (crypto::IsTPMTokenReady()) |
| + crypto::GetTPMTokenInfo(NULL, &user_pin); |
| + SetCryptohomeStringProperty(destination_id, user_pin); |
| + } else { |
| + NOTREACHED(); |
| + } |
| + } |
| + |
| + // Returns a callback to handle Cryptohome property values. |
| + CryptohomeClient::BoolMethodCallback GetCryptohomeBoolCallback( |
| + const std::string& destination_id) { |
| + return base::Bind(&CryptohomeWebUIHandler::OnCryptohomeBoolProperty, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + destination_id); |
| + } |
| + |
| + // This method is called when Cryptohome D-Bus method call completes. |
| + void OnCryptohomeBoolProperty(const std::string& destination_id, |
| + DBusMethodCallStatus call_status, |
| + bool value) { |
| + if (call_status != DBUS_METHOD_CALL_SUCCESS) |
| + value = false; |
| + SetCryptohomeBoolProperty(destination_id, value); |
| + } |
| + |
| + // Sets textcontent of the element whose id is |destination_id| to true/false. |
| + void SetCryptohomeBoolProperty(const std::string& destination_id, |
| + bool value) { |
| + SetCryptohomeStringProperty(destination_id, |
| + value ? "true" : "false"); |
| + } |
| + |
| + // Sets textcontent of the element whose id is |destination_id| to |value|. |
| + void SetCryptohomeStringProperty(const std::string& destination_id, |
| + const std::string& value) { |
| + web_ui()->CallJavascriptFunction("SetCryptohomeProperty", |
| + base::StringValue(destination_id), |
| + base::StringValue(value)); |
| + } |
| + |
| + base::WeakPtrFactory<CryptohomeWebUIHandler> weak_ptr_factory_; |
| + DISALLOW_COPY_AND_ASSIGN(CryptohomeWebUIHandler); |
| +}; |
| + |
| +} // namespace |
| + |
| +CryptohomeUI::CryptohomeUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| + web_ui->AddMessageHandler(new CryptohomeWebUIHandler()); |
| + |
| + Profile* profile = Profile::FromWebUI(web_ui); |
| + ChromeURLDataManager::AddDataSource(profile, CreateCryptohomeUIHTMLSource()); |
| +} |
| + |
| +} // namespace chromeos |