blob: 4fde2bb4ca89532b5e3bd60458239567f867d831 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2020 The Chromium Authors
Lingqi Chi775e60752020-12-14 06:31:162// 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 Hasana963a9342024-04-03 10:15:149#include <string_view>
Lingqi Chi775e60752020-12-14 06:31:1610
11#include "base/containers/flat_map.h"
Lingqi Chi775e60752020-12-14 06:31:1612#include "content/common/content_export.h"
13#include "content/public/browser/mojo_binder_policy_map.h"
14
15namespace content {
16
17// Implements MojoBinderPolicyMap and owns a policy map.
18class CONTENT_EXPORT MojoBinderPolicyMapImpl : public MojoBinderPolicyMap {
19 public:
20 MojoBinderPolicyMapImpl();
Lingqi Chi2e03d9d62021-11-08 05:45:4121
22 // This constructor is for testing.
Lingqi Chi775e60752020-12-14 06:31:1623 explicit MojoBinderPolicyMapImpl(
Lingqi Chi2e03d9d62021-11-08 05:45:4124 const base::flat_map<std::string, MojoBinderNonAssociatedPolicy>&
25 init_map);
Lingqi Chi775e60752020-12-14 06:31:1626 ~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 Chicd5239e2021-03-03 09:15:3335 // 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 Chi775e60752020-12-14 06:31:1640
Takashi Toyoshimaa35e5fc2023-10-20 04:00:3441 // 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 Chi2e03d9d62021-11-08 05:45:4145 // 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 Chi775e60752020-12-14 06:31:1653 const std::string& interface_name,
Lingqi Chi2e03d9d62021-11-08 05:45:4154 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 Chi775e60752020-12-14 06:31:1673 const std::string& interface_name) const;
74
75 private:
76 // MojoBinderPolicyMap implementation:
Md Hasibul Hasana963a9342024-04-03 10:15:1477 void SetPolicyByName(const std::string_view& name,
Lingqi Chi2e03d9d62021-11-08 05:45:4178 MojoBinderAssociatedPolicy policy) override;
Lingqi Chi775e60752020-12-14 06:31:1679
Md Hasibul Hasana963a9342024-04-03 10:15:1480 void SetPolicyByName(const std::string_view& name,
Lingqi Chi2e03d9d62021-11-08 05:45:4181 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 Chi775e60752020-12-14 06:31:1687};
88
89} // namespace content
90
91#endif // CONTENT_BROWSER_MOJO_BINDER_POLICY_MAP_IMPL_H_