Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
shaochenguang | 3a1af3d2 | 2023-11-27 21:36:51 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_WEBAUTH_AUTHENTICATOR_ENVIRONMENT_H_ |
| 6 | #define CONTENT_BROWSER_WEBAUTH_AUTHENTICATOR_ENVIRONMENT_H_ |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 12 | #include "base/no_destructor.h" |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 13 | #include "content/browser/renderer_host/frame_tree_node.h" |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 14 | #include "content/common/content_export.h" |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 15 | |
| 16 | namespace device { |
| 17 | class FidoDiscoveryFactory; |
Martin Kreichgauer | 37ace49 | 2021-04-08 23:36:46 | [diff] [blame] | 18 | } // namespace device |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 19 | |
| 20 | namespace content { |
| 21 | |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 22 | class VirtualAuthenticatorManagerImpl; |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 23 | |
| 24 | // Allows enabling and disabling per-frame virtual environments for the Web |
| 25 | // Authentication API. Disabling the environment resets its state. |
| 26 | // |
| 27 | // This class is a singleton. |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 28 | class CONTENT_EXPORT AuthenticatorEnvironment : public FrameTreeNode::Observer { |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 29 | public: |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 30 | static AuthenticatorEnvironment* GetInstance(); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 31 | |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 32 | AuthenticatorEnvironment(const AuthenticatorEnvironment&) = delete; |
| 33 | AuthenticatorEnvironment& operator=(const AuthenticatorEnvironment&) = delete; |
| 34 | |
| 35 | // Disables all test environments. |
| 36 | void Reset(); |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 37 | |
Nina Satragno | f8ed79a | 2019-06-05 01:09:32 | [diff] [blame] | 38 | // Enables the scoped virtual authenticator environment for the |node| and its |
| 39 | // descendants. |
| 40 | // Does not have any effect if the |node| already has the virtual environment |
Nina Satragno | f2d56e1 | 2019-05-28 18:30:25 | [diff] [blame] | 41 | // enabled. |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 42 | void EnableVirtualAuthenticatorFor(FrameTreeNode* node, bool enable_ui); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 43 | |
Nina Satragno | f8ed79a | 2019-06-05 01:09:32 | [diff] [blame] | 44 | // Disables the scoped virtual authenticator environment for this |node|, |
| 45 | // resetting the state. If the environment is set on one of the |node|'s |
Nina Satragno | f2d56e1 | 2019-05-28 18:30:25 | [diff] [blame] | 46 | // parents instead, this won't have any effect. |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 47 | void DisableVirtualAuthenticatorFor(FrameTreeNode* node); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 48 | |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 49 | // Returns whether the virtual authenticator environment is enabled for |
| 50 | // |node|. |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 51 | bool IsVirtualAuthenticatorEnabledFor(FrameTreeNode* node); |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 52 | |
Nina Satragno | f8ed79a | 2019-06-05 01:09:32 | [diff] [blame] | 53 | // Returns the virtual fido discovery factory for the |node| if the virtual |
| 54 | // environment is enabled for it, otherwise returns nullptr. |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 55 | VirtualAuthenticatorManagerImpl* MaybeGetVirtualAuthenticatorManager( |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 56 | FrameTreeNode* node); |
Nina Satragno | f8ed79a | 2019-06-05 01:09:32 | [diff] [blame] | 57 | |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 58 | // Returns whether |node| has the virtual authenticator environment enabled |
| 59 | // with a user-verifying platform installed in that environment. |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 60 | bool HasVirtualUserVerifyingPlatformAuthenticator(FrameTreeNode* node); |
Martin Kreichgauer | 0b24720b | 2020-08-17 19:58:23 | [diff] [blame] | 61 | |
| 62 | // Returns the override installed by |
| 63 | // ReplaceDefaultDiscoveryFactoryForTesting(). |
| 64 | device::FidoDiscoveryFactory* MaybeGetDiscoveryFactoryTestOverride(); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 65 | |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 66 | void ReplaceDefaultDiscoveryFactoryForTesting( |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 67 | std::unique_ptr<device::FidoDiscoveryFactory> factory); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 68 | |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 69 | // FrameTreeNode::Observer: |
| 70 | void OnFrameTreeNodeDestroyed(FrameTreeNode* node) override; |
| 71 | |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 72 | protected: |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 73 | AuthenticatorEnvironment(); |
| 74 | ~AuthenticatorEnvironment() override; |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 75 | |
| 76 | private: |
Adam Langley | 1e03fb0 | 2023-03-16 23:02:03 | [diff] [blame] | 77 | friend class base::NoDestructor<AuthenticatorEnvironment>; |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 78 | |
Nina Satragno | f585eca | 2019-07-29 20:05:32 | [diff] [blame] | 79 | std::unique_ptr<device::FidoDiscoveryFactory> replaced_discovery_factory_; |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 80 | |
Nina Satragno | 1c4c1fe | 2022-08-31 21:31:22 | [diff] [blame] | 81 | std::map<FrameTreeNode*, std::unique_ptr<VirtualAuthenticatorManagerImpl>> |
| 82 | virtual_authenticator_managers_; |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace content |
| 86 | |
shaochenguang | 3a1af3d2 | 2023-11-27 21:36:51 | [diff] [blame] | 87 | #endif // CONTENT_BROWSER_WEBAUTH_AUTHENTICATOR_ENVIRONMENT_H_ |