Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / easy_unlock / easy_unlock_remove_keys_operation.cc
1 // Copyright 2014 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/chromeos/login/easy_unlock/easy_unlock_remove_keys_operation.h"
6
7 #include "base/bind.h"
8 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
9 #include "chromeos/cryptohome/homedir_methods.h"
10 #include "google_apis/gaia/gaia_auth_util.h"
11
12 namespace chromeos {
13
14 EasyUnlockRemoveKeysOperation::EasyUnlockRemoveKeysOperation(
15     const UserContext& user_context,
16     size_t start_index,
17     const RemoveKeysCallback& callback)
18     : user_context_(user_context),
19       callback_(callback),
20       key_index_(start_index),
21       weak_ptr_factory_(this) {
22   // Must have the secret and callback.
23   DCHECK(!user_context_.GetKey()->GetSecret().empty());
24   DCHECK(!callback_.is_null());
25 }
26
27 EasyUnlockRemoveKeysOperation::~EasyUnlockRemoveKeysOperation() {
28 }
29
30 void EasyUnlockRemoveKeysOperation::Start() {
31   // TODO(xiyuan): Use ListKeyEx and delete by label instead of by index.
32   RemoveKey();
33 }
34
35 void EasyUnlockRemoveKeysOperation::RemoveKey() {
36   std::string canonicalized =
37       gaia::CanonicalizeEmail(user_context_.GetUserID());
38   cryptohome::Identification id(canonicalized);
39   const Key* const auth_key = user_context_.GetKey();
40   cryptohome::Authorization auth(auth_key->GetSecret(), auth_key->GetLabel());
41
42   cryptohome::HomedirMethods::GetInstance()->RemoveKeyEx(
43       id,
44       auth,
45       EasyUnlockKeyManager::GetKeyLabel(key_index_),
46       base::Bind(&EasyUnlockRemoveKeysOperation::OnKeyRemoved,
47                  weak_ptr_factory_.GetWeakPtr()));
48 }
49
50 void EasyUnlockRemoveKeysOperation::OnKeyRemoved(
51     bool success,
52     cryptohome::MountError return_code) {
53   if (success) {
54     ++key_index_;
55     RemoveKey();
56     return;
57   }
58
59   // MOUNT_ERROR_KEY_FAILURE is considered as success. Other error codes are
60   // treated as failures.
61   if (return_code == cryptohome::MOUNT_ERROR_KEY_FAILURE) {
62     callback_.Run(true);
63   } else {
64     LOG(ERROR) << "Easy unlock remove keys operation failed, code="
65                << return_code;
66     callback_.Run(false);
67   }
68 }
69
70 }  // namespace chromeos