1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MANAGER_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MANAGER_H_
#include <stddef.h>
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include <vector>
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/utils.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/permissions/permissions_data.h"
namespace content {
class BrowserContext;
class NavigationHandle;
class RenderFrameHost;
}
namespace extensions {
class ExtensionPrefs;
class PermissionHelper;
struct WebRequestInfo;
namespace declarative_net_request {
class CompositeMatcher;
struct RequestAction;
struct RequestParams;
// Manages the set of active rulesets for the Declarative Net Request API. Can
// be constructed on any sequence but must be accessed and destroyed from the
// same sequence.
class RulesetManager {
public:
explicit RulesetManager(content::BrowserContext* browser_context);
RulesetManager(const RulesetManager&) = delete;
RulesetManager& operator=(const RulesetManager&) = delete;
~RulesetManager();
// An observer used for testing purposes.
class TestObserver {
public:
virtual void OnEvaluateRequest(const WebRequestInfo& request,
bool is_incognito_context) {}
// Called whenever a ruleset is added or removed.
virtual void OnRulesetCountChanged(size_t new_count) {}
protected:
virtual ~TestObserver() {}
};
// Adds the ruleset for the given `extension_id`. Should not be called twice
// in succession for an extension.
void AddRuleset(const ExtensionId& extension_id,
std::unique_ptr<CompositeMatcher> matcher);
// Removes the ruleset for `extension_id`. Should be called only after a
// corresponding AddRuleset.
void RemoveRuleset(const ExtensionId& extension_id);
// Returns the set of extensions which have active rulesets.
std::set<ExtensionId> GetExtensionsWithRulesets() const;
// Returns the CompositeMatcher corresponding to the `extension_id` or null
// if no matcher is present for the extension.
CompositeMatcher* GetMatcherForExtension(const ExtensionId& extension_id);
const CompositeMatcher* GetMatcherForExtension(
const ExtensionId& extension_id) const;
// Returns the action to take for the given request before it is sent.
// Note: this can return an `ALLOW` or `ALLOW_ALL_REQUESTS` rule which is
// effectively a no-op. We do this to ensure that matched allow rules are
// correctly tracked by the `getMatchedRules` and `OnRuleMatchedDebug` APIs.
// Note: the returned action is owned by `request`.
const std::vector<RequestAction>& EvaluateBeforeRequest(
const WebRequestInfo& request,
bool is_incognito_context) const;
// Returns the action to take for the given request after response headers
// have been received.
// Note: See comments for `EvaluateBeforeRequest` above for notes on returning
// an ALLOW or ALLOW_ALL_REQUESTS action.
std::vector<RequestAction> EvaluateRequestWithHeaders(
const WebRequestInfo& request,
const net::HttpResponseHeaders* response_headers,
bool is_incognito_context) const;
// Returns true if there is an active matcher which modifies "extraHeaders".
bool HasAnyExtraHeadersMatcher() const;
// Returns true if there is a matcher which modifies "extraHeaders" for the
// given `request`.
bool HasExtraHeadersMatcherForRequest(const WebRequestInfo& request,
bool is_incognito_context) const;
void OnRenderFrameCreated(content::RenderFrameHost* host);
void OnRenderFrameDeleted(content::RenderFrameHost* host);
void OnDidFinishNavigation(content::NavigationHandle* navigation_handle);
// Returns if there are any matchers containing rules for the corresponding
// request matching `stage`.
bool HasRulesets(RulesetMatchingStage stage) const;
// Merges two lists of modifyHeaders actions and returns a list containing
// actions from both lists sorted in descending order of priority.
std::vector<RequestAction> MergeModifyHeaderActions(
std::vector<RequestAction> lhs_actions,
std::vector<RequestAction> rhs_actions) const;
// Returns the number of CompositeMatchers currently being managed.
size_t GetMatcherCountForTest() const { return rulesets_.size(); }
// Sets the TestObserver. Client maintains ownership of `observer`.
void SetObserverForTest(TestObserver* observer);
private:
struct ExtensionRulesetData {
ExtensionRulesetData(const ExtensionId& extension_id,
const base::Time& extension_install_time,
std::unique_ptr<CompositeMatcher> matcher);
ExtensionRulesetData(const ExtensionRulesetData&) = delete;
ExtensionRulesetData(ExtensionRulesetData&& other);
ExtensionRulesetData& operator=(const ExtensionRulesetData&) = delete;
ExtensionRulesetData& operator=(ExtensionRulesetData&& other);
~ExtensionRulesetData();
ExtensionId extension_id;
base::Time extension_install_time;
std::unique_ptr<CompositeMatcher> matcher;
bool operator<(const ExtensionRulesetData& other) const;
};
using RulesetAndPageAccess =
std::pair<const ExtensionRulesetData*, PermissionsData::PageAccess>;
std::optional<RequestAction> GetAction(
const std::vector<RulesetAndPageAccess>& rulesets,
const WebRequestInfo& request,
const RequestParams& params,
RulesetMatchingStage stage) const;
// Returns the list of matching modifyHeaders actions sorted in descending
// order of priority (`rulesets` is sorted in descending order of extension
// priority.)
std::vector<RequestAction> GetModifyHeadersActions(
const std::vector<RulesetAndPageAccess>& rulesets,
const WebRequestInfo& request,
const RequestParams& params,
RulesetMatchingStage stage) const;
// Helper for EvaluateRequest.
std::vector<RequestAction> EvaluateRequestInternal(
const WebRequestInfo& request,
const net::HttpResponseHeaders* response_headers,
bool is_incognito_context) const;
// Returns true if the given `request` should be evaluated for
// blocking/redirection.
bool ShouldEvaluateRequest(const WebRequestInfo& request) const;
// Returns whether `ruleset` should be evaluated for the given `request`.
// Returns true if it should and populates `host_permission_access`.
bool ShouldEvaluateRulesetForRequest(
const ExtensionRulesetData& ruleset,
const WebRequestInfo& request,
bool is_incognito_context,
PermissionsData::PageAccess& host_permission_access) const;
// Sorted in decreasing order of `extension_install_time`.
// Use a flat_set instead of std::set/map. This makes [Add/Remove]Ruleset
// O(n), but it's fine since the no. of rulesets are expected to be quite
// small.
base::flat_set<ExtensionRulesetData> rulesets_;
// Maps an extension ID to its install time. Used to determine an extension's
// ruleset matching priority order relative to other extensions, with matched
// rules/actions from more recently installed extensions having higher
// precedence.
std::map<ExtensionId, base::Time> extension_install_times_;
// Non-owning pointer to BrowserContext.
const raw_ptr<content::BrowserContext> browser_context_;
// Guaranteed to be valid through-out the lifetime of this instance.
const raw_ptr<ExtensionPrefs> prefs_;
const raw_ptr<PermissionHelper> permission_helper_;
// Non-owning pointer to TestObserver.
raw_ptr<TestObserver> test_observer_ = nullptr;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace declarative_net_request
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MANAGER_H_
|