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