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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_
#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/extensions/api/declarative_content/content_predicate_evaluator.h"
#include "content/public/browser/render_process_host_creation_observer.h"
#include "content/public/browser/web_contents_observer.h"
namespace base {
class Value;
}
namespace extensions {
class Extension;
// Tests whether all the specified CSS selectors match on the page.
class DeclarativeContentCssPredicate : public ContentPredicate {
public:
DeclarativeContentCssPredicate(const DeclarativeContentCssPredicate&) =
delete;
DeclarativeContentCssPredicate& operator=(
const DeclarativeContentCssPredicate&) = delete;
~DeclarativeContentCssPredicate() override;
const std::vector<std::string>& css_selectors() const {
return css_selectors_;
}
static std::unique_ptr<DeclarativeContentCssPredicate> Create(
ContentPredicateEvaluator* evaluator,
const base::Value& value,
std::string* error);
// ContentPredicate:
ContentPredicateEvaluator* GetEvaluator() const override;
private:
DeclarativeContentCssPredicate(ContentPredicateEvaluator* evaluator,
const std::vector<std::string>& css_selectors);
// Weak.
const raw_ptr<ContentPredicateEvaluator, DanglingUntriaged> evaluator_;
std::vector<std::string> css_selectors_;
};
// Supports watching of CSS selectors to across tab contents in a browser
// context, and querying for the matching CSS selectors for a context.
class DeclarativeContentCssConditionTracker
: public ContentPredicateEvaluator,
public content::RenderProcessHostCreationObserver {
public:
explicit DeclarativeContentCssConditionTracker(Delegate* delegate);
DeclarativeContentCssConditionTracker(
const DeclarativeContentCssConditionTracker&) = delete;
DeclarativeContentCssConditionTracker& operator=(
const DeclarativeContentCssConditionTracker&) = delete;
~DeclarativeContentCssConditionTracker() override;
// ContentPredicateEvaluator:
std::string GetPredicateApiAttributeName() const override;
std::unique_ptr<const ContentPredicate> CreatePredicate(
const Extension* extension,
const base::Value& value,
std::string* error) override;
void TrackPredicates(
const std::map<const void*, std::vector<const ContentPredicate*>>&
predicates) override;
void StopTrackingPredicates(
const std::vector<const void*>& predicate_groups) override;
void TrackForWebContents(content::WebContents* contents) override;
void OnWebContentsNavigation(
content::WebContents* contents,
content::NavigationHandle* navigation_handle) override;
void OnWatchedPageChanged(
content::WebContents* contents,
const std::vector<std::string>& css_selectors) override;
bool EvaluatePredicate(const ContentPredicate* predicate,
content::WebContents* tab) const override;
private:
// Monitors CSS selector matching state on one WebContents.
class PerWebContentsTracker : public content::WebContentsObserver {
public:
using RequestEvaluationCallback =
base::RepeatingCallback<void(content::WebContents*)>;
using WebContentsDestroyedCallback =
base::OnceCallback<void(content::WebContents*)>;
PerWebContentsTracker(content::WebContents* contents,
RequestEvaluationCallback request_evaluation,
WebContentsDestroyedCallback web_contents_destroyed);
PerWebContentsTracker(const PerWebContentsTracker&) = delete;
PerWebContentsTracker& operator=(const PerWebContentsTracker&) = delete;
~PerWebContentsTracker() override;
void OnWebContentsNavigation(content::NavigationHandle* navigation_handle);
void OnWatchedPageChanged(const std::vector<std::string>& css_selectors);
const std::unordered_set<std::string>& matching_css_selectors() const {
return matching_css_selectors_;
}
private:
// content::WebContentsObserver overrides.
void WebContentsDestroyed() override;
const RequestEvaluationCallback request_evaluation_;
WebContentsDestroyedCallback web_contents_destroyed_;
// We use a hash_set for maximally efficient lookup.
std::unordered_set<std::string> matching_css_selectors_;
};
// content::RenderProcessHostCreationObserver implementation.
void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
// Informs renderer processes of a new set of watched CSS selectors.
void UpdateRenderersWatchedCssSelectors(
const std::vector<std::string>& watched_css_selectors);
// Returns the current list of watched CSS selectors.
std::vector<std::string> GetWatchedCssSelectors() const;
// If the renderer process is associated with our browser context, tells it
// what page attributes to watch for using the WatchPages Mojo method.
void InstructRenderProcessIfManagingBrowserContext(
content::RenderProcessHost* process,
std::vector<std::string> watched_css_selectors);
// Called by PerWebContentsTracker on web contents destruction.
void DeletePerWebContentsTracker(content::WebContents* contents);
// Weak.
raw_ptr<Delegate> delegate_;
// Maps the CSS selectors to watch to the number of predicates specifying
// them.
std::map<std::string, int> watched_css_selector_predicate_count_;
// Grouped predicates tracked by this object.
std::map<const void*,
std::vector<raw_ptr<const DeclarativeContentCssPredicate,
VectorExperimental>>>
tracked_predicates_;
// Maps WebContents to the tracker for that WebContents state.
std::map<content::WebContents*, std::unique_ptr<PerWebContentsTracker>>
per_web_contents_tracker_;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_
|