1//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef TRUNKS_TRUNKS_FACTORY_IMPL_H_
18#define TRUNKS_TRUNKS_FACTORY_IMPL_H_
19
20#include "trunks/trunks_factory.h"
21
22#include <memory>
23#include <string>
24
25#include <base/macros.h>
26
27#include "trunks/command_transceiver.h"
28#include "trunks/trunks_export.h"
29
30namespace trunks {
31
32// TrunksFactoryImpl is the default TrunksFactory implementation. This class is
33// thread-safe with the exception of Initialize() but created objects are not
34// necessarily thread-safe. Example usage:
35//
36// TrunksFactoryImpl factory;
37// factory.Initialize(true /*failure_is_fatal*/);
38// Tpm* tpm = factory.GetTpm();
39class TRUNKS_EXPORT TrunksFactoryImpl : public TrunksFactory {
40 public:
41  // Uses an IPC proxy as the default CommandTransceiver.
42  TrunksFactoryImpl();
43  // TrunksFactoryImpl does not take ownership of |transceiver|. This
44  // transceiver is forwarded down to the Tpm instance maintained by
45  // this factory. It is assumed that the |transceiver| is already initialized.
46  explicit TrunksFactoryImpl(CommandTransceiver* transceiver);
47  ~TrunksFactoryImpl() override;
48
49  // Initialize the factory. This must be called before any other methods.
50  // Returns true on success.
51  bool Initialize();
52
53  // TrunksFactory methods.
54  Tpm* GetTpm() const override;
55  std::unique_ptr<TpmState> GetTpmState() const override;
56  std::unique_ptr<TpmUtility> GetTpmUtility() const override;
57  std::unique_ptr<AuthorizationDelegate> GetPasswordAuthorization(
58      const std::string& password) const override;
59  std::unique_ptr<SessionManager> GetSessionManager() const override;
60  std::unique_ptr<HmacSession> GetHmacSession() const override;
61  std::unique_ptr<PolicySession> GetPolicySession() const override;
62  std::unique_ptr<PolicySession> GetTrialSession() const override;
63  std::unique_ptr<BlobParser> GetBlobParser() const override;
64
65 private:
66  std::unique_ptr<CommandTransceiver> default_transceiver_;
67  CommandTransceiver* transceiver_;
68  std::unique_ptr<Tpm> tpm_;
69  bool initialized_ = false;
70
71  DISALLOW_COPY_AND_ASSIGN(TrunksFactoryImpl);
72};
73
74}  // namespace trunks
75
76#endif  // TRUNKS_TRUNKS_FACTORY_IMPL_H_
77