Index: chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h |
diff --git a/chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h b/chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..34c1d7a5efdb7959a5b00f11b6fe43085ff258b5 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h |
@@ -0,0 +1,145 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// 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 <set> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/callback.h" |
+#include "base/containers/hash_tables.h" |
+#include "base/memory/linked_ptr.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+#include "content/public/browser/web_contents_observer.h" |
+ |
+ |
+namespace content { |
+class BrowserContext; |
+struct FrameNavigateParams; |
+struct LoadCommittedDetails; |
+class RenderProcessHost; |
+class WebContents; |
+} |
+ |
+namespace extensions { |
+ |
+class DeclarativeContentCssConditionTrackerDelegate { |
+ public: |
+ // Requests re-evaluation of conditions for |contents|. |
+ virtual void RequestEvaluation(content::WebContents* contents) = 0; |
+ |
+ // Returns true if the evaluator should manage condition state for |context|. |
+ virtual bool ShouldManageConditionsForBrowserContext( |
+ content::BrowserContext* context) = 0; |
+ |
+ protected: |
+ DeclarativeContentCssConditionTrackerDelegate(); |
+ virtual ~DeclarativeContentCssConditionTrackerDelegate(); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerDelegate); |
+}; |
+ |
+// 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 content::NotificationObserver { |
+ public: |
+ DeclarativeContentCssConditionTracker( |
+ content::BrowserContext* context, |
+ DeclarativeContentCssConditionTrackerDelegate* delegate); |
+ ~DeclarativeContentCssConditionTracker() override; |
+ |
+ // Sets the set of CSS selectors to watch for CSS condition evaluation. |
+ void SetWatchedCssSelectors( |
+ const std::set<std::string>& watched_css_selectors); |
+ |
+ // Handles navigation of |contents|. |
+ void OnWebContentsNavigation(content::WebContents* contents, |
+ const content::LoadCommittedDetails& details, |
+ const content::FrameNavigateParams& params); |
+ |
+ // Inserts the currently-matching CSS selectors into |css_selectors|. We use |
+ // hash_set for maximally efficient lookup. |
+ void GetMatchingCssSelectors(content::WebContents* contents, |
+ base::hash_set<std::string>* css_selectors); |
+ |
+ // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer |
+ // depends on concrete condition implementations. At that point |
+ // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be |
+ // able to use a test condition object and not need to depend on force setting |
+ // matching CSS seleectors. |
+ void UpdateMatchingCssSelectorsForTesting( |
+ content::WebContents* contents, |
+ const std::vector<std::string>& matching_css_selectors); |
+ |
+ private: |
+ class MatchedSelectorChangeObserver : public content::WebContentsObserver { |
+ public: |
+ using NotificationCallback = |
+ base::Callback<void(content::WebContents*, |
+ const std::vector<std::string>&)>; |
+ |
+ MatchedSelectorChangeObserver( |
+ content::WebContents* contents, |
+ const NotificationCallback& notification_callback); |
+ ~MatchedSelectorChangeObserver() override; |
+ |
+ private: |
+ // content::WebContentsObserver overrides. |
+ bool OnMessageReceived(const IPC::Message& message) override; |
+ |
+ void OnWatchedPageChange(const std::vector<std::string>& css_selectors); |
+ |
+ NotificationCallback notification_callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MatchedSelectorChangeObserver); |
+ }; |
+ |
+ // content::NotificationObserver implementation. |
+ void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) override; |
+ |
+ // If the renderer process is associated with our browser context, tells it |
+ // what page attributes to watch for using an ExtensionMsg_WatchPages. |
+ void InstructRenderProcessIfManagingBrowserContext( |
+ content::RenderProcessHost* process); |
+ |
+ // Updates the matching CSS selectors, from state provided by the renderer. |
+ void UpdateMatchingCssSelectors( |
+ content::WebContents* contents, |
+ const std::vector<std::string>& matching_css_selectors); |
+ |
+ // The context whose state we're monitoring for evaluation. |
+ content::BrowserContext* context_; |
+ |
+ // All CSS selectors that are watched for by any rule's conditions. This |
+ // vector is sorted by construction. |
+ std::vector<std::string> watched_css_selectors_; |
+ |
+ // Maps WebContents to its matching CSS selectors. |
+ std::map<content::WebContents*, std::vector<std::string>> |
+ matching_css_selectors_; |
+ |
+ // Maps WebContents to its observer monitoring for matched selector changes. |
+ std::map<content::WebContents*, linked_ptr<MatchedSelectorChangeObserver>> |
+ matched_selector_change_observers_; |
+ |
+ // Weak. |
+ DeclarativeContentCssConditionTrackerDelegate* delegate_; |
+ |
+ // Manages our notification registrations. |
+ content::NotificationRegistrar registrar_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTracker); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_CSS_CONDITION_TRACKER_H_ |