Chromium Code Reviews| 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..aebe9ee706a501ef6a81c66191eb4f62bbee9956 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h |
| @@ -0,0 +1,134 @@ |
| +// 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 "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 { |
| + |
| +// Interface that allows the DeclarativeContentCssConditionTracker to request |
| +// condition evaluation, and shields it from having to know about Browsers. |
| +class DeclarativeContentCssConditionTrackerDelegate { |
| + public: |
| + // Callback for per-tab invocations. |
| + using WebContentsCallback = |
| + base::Callback<void(content::WebContents* content)>; |
| + |
| + // 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; |
| + |
| + // Runs the callback on every existing tab that should be tracked. |
| + virtual void ForEachWebContents(const WebContentsCallback& callback) = 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); |
| + |
| + // Requests that CSS conditions be tracked for |contents|. |
| + void TrackForWebContents(content::WebContents* contents); |
| + |
| + // 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.
|
| + 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 PerWebContentsTracker; |
| + |
| + // Instantiate a PerWebContentsTracker watching |contents|. |
| + void CreatePerWebContentsTracker(content::WebContents* contents); |
| + |
| + // 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); |
| + |
| + // Called by PerWebContentsTracker on destruction. |
| + void RemovePerWebContentsTracker(PerWebContentsTracker* tracker); |
| + |
| + // 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 the tracker for that WebContents |
| + // state. PerWebContentsTracker owns itself, so this pointer is weak. |
| + std::map<content::WebContents*, PerWebContentsTracker*> |
| + per_web_contents_tracker_; |
| + |
| + // 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_ |