Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h

Issue 1159733004: Encapsulate CSS selector declarative content condition tracking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stars-declarative-content-range-for
Patch Set: iwyu Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CS S_CONDITION_TRACKER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CS S_CONDITION_TRACKER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/linked_ptr.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_observer.h"
19
20
21 namespace content {
22 class BrowserContext;
23 struct FrameNavigateParams;
24 struct LoadCommittedDetails;
25 class RenderProcessHost;
26 class WebContents;
27 }
28
29 namespace extensions {
30
31 class DeclarativeContentCssConditionTrackerDelegate {
32 public:
33 // Requests re-evaluation of conditions for |contents|.
34 virtual void RequestEvaluation(content::WebContents* contents) = 0;
35
36 // Returns true if the evaluator should manage condition state for |context|.
37 virtual bool ShouldManageConditionsForBrowserContext(
38 content::BrowserContext* context) = 0;
39
40 protected:
41 DeclarativeContentCssConditionTrackerDelegate();
42 virtual ~DeclarativeContentCssConditionTrackerDelegate();
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerDelegate);
46 };
47
48 // Supports watching of CSS selectors to across tab contents in a browser
49 // context, and querying for the matching CSS selectors for a context.
50 class DeclarativeContentCssConditionTracker
51 : public content::NotificationObserver {
52 public:
53 DeclarativeContentCssConditionTracker(
54 content::BrowserContext* context,
55 DeclarativeContentCssConditionTrackerDelegate* delegate);
56 ~DeclarativeContentCssConditionTracker() override;
57
58 // Sets the set of CSS selectors to watch for CSS condition evaluation.
59 void SetWatchedCssSelectors(
60 const std::set<std::string>& watched_css_selectors);
61
62 // Handles navigation of |contents|.
63 void OnWebContentsNavigation(content::WebContents* contents,
64 const content::LoadCommittedDetails& details,
65 const content::FrameNavigateParams& params);
66
67 // Inserts the currently-matching CSS selectors into |css_selectors|. We use
68 // hash_set for maximally efficient lookup.
69 void GetMatchingCssSelectors(content::WebContents* contents,
70 base::hash_set<std::string>* css_selectors);
71
72 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer
73 // depends on concrete condition implementations. At that point
74 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be
75 // able to use a test condition object and not need to depend on force setting
76 // matching CSS seleectors.
77 void UpdateMatchingCssSelectorsForTesting(
78 content::WebContents* contents,
79 const std::vector<std::string>& matching_css_selectors);
80
81 private:
82 class MatchedSelectorChangeObserver : public content::WebContentsObserver {
83 public:
84 using NotificationCallback =
85 base::Callback<void(content::WebContents*,
86 const std::vector<std::string>&)>;
87
88 MatchedSelectorChangeObserver(
89 content::WebContents* contents,
90 const NotificationCallback& notification_callback);
91 ~MatchedSelectorChangeObserver() override;
92
93 private:
94 // content::WebContentsObserver overrides.
95 bool OnMessageReceived(const IPC::Message& message) override;
96
97 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
98
99 NotificationCallback notification_callback_;
100
101 DISALLOW_COPY_AND_ASSIGN(MatchedSelectorChangeObserver);
102 };
103
104 // content::NotificationObserver implementation.
105 void Observe(int type,
106 const content::NotificationSource& source,
107 const content::NotificationDetails& details) override;
108
109 // If the renderer process is associated with our browser context, tells it
110 // what page attributes to watch for using an ExtensionMsg_WatchPages.
111 void InstructRenderProcessIfManagingBrowserContext(
112 content::RenderProcessHost* process);
113
114 // Updates the matching CSS selectors, from state provided by the renderer.
115 void UpdateMatchingCssSelectors(
116 content::WebContents* contents,
117 const std::vector<std::string>& matching_css_selectors);
118
119 // The context whose state we're monitoring for evaluation.
120 content::BrowserContext* context_;
121
122 // All CSS selectors that are watched for by any rule's conditions. This
123 // vector is sorted by construction.
124 std::vector<std::string> watched_css_selectors_;
125
126 // Maps WebContents to its matching CSS selectors.
127 std::map<content::WebContents*, std::vector<std::string>>
128 matching_css_selectors_;
129
130 // Maps WebContents to its observer monitoring for matched selector changes.
131 std::map<content::WebContents*, linked_ptr<MatchedSelectorChangeObserver>>
132 matched_selector_change_observers_;
133
134 // Weak.
135 DeclarativeContentCssConditionTrackerDelegate* delegate_;
136
137 // Manages our notification registrations.
138 content::NotificationRegistrar registrar_;
139
140 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker);
141 };
142
143 } // namespace extensions
144
145 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT _CSS_CONDITION_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698