login_library.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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/cros/login_library.h" 6 7#include "base/message_loop.h" 8#include "chrome/browser/chrome_thread.h" 9#include "chrome/browser/chromeos/cros/cros_library.h" 10 11namespace chromeos { 12 13class LoginLibraryImpl : public LoginLibrary { 14 public: 15 LoginLibraryImpl() 16 : set_owner_key_callback_(NULL), 17 whitelist_op_callback_(NULL), 18 property_op_callback_(NULL) { 19 if (CrosLibrary::Get()->EnsureLoaded()) 20 Init(); 21 } 22 virtual ~LoginLibraryImpl() { 23 if (session_connection_) { 24 chromeos::DisconnectSession(session_connection_); 25 } 26 } 27 28 bool EmitLoginPromptReady() { 29 return chromeos::EmitLoginPromptReady(); 30 } 31 32 bool CheckWhitelist(const std::string& email, 33 std::vector<uint8>* OUT_signature) { 34 return chromeos::CheckWhitelist(email.c_str(), OUT_signature); 35 } 36 37 bool RetrieveProperty(const std::string& name, 38 std::string* OUT_value, 39 std::vector<uint8>* OUT_signature) { 40 return chromeos::RetrieveProperty(name.c_str(), OUT_value, OUT_signature); 41 } 42 43 bool SetOwnerKeyAsync(const std::vector<uint8>& public_key_der, 44 Delegate* callback) { 45 DCHECK(callback) << "must provide a callback to SetOwnerKeyAsync()"; 46 if (set_owner_key_callback_) 47 return false; 48 set_owner_key_callback_ = callback; 49 return chromeos::SetOwnerKey(public_key_der); 50 } 51 52 bool StorePropertyAsync(const std::string& name, 53 const std::string& value, 54 const std::vector<uint8>& signature, 55 Delegate* callback) { 56 DCHECK(callback) << "must provide a callback to StorePropertyAsync()"; 57 if (property_op_callback_) 58 return false; 59 property_op_callback_ = callback; 60 return chromeos::StoreProperty(name.c_str(), value.c_str(), signature); 61 } 62 63 bool UnwhitelistAsync(const std::string& email, 64 const std::vector<uint8>& signature, 65 Delegate* callback) { 66 DCHECK(callback) << "must provide a callback to UnwhitelistAsync()"; 67 if (whitelist_op_callback_) 68 return false; 69 whitelist_op_callback_ = callback; 70 return chromeos::Unwhitelist(email.c_str(), signature); 71 } 72 73 bool WhitelistAsync(const std::string& email, 74 const std::vector<uint8>& signature, 75 Delegate* callback) { 76 DCHECK(callback) << "must provide a callback to WhitelistAsync()"; 77 if (whitelist_op_callback_) 78 return false; 79 whitelist_op_callback_ = callback; 80 return chromeos::Whitelist(email.c_str(), signature); 81 } 82 83 bool EnumerateWhitelisted(std::vector<std::string>* whitelisted) { 84 return chromeos::EnumerateWhitelisted(whitelisted); 85 } 86 87 bool StartSession(const std::string& user_email, 88 const std::string& unique_id /* unused */) { 89 // only pass unique_id through once we use it for something. 90 return chromeos::StartSession(user_email.c_str(), ""); 91 } 92 93 bool StopSession(const std::string& unique_id /* unused */) { 94 // only pass unique_id through once we use it for something. 95 return chromeos::StopSession(""); 96 } 97 98 bool RestartJob(int pid, const std::string& command_line) { 99 return chromeos::RestartJob(pid, command_line.c_str()); 100 } 101 102 private: 103 static void Handler(void* object, const OwnershipEvent& event) { 104 LoginLibraryImpl* self = static_cast<LoginLibraryImpl*>(object); 105 switch (event) { 106 case SetKeySuccess: 107 self->CompleteSetOwnerKey(true); 108 break; 109 case SetKeyFailure: 110 self->CompleteSetOwnerKey(false); 111 break; 112 case WhitelistOpSuccess: 113 self->CompleteWhitelistOp(true); 114 break; 115 case WhitelistOpFailure: 116 self->CompleteWhitelistOp(false); 117 break; 118 case PropertyOpSuccess: 119 self->CompletePropertyOp(true); 120 break; 121 case PropertyOpFailure: 122 self->CompletePropertyOp(false); 123 break; 124 default: 125 NOTREACHED(); 126 break; 127 } 128 } 129 130 void Init() { 131 session_connection_ = chromeos::MonitorSession(&Handler, this); 132 } 133 134 void CompleteSetOwnerKey(bool result) { 135 CHECK(set_owner_key_callback_) << "CompleteSetOwnerKey() called without " 136 "a registered callback!"; 137 set_owner_key_callback_->OnComplete(result); 138 set_owner_key_callback_ = NULL; 139 } 140 141 void CompleteWhitelistOp(bool result) { 142 CHECK(whitelist_op_callback_); 143 whitelist_op_callback_->OnComplete(result); 144 whitelist_op_callback_ = NULL; 145 } 146 147 void CompletePropertyOp(bool result) { 148 CHECK(property_op_callback_); 149 property_op_callback_->OnComplete(result); 150 property_op_callback_ = NULL; 151 } 152 153 chromeos::SessionConnection session_connection_; 154 155 Delegate* set_owner_key_callback_; 156 Delegate* whitelist_op_callback_; 157 Delegate* property_op_callback_; 158 159 DISALLOW_COPY_AND_ASSIGN(LoginLibraryImpl); 160}; 161 162class LoginLibraryStubImpl : public LoginLibrary { 163 public: 164 LoginLibraryStubImpl() {} 165 virtual ~LoginLibraryStubImpl() {} 166 167 bool EmitLoginPromptReady() { return true; } 168 bool CheckWhitelist(const std::string& email, 169 std::vector<uint8>* OUT_signature) { 170 OUT_signature->assign(2, 0); 171 return true; 172 } 173 bool RetrieveProperty(const std::string& name, 174 std::string* OUT_value, 175 std::vector<uint8>* OUT_signature) { 176 OUT_value->assign("stub"); 177 OUT_signature->assign(2, 0); 178 return true; 179 } 180 bool SetOwnerKeyAsync(const std::vector<uint8>& public_key_der, 181 Delegate* callback) { 182 ChromeThread::PostTask( 183 ChromeThread::UI, FROM_HERE, 184 NewRunnableFunction(&DoStubCallback, callback)); 185 return true; 186 } 187 bool StorePropertyAsync(const std::string& name, 188 const std::string& value, 189 const std::vector<uint8>& signature, 190 Delegate* callback) { 191 ChromeThread::PostTask( 192 ChromeThread::UI, FROM_HERE, 193 NewRunnableFunction(&DoStubCallback, callback)); 194 return true; 195 } 196 bool UnwhitelistAsync(const std::string& email, 197 const std::vector<uint8>& signature, 198 Delegate* callback) { 199 ChromeThread::PostTask( 200 ChromeThread::UI, FROM_HERE, 201 NewRunnableFunction(&DoStubCallback, callback)); 202 return true; 203 } 204 bool WhitelistAsync(const std::string& email, 205 const std::vector<uint8>& signature, 206 Delegate* callback) { 207 ChromeThread::PostTask( 208 ChromeThread::UI, FROM_HERE, 209 NewRunnableFunction(&DoStubCallback, callback)); 210 return true; 211 } 212 bool EnumerateWhitelisted(std::vector<std::string>* whitelisted) { 213 return true; 214 } 215 bool StartSession(const std::string& user_email, 216 const std::string& unique_id /* unused */) { return true; } 217 bool StopSession(const std::string& unique_id /* unused */) { return true; } 218 bool RestartJob(int pid, const std::string& command_line) { return true; } 219 220 private: 221 static void DoStubCallback(Delegate* callback) { 222 callback->OnComplete(true); 223 } 224 225 DISALLOW_COPY_AND_ASSIGN(LoginLibraryStubImpl); 226}; 227 228// static 229LoginLibrary* LoginLibrary::GetImpl(bool stub) { 230 if (stub) 231 return new LoginLibraryStubImpl(); 232 else 233 return new LoginLibraryImpl(); 234} 235 236} // namespace chromeos 237