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/browser_thread.h"
12#include "content/public/browser/web_ui.h"
13#include "crypto/nss_util.h"
14
15using content::BrowserThread;
16
17namespace chromeos {
18
19CryptohomeWebUIHandler::CryptohomeWebUIHandler() : weak_ptr_factory_(this) {}
20
21CryptohomeWebUIHandler::~CryptohomeWebUIHandler() {}
22
23void CryptohomeWebUIHandler::RegisterMessages() {
24  web_ui()->RegisterMessageCallback(
25      "pageLoaded",
26      base::Bind(&CryptohomeWebUIHandler::OnPageLoaded,
27                 weak_ptr_factory_.GetWeakPtr()));
28}
29
30void CryptohomeWebUIHandler::OnPageLoaded(const base::ListValue* args) {
31  CryptohomeClient* cryptohome_client =
32      DBusThreadManager::Get()->GetCryptohomeClient();
33
34  cryptohome_client->IsMounted(GetCryptohomeBoolCallback("is-mounted"));
35  cryptohome_client->TpmIsReady(GetCryptohomeBoolCallback("tpm-is-ready"));
36  cryptohome_client->TpmIsEnabled(GetCryptohomeBoolCallback("tpm-is-enabled"));
37  cryptohome_client->TpmIsOwned(GetCryptohomeBoolCallback("tpm-is-owned"));
38  cryptohome_client->TpmIsBeingOwned(
39      GetCryptohomeBoolCallback("tpm-is-being-owned"));
40  cryptohome_client->Pkcs11IsTpmTokenReady(
41      GetCryptohomeBoolCallback("pkcs11-is-tpm-token-ready"));
42
43  BrowserThread::PostTaskAndReplyWithResult(
44      BrowserThread::IO,
45      FROM_HERE,
46      base::Bind(&crypto::IsTPMTokenReady, base::Closure()),
47      base::Bind(&CryptohomeWebUIHandler::DidGetNSSUtilInfoOnUIThread,
48                 weak_ptr_factory_.GetWeakPtr()));
49}
50
51void CryptohomeWebUIHandler::DidGetNSSUtilInfoOnUIThread(
52    bool is_tpm_token_ready) {
53  DCHECK_CURRENTLY_ON(BrowserThread::UI);
54
55  base::FundamentalValue is_tpm_token_ready_value(is_tpm_token_ready);
56  SetCryptohomeProperty("is-tpm-token-ready", is_tpm_token_ready_value);
57}
58
59BoolDBusMethodCallback CryptohomeWebUIHandler::GetCryptohomeBoolCallback(
60    const std::string& destination_id) {
61  return base::Bind(&CryptohomeWebUIHandler::OnCryptohomeBoolProperty,
62                    weak_ptr_factory_.GetWeakPtr(),
63                    destination_id);
64}
65
66void CryptohomeWebUIHandler::OnCryptohomeBoolProperty(
67    const std::string& destination_id,
68    DBusMethodCallStatus call_status,
69    bool value) {
70  if (call_status != DBUS_METHOD_CALL_SUCCESS)
71    value = false;
72  base::FundamentalValue fundamental_value(value);
73  SetCryptohomeProperty(destination_id, fundamental_value);
74}
75
76void CryptohomeWebUIHandler::SetCryptohomeProperty(
77    const std::string& destination_id,
78    const base::Value& value) {
79  base::StringValue destination_id_value(destination_id);
80  web_ui()->CallJavascriptFunction(
81      "SetCryptohomeProperty", destination_id_value, value);
82}
83
84}  // namespace chromeos
85