1 // Copyright (c) 2012 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/ui/webui/chromeos/cryptohome_web_ui_handler.h" 6 7 #include "base/bind.h" 8 #include "base/values.h" 9 #include "chromeos/dbus/cryptohome_client.h" 10 #include "chromeos/dbus/dbus_thread_manager.h" 11 #include "content/public/browser/web_ui.h" 12 #include "crypto/nss_util.h" 13 14 namespace chromeos { 15 16 CryptohomeWebUIHandler::CryptohomeWebUIHandler() : weak_ptr_factory_(this) {} 17 18 CryptohomeWebUIHandler::~CryptohomeWebUIHandler() {} 19 20 void CryptohomeWebUIHandler::RegisterMessages() { 21 web_ui()->RegisterMessageCallback( 22 "pageLoaded", 23 base::Bind(&CryptohomeWebUIHandler::OnPageLoaded, 24 weak_ptr_factory_.GetWeakPtr())); 25 } 26 27 void CryptohomeWebUIHandler::OnPageLoaded(const base::ListValue* args) { 28 CryptohomeClient* cryptohome_client = 29 DBusThreadManager::Get()->GetCryptohomeClient(); 30 31 cryptohome_client->IsMounted(GetCryptohomeBoolCallback("is-mounted")); 32 cryptohome_client->TpmIsReady(GetCryptohomeBoolCallback("tpm-is-ready")); 33 cryptohome_client->TpmIsEnabled(GetCryptohomeBoolCallback("tpm-is-enabled")); 34 cryptohome_client->TpmIsOwned(GetCryptohomeBoolCallback("tpm-is-owned")); 35 cryptohome_client->TpmIsBeingOwned( 36 GetCryptohomeBoolCallback("tpm-is-being-owned")); 37 cryptohome_client->Pkcs11IsTpmTokenReady( 38 GetCryptohomeBoolCallback("pkcs11-is-tpm-token-ready")); 39 base::FundamentalValue is_tpm_token_ready(crypto::IsTPMTokenReady()); 40 SetCryptohomeProperty("is-tpm-token-ready", is_tpm_token_ready); 41 42 if (crypto::IsTPMTokenReady()) { 43 std::string token_name; 44 std::string user_pin; 45 crypto::GetTPMTokenInfo(&token_name, &user_pin); 46 // Hide user_pin. 47 user_pin = std::string(user_pin.length(), '*'); 48 base::StringValue token_name_value(token_name); 49 SetCryptohomeProperty("token-name", token_name_value); 50 base::StringValue user_pin_value(user_pin); 51 SetCryptohomeProperty("user-pin", user_pin_value); 52 } 53 } 54 55 BoolDBusMethodCallback CryptohomeWebUIHandler::GetCryptohomeBoolCallback( 56 const std::string& destination_id) { 57 return base::Bind(&CryptohomeWebUIHandler::OnCryptohomeBoolProperty, 58 weak_ptr_factory_.GetWeakPtr(), 59 destination_id); 60 } 61 62 void CryptohomeWebUIHandler::OnCryptohomeBoolProperty( 63 const std::string& destination_id, 64 DBusMethodCallStatus call_status, 65 bool value) { 66 if (call_status != DBUS_METHOD_CALL_SUCCESS) 67 value = false; 68 base::FundamentalValue fundamental_value(value); 69 SetCryptohomeProperty(destination_id, fundamental_value); 70 } 71 72 void CryptohomeWebUIHandler::SetCryptohomeProperty( 73 const std::string& destination_id, 74 const base::Value& value) { 75 base::StringValue destination_id_value(destination_id); 76 web_ui()->CallJavascriptFunction( 77 "SetCryptohomeProperty", destination_id_value, value); 78 } 79 80 } // namespace chromeos 81