Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative_content/content_condition.h |
| diff --git a/chrome/browser/extensions/api/declarative_content/content_condition.h b/chrome/browser/extensions/api/declarative_content/content_condition.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1c961bb53908d4cc8b674ebc137f72475bf3075 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative_content/content_condition.h |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_ |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/hash_tables.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| +#include "chrome/common/extensions/matcher/url_matcher.h" |
| +#include "googleurl/src/gurl.h" |
|
battre
2012/12/15 11:54:10
nit: some of the includes are not necessary.
Jeffrey Yasskin
2013/01/17 09:22:56
Done.
|
| + |
| +namespace extensions { |
| + |
| +struct RendererContentMatchData { |
| + RendererContentMatchData(); |
| + ~RendererContentMatchData(); |
| + GURL page_url; |
| + base::hash_set<std::string> css_rules; |
|
battre
2012/12/15 11:54:10
are these css selectors? in that case, I think we
Jeffrey Yasskin
2013/01/17 09:22:56
Good point. Done.
|
| +}; |
| + |
| +// Representation of a condition in the Declarative Content API. A condition |
| +// consists of an optional URL and a list of CSS selectors. Each of these |
| +// attributes needs to be fulfilled in order for the condition to be fulfilled. |
| +// |
| +// We distinguish between two types of attributes: |
| +// - URL Matcher attributes are attributes that test the URL of a page. |
|
battre
2012/12/15 11:54:10
does this apply to main_frames or also to other fr
Jeffrey Yasskin
2013/01/17 09:22:56
For now, it's just top-level pages. I think the at
|
| +// These are treated separately because we use a URLMatcher to efficiently |
| +// test many of these attributes in parallel by using some advanced |
| +// data structures. The URLMatcher tells us if all URL Matcher attributes |
| +// are fulfilled for a ContentCondition. |
| +// - All other conditions are represented individually as fields in |
| +// ContentCondition. These conditions are probed linearly (only if the URL |
| +// Matcher found a hit). |
| +// |
| +// TODO(battre) Consider making the URLMatcher an owner of the |
| +// URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet |
| +// in url_matcher_condition_set(). This saves some copying in |
| +// ContentConditionSet::GetURLMatcherConditionSets. |
| +class ContentCondition { |
| + public: |
| + typedef RendererContentMatchData MatchData; |
| + |
| + ContentCondition( |
| + scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| + const std::vector<std::string>& css_rules); |
| + ~ContentCondition(); |
| + |
| + // Factory method that instantiates a ContentCondition according to the |
| + // description |condition| passed by the extension API. |condition| should be |
| + // an instance of declarativeContent.PageStateMatcher. |
| + static scoped_ptr<ContentCondition> Create( |
| + URLMatcherConditionFactory* url_matcher_condition_factory, |
| + const base::Value& condition, |
| + std::string* error); |
| + |
| + // Returns whether the request is a match, given that the URLMatcher found |
| + // a match for |url_matcher_conditions_|. |
| + bool IsFulfilled(const RendererContentMatchData& renderer_data) const; |
| + |
| + // Returns a URLMatcherConditionSet::ID which is the canonical representation |
| + // for all URL patterns that need to be matched by this ContentCondition. |
| + // This ID is registered in a URLMatcher that can inform us in case of a |
| + // match. |
| + URLMatcherConditionSet::ID url_matcher_condition_set_id() const { |
| + return url_matcher_conditions_->id(); |
| + } |
| + |
| + // Returns the set of conditions that are checked on the URL. This is the |
| + // primary trigger for ContentCondition and therefore never empty. (If it was |
| + // empty, the URLMatcher would never notify us about page changes which might |
| + // fulfill the entire ContentCondition). |
| + scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { |
| + return url_matcher_conditions_; |
| + } |
| + |
| + // Returns the CSS rules required to match by this condition. |
| + const std::vector<std::string>& css_rules() const { |
| + return css_rules_; |
| + } |
| + |
| + private: |
| + scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; |
| + std::vector<std::string> css_rules_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContentCondition); |
| +}; |
| + |
| +typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_ |