Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
css_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_FILTER_H_
21 
22 #include "net/instaweb/rewriter/cached_result.pb.h"
43 
44 namespace Css {
45 
46 class Stylesheet;
47 
48 }
49 
50 namespace net_instaweb {
51 
52 class AssociationTransformer;
53 class AsyncFetch;
54 class CssImageRewriter;
55 class CacheExtender;
56 class ImageCombineFilter;
57 class ImageRewriteFilter;
58 class MessageHandler;
59 class RewriteDomainTransformer;
60 class Statistics;
61 class UpDownCounter;
62 class Variable;
63 
74 class CssFilter : public RewriteFilter {
75  public:
76  class Context;
77 
78  CssFilter(RewriteDriver* driver,
81  CacheExtender* cache_extender,
82  ImageRewriteFilter* image_rewriter,
83  ImageCombineFilter* image_combiner);
84  virtual ~CssFilter();
85 
88  static void InitStats(Statistics* statistics);
89 
91  static void Initialize();
92  static void Terminate();
93 
95  static void AddRelatedOptions(StringPieceVector* target);
96 
97  virtual void StartDocumentImpl();
98  virtual void StartElementImpl(HtmlElement* element);
99  virtual void Characters(HtmlCharactersNode* characters);
100  virtual void EndElementImpl(HtmlElement* element);
101 
102  virtual const char* Name() const { return "CssFilter"; }
103  virtual const char* id() const { return RewriteOptions::kCssFilterId; }
105  ResourceContext* context) const;
106 
107  static const char kBlocksRewritten[];
108  static const char kParseFailures[];
109  static const char kFallbackRewrites[];
110  static const char kFallbackFailures[];
111  static const char kRewritesDropped[];
112  static const char kTotalBytesSaved[];
113  static const char kTotalOriginalBytes[];
114  static const char kUses[];
115  static const char kCharsetMismatch[];
116  static const char kInvalidUrl[];
117  static const char kLimitExceeded[];
118  static const char kMinifyFailed[];
119  static const char kRecursion[];
120  static const char kComplexQueries[];
121 
122  RewriteContext* MakeNestedFlatteningContextInNewSlot(
123  const ResourcePtr& resource, const GoogleString& location,
124  CssFilter::Context* rewriter, RewriteContext* parent,
125  CssHierarchy* hierarchy);
126 
127  virtual const RewriteOptions::Filter* RelatedFilters(int* num_filters) const {
128  *num_filters = merged_filters_size_;
129  return merged_filters_;
130  }
131  virtual const StringPieceVector* RelatedOptions() const {
132  return related_options_;
133  }
134 
135  protected:
137  virtual const UrlSegmentEncoder* encoder() const;
139  RewriteContext* parent, const ResourceSlotPtr& slot);
140 
141  private:
142  friend class Context;
144  friend class CssHierarchy;
145 
146  enum InlineCssKind {
147  kInsideStyleTag,
148  kAttributeWithoutUrls,
149  kAttributeWithUrls
150  };
151 
152  Context* MakeContext(RewriteDriver* driver,
153  RewriteContext* parent);
154 
156  void StartInlineRewrite(HtmlCharactersNode* text,
157  HtmlElement* parent_element);
158 
161  void StartAttributeRewrite(HtmlElement* element,
162  HtmlElement::Attribute* style,
163  InlineCssKind inline_css_kind);
164 
167  void StartExternalRewrite(HtmlElement* link, HtmlElement::Attribute* src);
168 
169  ResourcePtr MakeInlineResource(StringPiece content);
170  CssFilter::Context* StartRewriting(const ResourceSlotPtr& slot);
171 
177  bool GetApplicableCharset(const HtmlElement* element,
178  GoogleString* charset,
179  GoogleString* failure_reason) const;
180 
183  bool GetApplicableMedia(const HtmlElement* element,
184  StringVector* media) const;
185 
186  bool in_style_element_;
187  HtmlElement* style_element_;
189 
191  GoogleString meta_tag_charset_;
192 
194  CacheExtender* cache_extender_;
195  ImageRewriteFilter* image_rewrite_filter_;
196  ImageCombineFilter* image_combiner_;
197 
201  Variable* num_blocks_rewritten_;
203  Variable* num_parse_failures_;
206  Variable* num_fallback_rewrites_;
208  Variable* num_fallback_failures_;
211  Variable* num_rewrites_dropped_;
217  UpDownCounter* total_bytes_saved_;
221  Variable* total_original_bytes_;
224  Variable* num_uses_;
226  Variable* num_flatten_imports_charset_mismatch_;
228  Variable* num_flatten_imports_invalid_url_;
230  Variable* num_flatten_imports_limit_exceeded_;
232  Variable* num_flatten_imports_minify_failed_;
234  Variable* num_flatten_imports_recursion_;
236  Variable* num_flatten_imports_complex_queries_;
237 
238  CssUrlEncoder encoder_;
239 
241  static const RewriteOptions::Filter* merged_filters_;
242  static int merged_filters_size_;
243 
245  static StringPieceVector* related_options_;
246 
247 
248 };
249 
252  public:
253  Context(CssFilter* filter, RewriteDriver* driver,
255  CacheExtender* cache_extender,
256  ImageRewriteFilter* image_rewriter,
257  ImageCombineFilter* image_combiner,
258  ResourceContext* context);
259  virtual ~Context();
260 
262  void SetupInlineRewrite(HtmlElement* style_element, HtmlCharactersNode* text);
263  void SetupAttributeRewrite(HtmlElement* element,
265  InlineCssKind inline_css_kind);
266  void SetupExternalRewrite(HtmlElement* element,
267  const GoogleUrl& base_gurl,
268  const GoogleUrl& trim_gurl);
269 
272  void RewriteCssFromNested(RewriteContext* parent, CssHierarchy* hierarchy);
273 
276  virtual bool SendFallbackResponse(StringPiece output_url_base,
277  StringPiece input_contents,
279  MessageHandler* handler);
280 
281  CssResourceSlotFactory* slot_factory() { return &slot_factory_; }
282 
283  CssHierarchy* mutable_hierarchy() { return &hierarchy_; }
284 
285  protected:
286  bool PolicyPermitsRendering() const override;
287  void Render() override;
288  virtual void Harvest();
289  virtual bool Partition(OutputPartitions* partitions,
290  OutputResourceVector* outputs);
291  virtual void RewriteSingle(const ResourcePtr& input,
292  const OutputResourcePtr& output);
293  virtual const char* id() const { return filter_->id(); }
294  virtual OutputResourceKind kind() const { return kRewrittenResource; }
295  virtual GoogleString CacheKeySuffix() const;
296  virtual const UrlSegmentEncoder* encoder() const;
297 
300  const ResourceContext* resource_context) const;
301 
302  private:
303  void GetCssBaseUrlToUse(const ResourcePtr& input_resource,
304  GoogleUrl* css_base_gurl_to_use);
305 
306  void GetCssTrimUrlToUse(const ResourcePtr& input_resource,
307  const StringPiece& output_url_base,
308  GoogleUrl* css_base_gurl_to_use);
309 
310  void GetCssTrimUrlToUse(const ResourcePtr& input_resource,
311  const OutputResourcePtr& output_resource,
312  GoogleUrl* css_base_gurl_to_use);
313 
314  bool RewriteCssText(const GoogleUrl& css_base_gurl,
315  const GoogleUrl& css_trim_gurl,
316  const StringPiece& in_text,
317  int64 in_text_size,
318  bool text_is_declarations,
319  MessageHandler* handler);
320 
322  void RewriteCssFromRoot(const GoogleUrl& css_base_gurl,
323  const GoogleUrl& css_trim_gurl,
324  const StringPiece& in_text, int64 in_text_size,
325  bool has_unparseables, Css::Stylesheet* stylesheet);
326 
332  bool FallbackRewriteUrls(const GoogleUrl& css_base_gurl,
333  const GoogleUrl& css_trim_gurl,
334  const StringPiece& in_text);
335 
338  bool SerializeCss(int64 in_text_size,
339  const Css::Stylesheet* stylesheet,
340  const GoogleUrl& css_base_gurl,
341  const GoogleUrl& css_trim_gurl,
342  bool previously_optimized,
343  bool stylesheet_is_declarations,
344  bool add_utf8_bom,
345  GoogleString* out_text,
346  MessageHandler* handler);
347 
351  bool IsInlineAttribute() const {
352  return (rewrite_inline_attribute_ != NULL);
353  }
354 
358  int64 ImageInlineMaxBytes() const;
359 
360  virtual bool ScheduleViaCentralController() { return true; }
361 
362  CssFilter* filter_;
363  scoped_ptr<CssImageRewriter> css_image_rewriter_;
364  ImageRewriteFilter* image_rewrite_filter_;
365  CssResourceSlotFactory slot_factory_;
366  CssHierarchy hierarchy_;
367  bool css_rewritten_;
368  bool has_utf8_bom_;
369 
371  bool fallback_mode_;
374  scoped_ptr<AssociationTransformer> fallback_transformer_;
377  scoped_ptr<RewriteDomainTransformer> absolutifier_;
378 
381  HtmlElement* rewrite_element_;
382 
386  HtmlElement* rewrite_inline_element_;
387 
389  HtmlCharactersNode* rewrite_inline_char_node_;
390 
394  HtmlElement::Attribute* rewrite_inline_attribute_;
395 
399  InlineCssKind rewrite_inline_css_kind_;
400 
402  int64 in_text_size_;
403  GoogleUrl initial_css_base_gurl_;
404  GoogleUrl initial_css_trim_gurl_;
405  scoped_ptr<GoogleUrl> base_gurl_for_fallback_;
406  scoped_ptr<GoogleUrl> trim_gurl_for_fallback_;
407  ResourcePtr input_resource_;
408  OutputResourcePtr output_resource_;
409 
410 
411 };
412 
413 }
414 
415 #endif
RewriteContext * parent()
Allows a nested rewriter to walk up its parent hierarchy.
Definition: rewrite_context.h:280
class GoogleUrl
Definition: google_url.h:58
Context used by CssFilter under async flow.
Definition: css_filter.h:251
Definition: html_element.h:66
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: css_filter.h:102
Definition: statistics.h:43
static void AddRelatedOptions(StringPieceVector *target)
Add this filters related options to the given vector.
virtual GoogleString CacheKeySuffix() const
bool PolicyPermitsRendering() const override
Leaf node representing raw characters in HTML.
Definition: html_node.h:167
virtual GoogleString UserAgentCacheKey(const ResourceContext *resource_context) const
Implements UserAgentCacheKey method of RewriteContext.
Definition: css_resource_slot.h:83
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
virtual OutputResourceKind kind() const
Definition: css_filter.h:294
Definition: css_hierarchy.h:64
Definition: html_element.h:42
Definition: ref_counted_ptr.h:69
static void InitStats(Statistics *statistics)
Context used by CssFilter under async flow that flattens .
Definition: css_flatten_imports_context.h:48
virtual const StringPieceVector * RelatedOptions() const
Definition: css_filter.h:131
virtual const UrlSegmentEncoder * encoder() const
virtual void StartDocumentImpl()
virtual RewriteContext * MakeRewriteContext()
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual const RewriteOptions::Filter * RelatedFilters(int *num_filters) const
Definition: css_filter.h:127
virtual RewriteContext * MakeNestedRewriteContext(RewriteContext *parent, const ResourceSlotPtr &slot)
Definition: async_fetch.h:53
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
SingleRewriteContext takes care of these methods from RewriteContext:
Definition: rewrite_filter.h:35
Filter
Definition: rewrite_options.h:106
Definition: rewrite_driver.h:100
Definition: rewrite_context.h:155
Definition: image_rewrite_filter.h:64
Derived from some input resource URL or URLs.
Definition: output_resource_kind.h:27
virtual const char * id() const
Returns the filter ID.
Definition: css_filter.h:293
CssFilter(RewriteDriver *driver, CacheExtender *cache_extender, ImageRewriteFilter *image_rewriter, ImageCombineFilter *image_combiner)
Definition: url_segment_encoder.h:33
Definition: single_rewrite_context.h:36
virtual const UrlSegmentEncoder * encoder() const
Definition: statistics.h:73
void SetupInlineRewrite(HtmlElement *style_element, HtmlCharactersNode *text)
Setup rewriting for inline, attribute, or external CSS.
static void Initialize()
Initialize & Terminate must be paired.
Definition: message_handler.h:39
Definition: image_combine_filter.h:58
Definition: css_url_encoder.h:49
void RewriteCssFromNested(RewriteContext *parent, CssHierarchy *hierarchy)
virtual void EncodeUserAgentIntoResourceContext(ResourceContext *context) const
Definition: cache_extender.h:49
Definition: css_filter.h:74
virtual void Characters(HtmlCharactersNode *characters)
virtual bool SendFallbackResponse(StringPiece output_url_base, StringPiece input_contents, AsyncFetch *async_fetch, MessageHandler *handler)
virtual void RewriteSingle(const ResourcePtr &input, const OutputResourcePtr &output)
Subclasses of SingleRewriteContext must override this:
friend class CssHierarchy
for statistics
Definition: css_filter.h:144
OutputResourceKind
Definition: output_resource_kind.h:26
AsyncFetch * async_fetch()
Note that the following must only be called in the fetch flow.