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 EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_
      6 #define EXTENSIONS_BROWSER_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 "components/url_matcher/url_matcher.h"
     16 #include "extensions/browser/api/declarative/declarative_rule.h"
     17 #include "extensions/browser/api/declarative_webrequest/webrequest_condition_attribute.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<url_matcher::URLMatcherConditionSet::ID> url_match_ids;
     48   std::set<url_matcher::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<url_matcher::URLMatcherConditionSet> url_matcher_conditions,
     74       scoped_refptr<url_matcher::URLMatcherConditionSet>
     75           first_party_url_matcher_conditions,
     76       const WebRequestConditionAttributes& condition_attributes);
     77   ~WebRequestCondition();
     78 
     79   // Factory method that instantiates a WebRequestCondition according to
     80   // the description |condition| passed by the extension API.
     81   static scoped_ptr<WebRequestCondition> Create(
     82       const Extension* extension,
     83       url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory,
     84       const base::Value& condition,
     85       std::string* error);
     86 
     87   // Returns whether the request matches this condition.
     88   bool IsFulfilled(const MatchData& request_data) const;
     89 
     90   // If this condition has url attributes, appends them to |condition_sets|.
     91   void GetURLMatcherConditionSets(
     92       url_matcher::URLMatcherConditionSet::Vector* condition_sets) const;
     93 
     94   // Returns a bit vector representing extensions::RequestStage. The bit vector
     95   // contains a 1 for each request stage during which the condition can be
     96   // tested.
     97   int stages() const { return applicable_request_stages_; }
     98 
     99  private:
    100   // URL attributes of this condition.
    101   scoped_refptr<url_matcher::URLMatcherConditionSet> url_matcher_conditions_;
    102   scoped_refptr<url_matcher::URLMatcherConditionSet>
    103       first_party_url_matcher_conditions_;
    104 
    105   // All non-UrlFilter attributes of this condition.
    106   WebRequestConditionAttributes condition_attributes_;
    107 
    108   // Bit vector indicating all RequestStage during which all
    109   // |condition_attributes_| can be evaluated.
    110   int applicable_request_stages_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(WebRequestCondition);
    113 };
    114 
    115 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet;
    116 
    117 }  // namespace extensions
    118 
    119 #endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_H_
    120