summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/platform_keys/verify_trust_api.h
blob: 96a7aa5e03a83e140e39e5d2fa1655ee31a3f926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"

namespace content {
class BrowserContext;
}  // namespace content

namespace extensions {

namespace api {
namespace platform_keys {
namespace VerifyTLSServerCertificate {
struct Params;
}  // namespace VerifyTLSServerCertificate
}  // namespace platform_keys
}  // namespace api

// This keyed service is used by the platformKeys.verifyTLSServerCertificate for
// caching and to reuse objects between multiple API calls (e.g. the
// net::CertVerifier).
class VerifyTrustAPI : public BrowserContextKeyedAPI,
                       public ExtensionRegistryObserver {
 public:
  // Will be called with |return_value| set to the verification result (net::OK
  // if the certificate is trusted, otherwise a net error code) and
  // |cert_status| to the bitwise-OR of CertStatus flags. If an error occured
  // during processing the parameters, |error| is set to an english error
  // message and |return_value| and |cert_status| must be ignored.
  using VerifyCallback = base::Callback<
      void(const std::string& error, int return_value, int cert_status)>;
  using Params = api::platform_keys::VerifyTLSServerCertificate::Params;

  // Consumers should use the factory instead of this constructor.
  explicit VerifyTrustAPI(content::BrowserContext* context);
  ~VerifyTrustAPI() override;

  // Verifies the server certificate as described by |params| for the
  // extension with id |extension_id|. When verification is complete
  // (successful or not), the result will be passed to |callback|.
  //
  // Note: It is safe to delete this object while there are still
  // outstanding operations. However, if this happens, |callback|
  // will NOT be called.
  void Verify(scoped_ptr<Params> params,
              const std::string& extension_id,
              const VerifyCallback& callback);

  // ExtensionRegistryObserver:
  void OnExtensionUnloaded(content::BrowserContext* browser_context,
                           const Extension* extension,
                           UnloadedExtensionInfo::Reason reason) override;

  // BrowserContextKeyedAPI:
  static BrowserContextKeyedAPIFactory<VerifyTrustAPI>* GetFactoryInstance();

 protected:
  static const bool kServiceRedirectedInIncognito = true;
  static const bool kServiceIsCreatedWithBrowserContext = false;
  static const bool kServiceIsNULLWhileTesting = true;

 private:
  class IOPart;
  friend class BrowserContextKeyedAPIFactory<VerifyTrustAPI>;

  // Calls |ui_callback| with the given parameters.
  void FinishedVerificationOnUI(const VerifyCallback& ui_callback,
                                const std::string& error,
                                int return_value,
                                int cert_status);

  // Calls |ui_callback| on the UIThread with the given arguments.
  static void CallBackOnUI(const VerifyCallback& ui_callback,
                           const std::string& error,
                           int return_value,
                           int cert_status);

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() { return "VerifyTrustAPI"; }

  // Created on the UIThread but must be used and destroyed only on the
  // IOThread.
  scoped_ptr<IOPart, content::BrowserThread::DeleteOnIOThread> io_part_;

  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
      registry_observer_;

  base::WeakPtrFactory<VerifyTrustAPI> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(VerifyTrustAPI);
};

template <>
void BrowserContextKeyedAPIFactory<
    VerifyTrustAPI>::DeclareFactoryDependencies();

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_