blob: a74e4a8f12346ef6ce4e5778f73331688544486e [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2020 The Chromium Authors
Lingqi Chifa0dda12020-11-18 05:32:452// 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_APPLIER_H_
6#define CONTENT_BROWSER_MOJO_BINDER_POLICY_APPLIER_H_
7
8#include <string>
9
Avi Drissmanadac21992023-01-11 23:46:3910#include "base/functional/bind.h"
11#include "base/functional/callback.h"
Lingqi Chi0611a4c2022-12-15 01:59:1712#include "base/functional/callback_forward.h"
Ali Hijazid87307d2022-11-07 20:15:0313#include "base/memory/raw_ref.h"
Lingqi Chi775e60752020-12-14 06:31:1614#include "content/browser/mojo_binder_policy_map_impl.h"
Lingqi Chifa0dda12020-11-18 05:32:4515#include "content/common/content_export.h"
16
17namespace content {
18
19// MojoBinderPolicyApplier is a helper class for `BrowserInterfaceBrokerImpl`
20// which allows control over when to run the binder registered for a
21// requested interface. This is useful in cases like prerendering pages, where
22// it can be desirable to defer binding until the page is activated, or take
23// other actions.
24//
25// The action to take for each interface is specified in the given
26// `MojoBinderPolicyMap`, and kDefer is used when no policy is specified.
27//
Sreeja Kamishetty9d399662022-07-14 20:07:5128// See content/browser/preloading/prerender/README.md for more about capability
29// control.
Lingqi Chifa0dda12020-11-18 05:32:4530class CONTENT_EXPORT MojoBinderPolicyApplier {
31 public:
Lingqi Chi0ca6ac52021-03-17 10:39:5532 enum class Mode {
33 // In the kEnforce mode, MojoBinderPolicyApplier processes binding requests
34 // strictly according to the pre-set policies.
35 kEnforce,
36 // If the page is about to activate, MojoBinderPolicyApplier will switch to
37 // the kPrepareToGrantAll mode, and all non-kGrant binders will be
38 // deferred.
39 kPrepareToGrantAll,
40 // In the kGrantAll mode, MojoBinderPolicyApplier grants all binding
41 // requests regardless of their policies.
42 kGrantAll,
43 };
44
Lingqi Chifa0dda12020-11-18 05:32:4545 // `policy_map` must outlive `this` and must not be null.
Lingqi Chifc4b7d9c2021-04-08 01:41:2246 // `cancel_callback` will be executed when ApplyPolicyToBinder() processes a
Lingqi Chifa0dda12020-11-18 05:32:4547 // kCancel interface.
Lingqi Chifc4b7d9c2021-04-08 01:41:2248 MojoBinderPolicyApplier(
49 const MojoBinderPolicyMapImpl* policy_map,
50 base::OnceCallback<void(const std::string& interface_name)>
51 cancel_callback);
Lingqi Chifa0dda12020-11-18 05:32:4552 ~MojoBinderPolicyApplier();
53
Lingqi Chicd5239e2021-03-03 09:15:3354 // Returns the instance used by BrowserInterfaceBrokerImpl for same-origin
55 // prerendering pages. This is used when the prerendered page and the page
56 // that triggered the prerendering are same origin.
57 static std::unique_ptr<MojoBinderPolicyApplier>
Lingqi Chifc4b7d9c2021-04-08 01:41:2258 CreateForSameOriginPrerendering(
59 base::OnceCallback<void(const std::string& interface_name)>
60 cancel_closure);
Lingqi Chiee8814f72021-01-20 07:35:0761
Takashi Toyoshimaa35e5fc2023-10-20 04:00:3462 // Returns the instance used by BrowserInterfaceBrokerImpl for preview mode.
63 // This is used when a page is shown in preview mode.
64 static std::unique_ptr<MojoBinderPolicyApplier> CreateForPreview(
65 base::OnceCallback<void(const std::string& interface_name)>
66 cancel_closure);
67
Lingqi Chifa0dda12020-11-18 05:32:4568 // Disallows copy and move operations.
69 MojoBinderPolicyApplier(const MojoBinderPolicyApplier& other) = delete;
70 MojoBinderPolicyApplier& operator=(const MojoBinderPolicyApplier& other) =
71 delete;
72 MojoBinderPolicyApplier(MojoBinderPolicyApplier&&) = delete;
73 MojoBinderPolicyApplier& operator=(MojoBinderPolicyApplier&&) = delete;
74
Lingqi Chi2e03d9d62021-11-08 05:45:4175 // Applies `MojoBinderNonAssociatedPolicy` before binding a non-associated
76 // interface.
Lingqi Chi0ca6ac52021-03-17 10:39:5577 // - In kEnforce mode:
78 // - kGrant: Runs `binder_callback` immediately.
79 // - kDefer: Saves `binder_callback` and runs it when GrantAll() is called.
Lingqi Chifc4b7d9c2021-04-08 01:41:2280 // - kCancel: Drops `binder_callback` and runs `cancel_callback_`.
Lingqi Chi0ca6ac52021-03-17 10:39:5581 // - kUnexpected: Unimplemented now.
82 // - In the kPrepareToGrantAll mode:
83 // - kGrant: Runs `binder_callback` immediately.
84 // - kDefer, kCancel and kUnexpected: Saves `binder_callback` and runs it
85 // when GrantAll() is called.
86 // - In the kGrantAll mode: this always runs the callback immediately.
Lingqi Chi2e03d9d62021-11-08 05:45:4187 void ApplyPolicyToNonAssociatedBinder(const std::string& interface_name,
88 base::OnceClosure binder_callback);
89
90 // Applies `MojoBinderAssociatedPolicy` before binding an associated
91 // interface. Note that this method only applies kCancel and kGrant to
92 // associated intefaces, because messages sent over associated interfaces
93 // cannot be deferred. See
94 // https://chromium.googlesource.com/chromium/src/+/HEAD/mojo/public/cpp/bindings/README.md#Associated-Interfaces
95 // for more information.
96 // Runs the cancellation callback and returns false if kCancel is applied.
97 // Otherwise returns true.
98 bool ApplyPolicyToAssociatedBinder(const std::string& interface_name);
99
Lingqi Chi0ca6ac52021-03-17 10:39:55100 // Switches this to the kPrepareToGrantAll mode.
101 void PrepareToGrantAll();
Lingqi Chi2e03d9d62021-11-08 05:45:41102
Lingqi Chifa0dda12020-11-18 05:32:45103 // Runs all deferred binders and runs binder callbacks for all subsequent
104 // requests, i.e., it stops applying the policies.
Lingqi Chi2e03d9d62021-11-08 05:45:41105
Lingqi Chifa0dda12020-11-18 05:32:45106 void GrantAll();
Lingqi Chi242d891e2021-03-10 09:45:49107 // Deletes all deferred binders without running them.
108 void DropDeferredBinders();
Lingqi Chifa0dda12020-11-18 05:32:45109
110 private:
Lingqi Chi242d891e2021-03-10 09:45:49111 friend class MojoBinderPolicyApplierTest;
112
Lingqi Chifa0dda12020-11-18 05:32:45113 // Gets the corresponding policy of the given mojo interface name.
Lingqi Chi2e03d9d62021-11-08 05:45:41114 MojoBinderNonAssociatedPolicy GetNonAssociatedMojoBinderPolicy(
115 const std::string& interface_name) const;
Lingqi Chifa0dda12020-11-18 05:32:45116
Lingqi Chi2e03d9d62021-11-08 05:45:41117 const MojoBinderNonAssociatedPolicy default_policy_ =
118 MojoBinderNonAssociatedPolicy::kDefer;
Lingqi Chifa0dda12020-11-18 05:32:45119 // Maps Mojo interface name to its policy.
Ali Hijazid87307d2022-11-07 20:15:03120 const raw_ref<const MojoBinderPolicyMapImpl> policy_map_;
Lingqi Chi0611a4c2022-12-15 01:59:17121
Lingqi Chifa0dda12020-11-18 05:32:45122 // Will be executed upon a request for a kCancel interface.
Lingqi Chifc4b7d9c2021-04-08 01:41:22123 base::OnceCallback<void(const std::string& interface_name)> cancel_callback_;
Lingqi Chi0ca6ac52021-03-17 10:39:55124 Mode mode_ = Mode::kEnforce;
Lingqi Chi0611a4c2022-12-15 01:59:17125
Lingqi Chifa0dda12020-11-18 05:32:45126 // Stores binders which are delayed running.
127 std::vector<base::OnceClosure> deferred_binders_;
Lingqi Chi0611a4c2022-12-15 01:59:17128
129 // Stores binders that can be used to send synchronous messages but
130 // are delayed running.
131 std::vector<base::OnceClosure> deferred_sync_binders_;
Lingqi Chifa0dda12020-11-18 05:32:45132};
133
134} // namespace content
135
136#endif // CONTENT_BROWSER_MOJO_BINDER_POLICY_APPLIER_H_