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_CONDITION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/containers/hash_tables.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/values.h"
     17 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
     18 #include "extensions/common/matcher/url_matcher.h"
     19 #include "url/gurl.h"
     20 
     21 namespace extensions {
     22 
     23 struct RendererContentMatchData {
     24   RendererContentMatchData();
     25   ~RendererContentMatchData();
     26   // Match IDs for the URL of the top-level page the renderer is
     27   // returning data for.
     28   std::set<URLMatcherConditionSet::ID> page_url_matches;
     29   // All watched CSS selectors that match on frames with the same
     30   // origin as the page's main frame.
     31   base::hash_set<std::string> css_selectors;
     32 };
     33 
     34 // Representation of a condition in the Declarative Content API. A condition
     35 // consists of an optional URL and a list of CSS selectors. Each of these
     36 // attributes needs to be fulfilled in order for the condition to be fulfilled.
     37 //
     38 // We distinguish between two types of attributes:
     39 // - URL Matcher attributes are attributes that test the URL of a page.
     40 //   These are treated separately because we use a URLMatcher to efficiently
     41 //   test many of these attributes in parallel by using some advanced
     42 //   data structures. The URLMatcher tells us if all URL Matcher attributes
     43 //   are fulfilled for a ContentCondition.
     44 // - All other conditions are represented individually as fields in
     45 //   ContentCondition.  These conditions are probed linearly (only if the URL
     46 //   Matcher found a hit).
     47 //
     48 // TODO(battre) Consider making the URLMatcher an owner of the
     49 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
     50 // in url_matcher_condition_set(). This saves some copying in
     51 // ContentConditionSet::GetURLMatcherConditionSets.
     52 class ContentCondition {
     53  public:
     54   typedef RendererContentMatchData MatchData;
     55 
     56   ContentCondition(
     57       scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
     58       const std::vector<std::string>& css_selectors);
     59   ~ContentCondition();
     60 
     61   // Factory method that instantiates a ContentCondition according to the
     62   // description |condition| passed by the extension API.  |condition| should be
     63   // an instance of declarativeContent.PageStateMatcher.
     64   static scoped_ptr<ContentCondition> Create(
     65       URLMatcherConditionFactory* url_matcher_condition_factory,
     66       const base::Value& condition,
     67       std::string* error);
     68 
     69   // Returns whether the request is a match, given that the URLMatcher found
     70   // a match for |url_matcher_conditions_|.
     71   bool IsFulfilled(const RendererContentMatchData& renderer_data) const;
     72 
     73   // Returns a URLMatcherConditionSet::ID which is the canonical representation
     74   // for all URL patterns that need to be matched by this ContentCondition.
     75   // This ID is registered in a URLMatcher that can inform us in case of a
     76   // match.
     77   URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
     78     return url_matcher_conditions_->id();
     79   }
     80 
     81   // If this Condition has a url filter, appends it to |condition_sets|.
     82   void GetURLMatcherConditionSets(
     83       URLMatcherConditionSet::Vector* condition_sets) const {
     84     if (url_matcher_conditions_.get())
     85       condition_sets->push_back(url_matcher_conditions_);
     86   }
     87 
     88   // True if GetURLMatcherConditionSets would append anything to its
     89   // argument.
     90   bool has_url_matcher_condition_set() const {
     91     return url_matcher_conditions_.get() != NULL;
     92   }
     93 
     94   // Returns the CSS selectors required to match by this condition.
     95   const std::vector<std::string>& css_selectors() const {
     96     return css_selectors_;
     97   }
     98 
     99  private:
    100   scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
    101   std::vector<std::string> css_selectors_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(ContentCondition);
    104 };
    105 
    106 typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet;
    107 
    108 }  // namespace extensions
    109 
    110 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
    111