1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright (c) 2012 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_CHROME_CONTENT_RULES_REGISTRY_H_
#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/api/declarative_content/content_action.h"
#include "chrome/browser/extensions/api/declarative_content/content_condition.h"
#include "components/url_matcher/url_matcher.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/api/declarative/declarative_rule.h"
#include "extensions/browser/api/declarative_content/content_rules_registry.h"
#include "extensions/browser/info_map.h"
class ContentPermissions;
namespace content {
class BrowserContext;
class RenderProcessHost;
class WebContents;
struct FrameNavigateParams;
struct LoadCommittedDetails;
}
namespace extension_web_request_api_helpers {
struct EventResponseDelta;
}
namespace net {
class URLRequest;
}
namespace extensions {
class RulesRegistryService;
typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule;
// The ChromeContentRulesRegistry is responsible for managing
// the internal representation of rules for the Declarative Content API.
//
// Here is the high level overview of this functionality:
//
// RulesRegistry::Rule consists of Conditions and Actions, these are
// represented as a ContentRule with ContentConditions and
// ContentRuleActions.
//
// The evaluation of URL related condition attributes (host_suffix, path_prefix)
// is delegated to a URLMatcher, because this is capable of evaluating many
// of such URL related condition attributes in parallel.
class ChromeContentRulesRegistry : public ContentRulesRegistry,
public content::NotificationObserver {
public:
// For testing, |ui_part| can be NULL. In that case it constructs the
// registry with storage functionality suspended.
ChromeContentRulesRegistry(content::BrowserContext* browser_context,
RulesCacheDelegate* cache_delegate);
// ChromeContentRulesRegistry implementation:
// Applies all content rules given an update (CSS match change or
// page navigation, for now) from the renderer.
void Apply(content::WebContents* contents,
const std::vector<std::string>& matching_css_selectors) override;
// Applies all content rules given that a tab was just navigated.
void DidNavigateMainFrame(
content::WebContents* tab,
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) override;
// Implementation of RulesRegistry:
std::string AddRulesImpl(
const std::string& extension_id,
const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override;
std::string RemoveRulesImpl(
const std::string& extension_id,
const std::vector<std::string>& rule_identifiers) override;
std::string RemoveAllRulesImpl(const std::string& extension_id) override;
// content::NotificationObserver implementation.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// Returns true if this object retains no allocated data. Only for debugging.
bool IsEmpty() const;
protected:
~ChromeContentRulesRegistry() override;
// Virtual for testing:
virtual base::Time GetExtensionInstallationTime(
const std::string& extension_id) const;
private:
friend class DeclarativeChromeContentRulesRegistryTest;
std::set<ContentRule*> GetMatches(
const RendererContentMatchData& renderer_data) const;
// Scans the rules for the set of conditions they're watching. If the set has
// changed, calls InstructRenderProcess() for each RenderProcessHost in the
// current browser_context.
void UpdateConditionCache();
// Tells a renderer what page attributes to watch for using an
// ExtensionMsg_WatchPages.
void InstructRenderProcess(content::RenderProcessHost* process);
typedef std::map<url_matcher::URLMatcherConditionSet::ID, ContentRule*>
URLMatcherIdToRule;
typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> >
RulesMap;
// Map that tells us which ContentRules may match under the condition that
// the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
URLMatcherIdToRule match_id_to_rule_;
RulesMap content_rules_;
// Maps tab_id to the set of rules that match on that tab. This
// lets us call Revert as appropriate.
std::map<int, std::set<ContentRule*> > active_rules_;
// Matches URLs for the page_url condition.
url_matcher::URLMatcher url_matcher_;
// All CSS selectors any rule's conditions watch for.
std::vector<std::string> watched_css_selectors_;
// Manages our notification registrations.
content::NotificationRegistrar registrar_;
scoped_refptr<InfoMap> extension_info_map_;
DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
|