Home | History | Annotate | Download | only in declarative_webrequest
      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_WEBREQUEST_WEBREQUEST_CONDITION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/memory/linked_ptr.h"
     15 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
     16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute.h"
     17 #include "extensions/common/matcher/url_matcher.h"
     18 #include "net/http/http_response_headers.h"
     19 
     20 namespace extensions {
     21 
     22 // Container for information about a URLRequest to determine which
     23 // rules apply to the request.
     24 struct WebRequestData {
     25   WebRequestData(net::URLRequest* request, RequestStage stage);
     26   WebRequestData(
     27       net::URLRequest* request,
     28       RequestStage stage,
     29       const net::HttpResponseHeaders* original_response_headers);
     30   ~WebRequestData();
     31 
     32   // The network request that is currently being processed.
     33   net::URLRequest* request;
     34   // The stage (progress) of the network request.
     35   RequestStage stage;
     36   // Additional information about requests that is not
     37   // available in all request stages.
     38   const net::HttpResponseHeaders* original_response_headers;
     39 };
     40 
     41 // Adds information about URL matches to WebRequestData.
     42 struct WebRequestDataWithMatchIds {
     43   explicit WebRequestDataWithMatchIds(const WebRequestData* request_data);
     44   ~WebRequestDataWithMatchIds();
     45 
     46   const WebRequestData* data;
     47   std::set<URLMatcherConditionSet::ID> url_match_ids;
     48   std::set<URLMatcherConditionSet::ID> first_party_url_match_ids;
     49 };
     50 
     51 // Representation of a condition in the Declarative WebRequest API. A condition
     52 // consists of several attributes. Each of these attributes needs to be
     53 // fulfilled in order for the condition to be fulfilled.
     54 //
     55 // We distinguish between two types of conditions:
     56 // - URL Matcher conditions are conditions that test the URL of a request.
     57 //   These are treated separately because we use a URLMatcher to efficiently
     58 //   test many of these conditions in parallel by using some advanced
     59 //   data structures. The URLMatcher tells us if all URL Matcher conditions
     60 //   are fulfilled for a WebRequestCondition.
     61 // - All other conditions are represented as WebRequestConditionAttributes.
     62 //   These conditions are probed linearly (only if the URL Matcher found a hit).
     63 //
     64 // TODO(battre) Consider making the URLMatcher an owner of the
     65 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
     66 // in url_matcher_condition_set(). This saves some copying in
     67 // WebRequestConditionSet::GetURLMatcherConditionSets.
     68 class WebRequestCondition {
     69  public:
     70   typedef WebRequestDataWithMatchIds MatchData;
     71 
     72   WebRequestCondition(
     73       scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
     74       scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions,
     75       const WebRequestConditionAttributes& condition_attributes);
     76   ~WebRequestCondition();
     77 
     78   // Factory method that instantiates a WebRequestCondition according to
     79   // the description |condition| passed by the extension API.
     80   static scoped_ptr<WebRequestCondition> Create(
     81       URLMatcherConditionFactory* url_matcher_condition_factory,
     82       const base::Value& condition,
     83       std::string* error);
     84 
     85   // Returns whether the request matches this condition.
     86   bool IsFulfilled(const MatchData& request_data) const;
     87 
     88   // If this condition has url attributes, appends them to |condition_sets|.
     89   void GetURLMatcherConditionSets(
     90       URLMatcherConditionSet::Vector* condition_sets) const;
     91 
     92   // Returns a bit vector representing extensions::RequestStage. The bit vector
     93   // contains a 1 for each request stage during which the condition can be
     94   // tested.
     95   int stages() const { return applicable_request_stages_; }
     96 
     97  private:
     98   // URL attributes of this condition.
     99   scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
    100   scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions_;
    101 
    102   // All non-UrlFilter attributes of this condition.
    103   WebRequestConditionAttributes condition_attributes_;
    104 
    105   // Bit vector indicating all RequestStage during which all
    106   // |condition_attributes_| can be evaluated.
    107   int applicable_request_stages_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(WebRequestCondition);
    110 };
    111 
    112 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet;
    113 
    114 }  // namespace extensions
    115 
    116 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_
    117