Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [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 | |
| 5 | #ifndef CONTENT_BROWSER_MOJO_BINDER_POLICY_MAP_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_MOJO_BINDER_POLICY_MAP_IMPL_H_ |
| 7 | |
| 8 | #include <string> |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 9 | #include <string_view> |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 10 | |
| 11 | #include "base/containers/flat_map.h" |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
| 13 | #include "content/public/browser/mojo_binder_policy_map.h" |
| 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | // Implements MojoBinderPolicyMap and owns a policy map. |
| 18 | class CONTENT_EXPORT MojoBinderPolicyMapImpl : public MojoBinderPolicyMap { |
| 19 | public: |
| 20 | MojoBinderPolicyMapImpl(); |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 21 | |
| 22 | // This constructor is for testing. |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 23 | explicit MojoBinderPolicyMapImpl( |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 24 | const base::flat_map<std::string, MojoBinderNonAssociatedPolicy>& |
| 25 | init_map); |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 26 | ~MojoBinderPolicyMapImpl() override; |
| 27 | |
| 28 | // Disallows copy and move operations. |
| 29 | MojoBinderPolicyMapImpl(const MojoBinderPolicyMapImpl& other) = delete; |
| 30 | MojoBinderPolicyMapImpl& operator=(const MojoBinderPolicyMapImpl& other) = |
| 31 | delete; |
| 32 | MojoBinderPolicyMapImpl(MojoBinderPolicyMapImpl&&) = delete; |
| 33 | MojoBinderPolicyMapImpl& operator=(MojoBinderPolicyMapImpl&&) = delete; |
| 34 | |
Lingqi Chi | cd5239e | 2021-03-03 09:15:33 | [diff] [blame] | 35 | // Returns the instance used by MojoBinderPolicyApplier for prerendering |
| 36 | // pages. |
| 37 | // This is used when the prerendered page and the page that triggered the |
| 38 | // prerendering are same origin. Currently this is the only use of this class. |
| 39 | static const MojoBinderPolicyMapImpl* GetInstanceForSameOriginPrerendering(); |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 40 | |
Takashi Toyoshima | a35e5fc | 2023-10-20 04:00:34 | [diff] [blame] | 41 | // Returns the instance used by MojoBinderPolicyApplier for preview mode. This |
| 42 | // is used when a page is shown in preview mode. |
| 43 | static const MojoBinderPolicyMapImpl* GetInstanceForPreview(); |
| 44 | |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 45 | // Gets the corresponding policy of a given Mojo interface name. |
| 46 | // If the interface name is not in `non_associated_policy_map_`, the given |
| 47 | // `default_policy` will be returned. |
| 48 | // Callers should ensure that the corresponding interface is used as a |
| 49 | // non-associated interface in the context. If the interface is used as a |
| 50 | // channel-associated interface, they should call |
| 51 | // `GetAssociatedMojoBinderPolicy`. |
| 52 | MojoBinderNonAssociatedPolicy GetNonAssociatedMojoBinderPolicy( |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 53 | const std::string& interface_name, |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 54 | const MojoBinderNonAssociatedPolicy default_policy) const; |
| 55 | |
| 56 | // Gets the corresponding policy of a given Mojo interface name. |
| 57 | // If the interface name is not in `associated_policy_map_`, the given |
| 58 | // `default_policy` will be returned. |
| 59 | // Callers should ensure that the corresponding interface is used as a |
| 60 | // channel-associated interface in the context. If the interface is used as a |
| 61 | // non-associated interface, they should call |
| 62 | // `GetNonAssociatedMojoBinderPolicy`. |
| 63 | MojoBinderAssociatedPolicy GetAssociatedMojoBinderPolicy( |
| 64 | const std::string& interface_name, |
| 65 | const MojoBinderAssociatedPolicy default_policy) const; |
| 66 | |
| 67 | // Fails with DCHECK if the interface is not in `non_associated_policy_map_`. |
| 68 | MojoBinderNonAssociatedPolicy GetNonAssociatedMojoBinderPolicyOrDieForTesting( |
| 69 | const std::string& interface_name) const; |
| 70 | |
| 71 | // Fails with DCHECK if the interface is not in `associated_policy_map_`. |
| 72 | MojoBinderAssociatedPolicy GetAssociatedMojoBinderPolicyOrDieForTesting( |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 73 | const std::string& interface_name) const; |
| 74 | |
| 75 | private: |
| 76 | // MojoBinderPolicyMap implementation: |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 77 | void SetPolicyByName(const std::string_view& name, |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 78 | MojoBinderAssociatedPolicy policy) override; |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 79 | |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 80 | void SetPolicyByName(const std::string_view& name, |
Lingqi Chi | 2e03d9d6 | 2021-11-08 05:45:41 | [diff] [blame] | 81 | MojoBinderNonAssociatedPolicy policy) override; |
| 82 | |
| 83 | base::flat_map<std::string, MojoBinderNonAssociatedPolicy> |
| 84 | non_associated_policy_map_; |
| 85 | base::flat_map<std::string, MojoBinderAssociatedPolicy> |
| 86 | associated_policy_map_; |
Lingqi Chi | 775e6075 | 2020-12-14 06:31:16 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace content |
| 90 | |
| 91 | #endif // CONTENT_BROWSER_MOJO_BINDER_POLICY_MAP_IMPL_H_ |