OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_CONTENT_RULES_REGISTRY
_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGISTRY
_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/time.h" |
| 15 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| 16 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
| 17 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" |
| 19 #include "chrome/browser/extensions/extension_info_map.h" |
| 20 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 21 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_registrar.h" |
| 23 |
| 24 class Profile; |
| 25 class ContentPermissions; |
| 26 |
| 27 namespace content { |
| 28 class RenderProcessHost; |
| 29 class WebContents; |
| 30 struct FrameNavigateParams; |
| 31 struct LoadCommittedDetails; |
| 32 } |
| 33 |
| 34 namespace extension_web_request_api_helpers { |
| 35 struct EventResponseDelta; |
| 36 } |
| 37 |
| 38 namespace net { |
| 39 class URLRequest; |
| 40 } |
| 41 |
| 42 namespace extensions { |
| 43 |
| 44 class RulesRegistryService; |
| 45 |
| 46 typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule; |
| 47 |
| 48 // The ContentRulesRegistry is responsible for managing |
| 49 // the internal representation of rules for the Declarative Content API. |
| 50 // |
| 51 // Here is the high level overview of this functionality: |
| 52 // |
| 53 // RulesRegistry::Rule consists of Conditions and Actions, these are |
| 54 // represented as a ContentRule with ContentConditions and |
| 55 // ContentRuleActions. |
| 56 // |
| 57 // ContentConditions represent JSON dictionaries as the following: |
| 58 // { |
| 59 // 'instanceType': 'URLMatcher', |
| 60 // 'host_suffix': 'example.com', |
| 61 // 'path_prefix': '/query', |
| 62 // 'scheme': 'http' |
| 63 // } |
| 64 // |
| 65 // The evaluation of URL related condition attributes (host_suffix, path_prefix) |
| 66 // is delegated to a URLMatcher, because this is capable of evaluating many |
| 67 // of such URL related condition attributes in parallel. |
| 68 // |
| 69 // For this, the URLRequestCondition has a URLMatcherConditionSet, which |
| 70 // represents the {'host_suffix': 'example.com', 'path_prefix': '/query'} part. |
| 71 // We will then ask the URLMatcher, whether a given URL |
| 72 // "http://www.example.com/query/" has any matches, and the URLMatcher |
| 73 // will respond with the URLMatcherConditionSet::ID. We can map this |
| 74 // to the ContentRule and check whether also the other conditions (in this |
| 75 // example 'scheme': 'http') are fulfilled. |
| 76 class ContentRulesRegistry : public RulesRegistryWithCache, |
| 77 public content::NotificationObserver { |
| 78 public: |
| 79 ContentRulesRegistry(Profile* profile, Delegate* delegate); |
| 80 |
| 81 // Tells a renderer what page attributes to watch for using an |
| 82 // ExtensionMsg_WatchPages. |
| 83 void InstructRenderProcess(content::RenderProcessHost* process); |
| 84 |
| 85 // Applies all content rules given an update from the renderer. |
| 86 void Apply(content::WebContents* contents, |
| 87 const std::vector<std::string>& matching_css_selectors); |
| 88 |
| 89 // Applies all content rules given that a tab was just navigated. |
| 90 void DidNavigateMainFrame(content::WebContents* tab, |
| 91 const content::LoadCommittedDetails& details, |
| 92 const content::FrameNavigateParams& params); |
| 93 |
| 94 // Keep track of what rules are currently active, for each tab_id. |
| 95 // When a message comes in for a tab, find the rules that match |
| 96 // it. Diff the sets. Call Action.Apply for the new actions; |
| 97 // Action.Revert for the old ones. Update the set. When a tab is |
| 98 // closed, clear the tab_id. Should we call Action.Revert in that |
| 99 // case? For PageActions, doesn't matter. |
| 100 |
| 101 // Implementation of RulesRegistryWithCache: |
| 102 virtual std::string AddRulesImpl( |
| 103 const std::string& extension_id, |
| 104 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE; |
| 105 virtual std::string RemoveRulesImpl( |
| 106 const std::string& extension_id, |
| 107 const std::vector<std::string>& rule_identifiers) OVERRIDE; |
| 108 virtual std::string RemoveAllRulesImpl( |
| 109 const std::string& extension_id) OVERRIDE; |
| 110 virtual content::BrowserThread::ID GetOwnerThread() const OVERRIDE; |
| 111 |
| 112 // content::NotificationObserver implementation. |
| 113 virtual void Observe(int type, |
| 114 const content::NotificationSource& source, |
| 115 const content::NotificationDetails& details) OVERRIDE; |
| 116 |
| 117 // Returns true if this object retains no allocated data. Only for debugging. |
| 118 bool IsEmpty() const; |
| 119 |
| 120 protected: |
| 121 virtual ~ContentRulesRegistry(); |
| 122 |
| 123 // Virtual for testing: |
| 124 virtual base::Time GetExtensionInstallationTime( |
| 125 const std::string& extension_id) const; |
| 126 |
| 127 private: |
| 128 std::set<ContentRule*> |
| 129 GetMatches(const RendererContentMatchData& renderer_data); |
| 130 |
| 131 // Scans the rules for the set of conditions they're watching. If the set has |
| 132 // changed, calls InstructRenderProcess() for each RenderProcessHost in the |
| 133 // current profile. |
| 134 void UpdateConditionCache(); |
| 135 |
| 136 typedef std::map<URLMatcherConditionSet::ID, ContentRule*> URLMatcherIdToRule; |
| 137 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> > |
| 138 RulesMap; |
| 139 |
| 140 Profile* const profile_; |
| 141 |
| 142 // Map that tells us which ContentRules may match under the condition that |
| 143 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
| 144 URLMatcherIdToRule match_id_to_rule_; |
| 145 |
| 146 RulesMap content_rules_; |
| 147 |
| 148 // Maps tab_id to the set of rules that match on that tab. |
| 149 std::map<int, std::set<ContentRule*> > active_rules_; |
| 150 |
| 151 // Matches URLs for the page_url condition. |
| 152 URLMatcher url_matcher_; |
| 153 |
| 154 // All CSS selectors any rule's conditions watch for. |
| 155 std::vector<std::string> watched_css_selectors_; |
| 156 |
| 157 // Manages our notification registrations. |
| 158 content::NotificationRegistrar registrar_; |
| 159 |
| 160 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
| 161 }; |
| 162 |
| 163 } // namespace extensions |
| 164 |
| 165 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_RULES_REGIS
TRY_H_ |
OLD | NEW |