/chrome/browser/extensions/api/platform_keys/verify_trust_api.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 114 lines · 65 code · 23 blank · 26 comment · 0 complexity · fc48de67aa83fdc986b2f308de32cbaf MD5 · raw file

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