ownership_service.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OWNERSHIP_SERVICE_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_OWNERSHIP_SERVICE_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "chrome/browser/browser_thread.h"
13#include "chrome/browser/chromeos/login/owner_key_utils.h"
14#include "chrome/browser/chromeos/login/owner_manager.h"
15
16namespace base {
17template <typename T> struct DefaultLazyInstanceTraits;
18}
19
20namespace chromeos {
21
22class OwnershipService {
23 public:
24  // Returns the singleton instance of the OwnershipService.
25  static OwnershipService* GetSharedInstance();
26  virtual ~OwnershipService();
27
28  // If the device has been owned already, posts a task to the FILE thread to
29  // fetch the public key off disk.
30  //
31  // Sends out a OWNER_KEY_FETCH_ATTEMPT_SUCCESS notification on success,
32  // OWNER_KEY_FETCH_ATTEMPT_FAILED on failure.
33  virtual void StartLoadOwnerKeyAttempt();
34
35  // If the device has not yet been owned, posts a task to the FILE
36  // thread to generate the owner's keys and put them in the right
37  // places.  Keeps them in memory as well, for later use.
38  //
39  // Upon failure, sends out OWNER_KEY_FETCH_ATTEMPT_FAILED.
40  // Upon success, sends out OWNER_KEY_FETCH_ATTEMPT_SUCCESS.
41  // If no attempt is started (if the device is already owned), no
42  // notification is sent.
43  virtual void StartTakeOwnershipAttempt(const std::string& unused);
44
45  // Initiate an attempt to sign |data| with |private_key_|.  Will call
46  // d->OnKeyOpComplete() when done.  Upon success, the signature will be passed
47  // as the |payload| argument to d->OnKeyOpComplete().
48  //
49  // If you call this on a well-known thread, you'll be called back on that
50  // thread.  Otherwise, you'll get called back on the UI thread.
51  virtual void StartSigningAttempt(const std::string& data,
52                                   OwnerManager::Delegate* d);
53
54  // Initiate an attempt to verify that |signature| is valid over |data| with
55  // |public_key_|.  When the attempt is completed, an appropriate KeyOpCode
56  // will be passed to d->OnKeyOpComplete().
57  //
58  // If you call this on a well-known thread, you'll be called back on that
59  // thread.  Otherwise, you'll get called back on the UI thread.
60  virtual void StartVerifyAttempt(const std::string& data,
61                                  const std::vector<uint8>& signature,
62                                  OwnerManager::Delegate* d);
63
64  // This method must be run on the FILE thread.
65  virtual bool CurrentUserIsOwner();
66
67  // This method must be run on the FILE thread.
68  // Note: not static, for better mocking.
69  virtual bool IsAlreadyOwned();
70
71 protected:
72  OwnershipService();
73
74 private:
75  friend struct base::DefaultLazyInstanceTraits<OwnershipService>;
76  friend class OwnershipServiceTest;
77
78  static void TryLoadOwnerKeyAttempt(OwnershipService* service);
79  static void TryTakeOwnershipAttempt(OwnershipService* service);
80  static void TrySigningAttempt(OwnershipService* service,
81                                const BrowserThread::ID thread_id,
82                                const std::string& data,
83                                OwnerManager::Delegate* d);
84  static void TryVerifyAttempt(OwnershipService* service,
85                               const BrowserThread::ID thread_id,
86                               const std::string& data,
87                               const std::vector<uint8>& signature,
88                               OwnerManager::Delegate* d);
89  static void FailAttempt(OwnerManager::Delegate* d);
90
91  OwnerManager* manager() { return manager_.get(); }
92
93  scoped_refptr<OwnerManager> manager_;
94  scoped_refptr<OwnerKeyUtils> utils_;
95};
96
97}  // namespace chromeos
98
99#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_OWNERSHIP_SERVICE_H_
100