OLD | NEW |
(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 // Interface that allows the DeclarativeContentCssConditionTracker to request |
| 32 // condition evaluation, and shields it from having to know about Browsers. |
| 33 class DeclarativeContentCssConditionTrackerDelegate { |
| 34 public: |
| 35 // Requests re-evaluation of conditions for |contents|. |
| 36 virtual void RequestEvaluation(content::WebContents* contents) = 0; |
| 37 |
| 38 // Returns true if the evaluator should manage condition state for |context|. |
| 39 virtual bool ShouldManageConditionsForBrowserContext( |
| 40 content::BrowserContext* context) = 0; |
| 41 |
| 42 protected: |
| 43 DeclarativeContentCssConditionTrackerDelegate(); |
| 44 virtual ~DeclarativeContentCssConditionTrackerDelegate(); |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerDelegate); |
| 48 }; |
| 49 |
| 50 // Supports watching of CSS selectors to across tab contents in a browser |
| 51 // context, and querying for the matching CSS selectors for a context. |
| 52 class DeclarativeContentCssConditionTracker |
| 53 : public content::NotificationObserver { |
| 54 public: |
| 55 DeclarativeContentCssConditionTracker( |
| 56 content::BrowserContext* context, |
| 57 DeclarativeContentCssConditionTrackerDelegate* delegate); |
| 58 ~DeclarativeContentCssConditionTracker() override; |
| 59 |
| 60 // Sets the set of CSS selectors to watch for CSS condition evaluation. |
| 61 void SetWatchedCssSelectors( |
| 62 const std::set<std::string>& watched_css_selectors); |
| 63 |
| 64 // Requests that CSS conditions be tracked for |contents|. |
| 65 void TrackForWebContents(content::WebContents* contents); |
| 66 |
| 67 // Handles navigation of |contents|. We depend on the caller to notify us of |
| 68 // this event rather than listening for it ourselves, so that the caller can |
| 69 // coordinate evaluation with all the trackers that respond to it. If we |
| 70 // listened ourselves and requested rule evaluation before another tracker |
| 71 // received the notification, our conditions would be evaluated based on the |
| 72 // new URL while the other tracker's conditions would still be evaluated based |
| 73 // on the previous URL. |
| 74 void OnWebContentsNavigation(content::WebContents* contents, |
| 75 const content::LoadCommittedDetails& details, |
| 76 const content::FrameNavigateParams& params); |
| 77 |
| 78 // Inserts the currently-matching CSS selectors into |css_selectors|. We use |
| 79 // hash_set for maximally efficient lookup. |
| 80 void GetMatchingCssSelectors(content::WebContents* contents, |
| 81 base::hash_set<std::string>* css_selectors); |
| 82 |
| 83 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer |
| 84 // depends on concrete condition implementations. At that point |
| 85 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be |
| 86 // able to use a test condition object and not need to depend on force setting |
| 87 // matching CSS seleectors. |
| 88 void UpdateMatchingCssSelectorsForTesting( |
| 89 content::WebContents* contents, |
| 90 const std::vector<std::string>& matching_css_selectors); |
| 91 |
| 92 private: |
| 93 // Monitors CSS selector matching state on one WebContents. |
| 94 class PerWebContentsTracker : public content::WebContentsObserver { |
| 95 public: |
| 96 using RequestEvaluationCallback = |
| 97 base::Callback<void(content::WebContents*)>; |
| 98 using WebContentsDestroyedCallback = |
| 99 base::Callback<void(content::WebContents*)>; |
| 100 |
| 101 PerWebContentsTracker( |
| 102 content::WebContents* contents, |
| 103 const RequestEvaluationCallback& request_evaluation, |
| 104 const WebContentsDestroyedCallback& web_contents_destroyed); |
| 105 ~PerWebContentsTracker() override; |
| 106 |
| 107 void OnWebContentsNavigation(const content::LoadCommittedDetails& details, |
| 108 const content::FrameNavigateParams& params); |
| 109 |
| 110 // See comment on similar function above. |
| 111 void UpdateMatchingCssSelectorsForTesting( |
| 112 const std::vector<std::string>& matching_css_selectors); |
| 113 |
| 114 const std::vector<std::string>& matching_css_selectors() const { |
| 115 return matching_css_selectors_; |
| 116 } |
| 117 |
| 118 private: |
| 119 // content::WebContentsObserver overrides. |
| 120 bool OnMessageReceived(const IPC::Message& message) override; |
| 121 void WebContentsDestroyed() override; |
| 122 |
| 123 void OnWatchedPageChange(const std::vector<std::string>& css_selectors); |
| 124 |
| 125 const RequestEvaluationCallback request_evaluation_; |
| 126 const WebContentsDestroyedCallback web_contents_destroyed_; |
| 127 |
| 128 std::vector<std::string> matching_css_selectors_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(PerWebContentsTracker); |
| 131 }; |
| 132 |
| 133 // Instantiate a PerWebContentsTracker watching |contents|. |
| 134 void CreatePerWebContentsTracker(content::WebContents* contents); |
| 135 |
| 136 // content::NotificationObserver implementation. |
| 137 void Observe(int type, |
| 138 const content::NotificationSource& source, |
| 139 const content::NotificationDetails& details) override; |
| 140 |
| 141 // If the renderer process is associated with our browser context, tells it |
| 142 // what page attributes to watch for using an ExtensionMsg_WatchPages. |
| 143 void InstructRenderProcessIfManagingBrowserContext( |
| 144 content::RenderProcessHost* process); |
| 145 |
| 146 // Called by PerWebContentsTracker on web contents destruction. |
| 147 void DeletePerWebContentsTracker(content::WebContents* tracker); |
| 148 |
| 149 // The context whose state we're monitoring for evaluation. |
| 150 content::BrowserContext* context_; |
| 151 |
| 152 // All CSS selectors that are watched for by any rule's conditions. This |
| 153 // vector is sorted by construction. |
| 154 std::vector<std::string> watched_css_selectors_; |
| 155 |
| 156 // Maps WebContents to the tracker for that WebContents |
| 157 // state. |
| 158 std::map<content::WebContents*, linked_ptr<PerWebContentsTracker>> |
| 159 per_web_contents_tracker_; |
| 160 |
| 161 // Weak. |
| 162 DeclarativeContentCssConditionTrackerDelegate* delegate_; |
| 163 |
| 164 // Manages our notification registrations. |
| 165 content::NotificationRegistrar registrar_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker); |
| 168 }; |
| 169 |
| 170 } // namespace extensions |
| 171 |
| 172 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT
_CSS_CONDITION_TRACKER_H_ |
OLD | NEW |