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_CHROME_CONTENT_RULES_REGISTRY_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_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_content/content_action.h"
     18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h"
     19 #include "components/url_matcher/url_matcher.h"
     20 #include "content/public/browser/notification_observer.h"
     21 #include "content/public/browser/notification_registrar.h"
     22 #include "extensions/browser/api/declarative/declarative_rule.h"
     23 #include "extensions/browser/api/declarative_content/content_rules_registry.h"
     24 #include "extensions/browser/info_map.h"
     25 
     26 class ContentPermissions;
     27 
     28 namespace content {
     29 class BrowserContext;
     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 ChromeContentRulesRegistry 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 ChromeContentRulesRegistry : public ContentRulesRegistry,
     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   ChromeContentRulesRegistry(content::BrowserContext* browser_context,
     68                              RulesCacheDelegate* cache_delegate);
     69 
     70   // ChromeContentRulesRegistry implementation:
     71   // Applies all content rules given an update (CSS match change or
     72   // page navigation, for now) from the renderer.
     73   virtual void Apply(
     74       content::WebContents* contents,
     75       const std::vector<std::string>& matching_css_selectors) OVERRIDE;
     76 
     77   // Applies all content rules given that a tab was just navigated.
     78   virtual void DidNavigateMainFrame(
     79       content::WebContents* tab,
     80       const content::LoadCommittedDetails& details,
     81       const content::FrameNavigateParams& params) OVERRIDE;
     82 
     83   // Implementation of RulesRegistry:
     84   virtual std::string AddRulesImpl(
     85       const std::string& extension_id,
     86       const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE;
     87   virtual std::string RemoveRulesImpl(
     88       const std::string& extension_id,
     89       const std::vector<std::string>& rule_identifiers) OVERRIDE;
     90   virtual std::string RemoveAllRulesImpl(
     91       const std::string& extension_id) OVERRIDE;
     92 
     93   // content::NotificationObserver implementation.
     94   virtual void Observe(int type,
     95                        const content::NotificationSource& source,
     96                        const content::NotificationDetails& details) OVERRIDE;
     97 
     98   // Returns true if this object retains no allocated data. Only for debugging.
     99   bool IsEmpty() const;
    100 
    101  protected:
    102   virtual ~ChromeContentRulesRegistry();
    103 
    104   // Virtual for testing:
    105   virtual base::Time GetExtensionInstallationTime(
    106       const std::string& extension_id) const;
    107 
    108  private:
    109   friend class DeclarativeChromeContentRulesRegistryTest;
    110 
    111   std::set<ContentRule*> GetMatches(
    112       const RendererContentMatchData& renderer_data) const;
    113 
    114   // Scans the rules for the set of conditions they're watching.  If the set has
    115   // changed, calls InstructRenderProcess() for each RenderProcessHost in the
    116   // current browser_context.
    117   void UpdateConditionCache();
    118 
    119   // Tells a renderer what page attributes to watch for using an
    120   // ExtensionMsg_WatchPages.
    121   void InstructRenderProcess(content::RenderProcessHost* process);
    122 
    123   typedef std::map<url_matcher::URLMatcherConditionSet::ID, ContentRule*>
    124       URLMatcherIdToRule;
    125   typedef std::map<ContentRule::GlobalRuleId, linked_ptr<ContentRule> >
    126       RulesMap;
    127 
    128   // Map that tells us which ContentRules may match under the condition that
    129   // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
    130   URLMatcherIdToRule match_id_to_rule_;
    131 
    132   RulesMap content_rules_;
    133 
    134   // Maps tab_id to the set of rules that match on that tab.  This
    135   // lets us call Revert as appropriate.
    136   std::map<int, std::set<ContentRule*> > active_rules_;
    137 
    138   // Matches URLs for the page_url condition.
    139   url_matcher::URLMatcher url_matcher_;
    140 
    141   // All CSS selectors any rule's conditions watch for.
    142   std::vector<std::string> watched_css_selectors_;
    143 
    144   // Manages our notification registrations.
    145   content::NotificationRegistrar registrar_;
    146 
    147   scoped_refptr<InfoMap> extension_info_map_;
    148 
    149   DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry);
    150 };
    151 
    152 }  // namespace extensions
    153 
    154 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
    155