Chromium Code Reviews| 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 "content/public/browser/notification_observer.h" | |
| 16 #include "content/public/browser/notification_registrar.h" | |
| 17 #include "content/public/browser/web_contents_observer.h" | |
| 18 | |
| 19 | |
| 20 namespace content { | |
| 21 class BrowserContext; | |
| 22 struct FrameNavigateParams; | |
| 23 struct LoadCommittedDetails; | |
| 24 class RenderProcessHost; | |
| 25 class WebContents; | |
| 26 } | |
| 27 | |
| 28 namespace extensions { | |
| 29 | |
| 30 // Interface that allows the DeclarativeContentCssConditionTracker to request | |
| 31 // condition evaluation, and shields it from having to know about Browsers. | |
| 32 class DeclarativeContentCssConditionTrackerDelegate { | |
| 33 public: | |
| 34 // Callback for per-tab invocations. | |
| 35 using WebContentsCallback = | |
| 36 base::Callback<void(content::WebContents* content)>; | |
| 37 | |
| 38 // Requests re-evaluation of conditions for |contents|. | |
| 39 virtual void RequestEvaluation(content::WebContents* contents) = 0; | |
| 40 | |
| 41 // Returns true if the evaluator should manage condition state for |context|. | |
| 42 virtual bool ShouldManageConditionsForBrowserContext( | |
| 43 content::BrowserContext* context) = 0; | |
| 44 | |
| 45 // Runs the callback on every existing tab that should be tracked. | |
| 46 virtual void ForEachWebContents(const WebContentsCallback& callback) = 0; | |
| 47 | |
| 48 protected: | |
| 49 DeclarativeContentCssConditionTrackerDelegate(); | |
| 50 virtual ~DeclarativeContentCssConditionTrackerDelegate(); | |
| 51 | |
| 52 private: | |
| 53 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerDelegate); | |
| 54 }; | |
| 55 | |
| 56 // Supports watching of CSS selectors to across tab contents in a browser | |
| 57 // context, and querying for the matching CSS selectors for a context. | |
| 58 class DeclarativeContentCssConditionTracker | |
| 59 : public content::NotificationObserver { | |
| 60 public: | |
| 61 DeclarativeContentCssConditionTracker( | |
| 62 content::BrowserContext* context, | |
| 63 DeclarativeContentCssConditionTrackerDelegate* delegate); | |
| 64 ~DeclarativeContentCssConditionTracker() override; | |
| 65 | |
| 66 // Sets the set of CSS selectors to watch for CSS condition evaluation. | |
| 67 void SetWatchedCssSelectors( | |
| 68 const std::set<std::string>& watched_css_selectors); | |
| 69 | |
| 70 // Requests that CSS conditions be tracked for |contents|. | |
| 71 void TrackForWebContents(content::WebContents* contents); | |
| 72 | |
| 73 // Handles navigation of |contents|. | |
|
not at google - send to devlin
2015/06/05 17:16:30
You could quickly note here why DeclarativeContent
Mike Wittman
2015/06/05 21:00:46
Done.
| |
| 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 class PerWebContentsTracker; | |
| 94 | |
| 95 // Instantiate a PerWebContentsTracker watching |contents|. | |
| 96 void CreatePerWebContentsTracker(content::WebContents* contents); | |
| 97 | |
| 98 // content::NotificationObserver implementation. | |
| 99 void Observe(int type, | |
| 100 const content::NotificationSource& source, | |
| 101 const content::NotificationDetails& details) override; | |
| 102 | |
| 103 // If the renderer process is associated with our browser context, tells it | |
| 104 // what page attributes to watch for using an ExtensionMsg_WatchPages. | |
| 105 void InstructRenderProcessIfManagingBrowserContext( | |
| 106 content::RenderProcessHost* process); | |
| 107 | |
| 108 // Called by PerWebContentsTracker on destruction. | |
| 109 void RemovePerWebContentsTracker(PerWebContentsTracker* tracker); | |
| 110 | |
| 111 // The context whose state we're monitoring for evaluation. | |
| 112 content::BrowserContext* context_; | |
| 113 | |
| 114 // All CSS selectors that are watched for by any rule's conditions. This | |
| 115 // vector is sorted by construction. | |
| 116 std::vector<std::string> watched_css_selectors_; | |
| 117 | |
| 118 // Maps WebContents to the tracker for that WebContents | |
| 119 // state. PerWebContentsTracker owns itself, so this pointer is weak. | |
| 120 std::map<content::WebContents*, PerWebContentsTracker*> | |
| 121 per_web_contents_tracker_; | |
| 122 | |
| 123 // Weak. | |
| 124 DeclarativeContentCssConditionTrackerDelegate* delegate_; | |
| 125 | |
| 126 // Manages our notification registrations. | |
| 127 content::NotificationRegistrar registrar_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker); | |
| 130 }; | |
| 131 | |
| 132 } // namespace extensions | |
| 133 | |
| 134 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT _CSS_CONDITION_TRACKER_H_ | |
| OLD | NEW |