Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_ui.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 9 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | |
| 12 #include "chrome/common/url_constants.h" | |
| 13 #include "chromeos/dbus/cryptohome_client.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 #include "content/public/browser/web_ui.h" | |
| 16 #include "content/public/browser/web_ui_message_handler.h" | |
| 17 #include "crypto/nss_util.h" | |
| 18 #include "grit/browser_resources.h" | |
| 19 | |
| 20 namespace chromeos { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Returns HTML data source for chrome://cryptohome. | |
| 25 ChromeWebUIDataSource* CreateCryptohomeUIHTMLSource() { | |
| 26 ChromeWebUIDataSource* source = | |
| 27 new ChromeWebUIDataSource(chrome::kChromeUICryptohomeHost); | |
| 28 source->add_resource_path("cryptohome.js", IDR_CRYPTOHOME_JS); | |
| 29 source->set_default_resource(IDR_CRYPTOHOME_HTML); | |
| 30 return source; | |
| 31 } | |
| 32 | |
| 33 // Class to handle messages from chrome://cryptohome. | |
| 34 class CryptohomeWebUIHandler : public content::WebUIMessageHandler { | |
| 35 public: | |
| 36 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.
| |
| 37 | |
| 38 virtual ~CryptohomeWebUIHandler() {} | |
| 39 | |
| 40 // WebUIMessageHandler override. | |
| 41 virtual void RegisterMessages() OVERRIDE { | |
| 42 web_ui()->RegisterMessageCallback( | |
| 43 "requestCryptohomeProperty", | |
| 44 base::Bind(&CryptohomeWebUIHandler::OnRequestCryptohomeProperty, | |
| 45 weak_ptr_factory_.GetWeakPtr())); | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 // This method is called from JavaScript. | |
| 50 void OnRequestCryptohomeProperty(const base::ListValue* args) { | |
| 51 std::string destination_id; | |
| 52 if (!args->GetString(0, &destination_id)) { | |
| 53 DLOG(ERROR) << "Invalid arguments."; | |
| 54 return; | |
| 55 } | |
| 56 CryptohomeClient* cryptohome_client = | |
| 57 DBusThreadManager::Get()->GetCryptohomeClient(); | |
| 58 CryptohomeLibrary* cryptohome_library = | |
| 59 CrosLibrary::Get()->GetCryptohomeLibrary(); | |
| 60 if (destination_id == "is-mounted") { | |
| 61 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
| |
| 62 cryptohome_library->IsMounted()); | |
| 63 } else if (destination_id == "tpm-is-ready") { | |
| 64 cryptohome_client->TpmIsReady(GetCryptohomeBoolCallback(destination_id)); | |
| 65 } else if (destination_id == "tpm-is-enabled") { | |
| 66 SetCryptohomeBoolProperty(destination_id, | |
| 67 cryptohome_library->TpmIsEnabled()); | |
| 68 } else if (destination_id == "tpm-is-owned") { | |
| 69 SetCryptohomeBoolProperty(destination_id, | |
| 70 cryptohome_library->TpmIsOwned()); | |
| 71 } else if (destination_id == "tpm-is-being-owned") { | |
| 72 SetCryptohomeBoolProperty(destination_id, | |
| 73 cryptohome_library->TpmIsBeingOwned()); | |
| 74 } else if (destination_id == "pkcs11-is-tpm-token-ready") { | |
| 75 cryptohome_client->Pkcs11IsTpmTokenReady( | |
| 76 GetCryptohomeBoolCallback(destination_id)); | |
| 77 } else if (destination_id == "is-tpm-token-ready") { | |
| 78 SetCryptohomeBoolProperty(destination_id, crypto::IsTPMTokenReady()); | |
| 79 } else if (destination_id == "token-name") { | |
| 80 std::string token_name; | |
| 81 if (crypto::IsTPMTokenReady()) | |
| 82 crypto::GetTPMTokenInfo(&token_name, NULL); | |
| 83 SetCryptohomeStringProperty(destination_id, token_name); | |
| 84 } else if (destination_id == "user-pin") { | |
| 85 std::string user_pin; | |
| 86 if (crypto::IsTPMTokenReady()) | |
| 87 crypto::GetTPMTokenInfo(NULL, &user_pin); | |
| 88 SetCryptohomeStringProperty(destination_id, user_pin); | |
| 89 } else { | |
| 90 NOTREACHED(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 // Returns a callback to handle Cryptohome property values. | |
| 95 CryptohomeClient::BoolMethodCallback GetCryptohomeBoolCallback( | |
| 96 const std::string& destination_id) { | |
| 97 return base::Bind(&CryptohomeWebUIHandler::OnCryptohomeBoolProperty, | |
| 98 weak_ptr_factory_.GetWeakPtr(), | |
| 99 destination_id); | |
| 100 } | |
| 101 | |
| 102 // This method is called when Cryptohome D-Bus method call completes. | |
| 103 void OnCryptohomeBoolProperty(const std::string& destination_id, | |
| 104 DBusMethodCallStatus call_status, | |
| 105 bool value) { | |
| 106 if (call_status != DBUS_METHOD_CALL_SUCCESS) | |
| 107 value = false; | |
| 108 SetCryptohomeBoolProperty(destination_id, value); | |
| 109 } | |
| 110 | |
| 111 // Sets textcontent of the element whose id is |destination_id| to true/false. | |
| 112 void SetCryptohomeBoolProperty(const std::string& destination_id, | |
| 113 bool value) { | |
| 114 SetCryptohomeStringProperty(destination_id, | |
| 115 value ? "true" : "false"); | |
| 116 } | |
| 117 | |
| 118 // Sets textcontent of the element whose id is |destination_id| to |value|. | |
| 119 void SetCryptohomeStringProperty(const std::string& destination_id, | |
| 120 const std::string& value) { | |
| 121 web_ui()->CallJavascriptFunction("SetCryptohomeProperty", | |
| 122 base::StringValue(destination_id), | |
| 123 base::StringValue(value)); | |
| 124 } | |
| 125 | |
| 126 base::WeakPtrFactory<CryptohomeWebUIHandler> weak_ptr_factory_; | |
| 127 DISALLOW_COPY_AND_ASSIGN(CryptohomeWebUIHandler); | |
| 128 }; | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 CryptohomeUI::CryptohomeUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 133 web_ui->AddMessageHandler(new CryptohomeWebUIHandler()); | |
| 134 | |
| 135 Profile* profile = Profile::FromWebUI(web_ui); | |
| 136 ChromeURLDataManager::AddDataSource(profile, CreateCryptohomeUIHTMLSource()); | |
| 137 } | |
| 138 | |
| 139 } // namespace chromeos | |
| OLD | NEW |