Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / platform_keys / verify_trust_api.h
blob96a7aa5e03a83e140e39e5d2fa1655ee31a3f926
1 // Copyright 2015 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "extensions/browser/browser_context_keyed_api_factory.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_registry_observer.h"
20 namespace content {
21 class BrowserContext;
22 } // namespace content
24 namespace extensions {
26 namespace api {
27 namespace platform_keys {
28 namespace VerifyTLSServerCertificate {
29 struct Params;
30 } // namespace VerifyTLSServerCertificate
31 } // namespace platform_keys
32 } // namespace api
34 // This keyed service is used by the platformKeys.verifyTLSServerCertificate for
35 // caching and to reuse objects between multiple API calls (e.g. the
36 // net::CertVerifier).
37 class VerifyTrustAPI : public BrowserContextKeyedAPI,
38 public ExtensionRegistryObserver {
39 public:
40 // Will be called with |return_value| set to the verification result (net::OK
41 // if the certificate is trusted, otherwise a net error code) and
42 // |cert_status| to the bitwise-OR of CertStatus flags. If an error occured
43 // during processing the parameters, |error| is set to an english error
44 // message and |return_value| and |cert_status| must be ignored.
45 using VerifyCallback = base::Callback<
46 void(const std::string& error, int return_value, int cert_status)>;
47 using Params = api::platform_keys::VerifyTLSServerCertificate::Params;
49 // Consumers should use the factory instead of this constructor.
50 explicit VerifyTrustAPI(content::BrowserContext* context);
51 ~VerifyTrustAPI() override;
53 // Verifies the server certificate as described by |params| for the
54 // extension with id |extension_id|. When verification is complete
55 // (successful or not), the result will be passed to |callback|.
57 // Note: It is safe to delete this object while there are still
58 // outstanding operations. However, if this happens, |callback|
59 // will NOT be called.
60 void Verify(scoped_ptr<Params> params,
61 const std::string& extension_id,
62 const VerifyCallback& callback);
64 // ExtensionRegistryObserver:
65 void OnExtensionUnloaded(content::BrowserContext* browser_context,
66 const Extension* extension,
67 UnloadedExtensionInfo::Reason reason) override;
69 // BrowserContextKeyedAPI:
70 static BrowserContextKeyedAPIFactory<VerifyTrustAPI>* GetFactoryInstance();
72 protected:
73 static const bool kServiceRedirectedInIncognito = true;
74 static const bool kServiceIsCreatedWithBrowserContext = false;
75 static const bool kServiceIsNULLWhileTesting = true;
77 private:
78 class IOPart;
79 friend class BrowserContextKeyedAPIFactory<VerifyTrustAPI>;
81 // Calls |ui_callback| with the given parameters.
82 void FinishedVerificationOnUI(const VerifyCallback& ui_callback,
83 const std::string& error,
84 int return_value,
85 int cert_status);
87 // Calls |ui_callback| on the UIThread with the given arguments.
88 static void CallBackOnUI(const VerifyCallback& ui_callback,
89 const std::string& error,
90 int return_value,
91 int cert_status);
93 // BrowserContextKeyedAPI implementation.
94 static const char* service_name() { return "VerifyTrustAPI"; }
96 // Created on the UIThread but must be used and destroyed only on the
97 // IOThread.
98 scoped_ptr<IOPart, content::BrowserThread::DeleteOnIOThread> io_part_;
100 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
101 registry_observer_;
103 base::WeakPtrFactory<VerifyTrustAPI> weak_factory_;
105 DISALLOW_COPY_AND_ASSIGN(VerifyTrustAPI);
108 template <>
109 void BrowserContextKeyedAPIFactory<
110 VerifyTrustAPI>::DeclareFactoryDependencies();
112 } // namespace extensions
114 #endif // CHROME_BROWSER_EXTENSIONS_API_PLATFORM_KEYS_VERIFY_TRUST_API_H_