Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
css_flatten_imports_context.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 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_FLATTEN_IMPORTS_CONTEXT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_FLATTEN_IMPORTS_CONTEXT_H_
21 
22 #include "base/logging.h"
23 #include "net/instaweb/rewriter/cached_result.pb.h"
44 
45 namespace net_instaweb {
46 
49  public:
51  CssFilter* filter,
52  CssFilter::Context* rewriter,
53  CssHierarchy* hierarchy)
54  : SingleRewriteContext(NULL, parent, NULL ),
55  filter_(filter),
56  rewriter_(rewriter),
57  hierarchy_(hierarchy) {
58  }
59  virtual ~CssFlattenImportsContext() {}
60 
61  virtual GoogleString CacheKeySuffix() const {
66  GoogleString suffix;
67  if (hierarchy_->media().empty()) {
68  suffix = "all";
69  } else {
70  suffix = hierarchy_->media()[0];
71  for (int i = 1, n = hierarchy_->media().size(); i < n; ++i) {
72  StrAppend(&suffix, "_", hierarchy_->media()[i]);
73  }
74  }
75  return suffix;
76  }
77 
78  virtual void RewriteSingle(const ResourcePtr& input_resource,
79  const OutputResourcePtr& output_resource) {
80  input_resource_ = input_resource;
81  output_resource_ = output_resource;
82 
91  RewriteDriver* driver = Driver();
92  RewriteDomainTransformer transformer(&hierarchy_->css_base_url(),
93  &hierarchy_->css_trim_url(),
94  driver->server_context(),
95  driver->options(),
96  driver->message_handler());
99  StringWriter writer(hierarchy_->input_contents_backing_store());
102  if ( driver->server_context()->url_namer()->ProxyMode()
104  !driver->options()->trim_urls_in_css() ||
105  !driver->options()->Enabled(RewriteOptions::kLeftTrimUrls)) {
106  transformer.set_trim_urls(false);
107  }
109  input_resource_->ExtractUncompressedContents(), &writer,
110  &transformer, driver->message_handler())) {
111  hierarchy_->set_input_contents_to_backing_store();
112  hierarchy_->set_input_contents_resolved(true);
113  } else {
114  hierarchy_->set_input_contents(
115  input_resource_->ExtractUncompressedContents());
116  }
117 
118  bool ok = true;
119  GoogleString failure_reason;
120  if (!hierarchy_->Parse()) {
122  ok = false;
123  failure_reason = StrCat("Cannot parse the CSS in ",
124  hierarchy_->url_for_humans());
125  filter_->num_flatten_imports_minify_failed_->Add(1);
126  } else if (!hierarchy_->CheckCharsetOk(input_resource, &failure_reason)) {
127  ok = false;
128  filter_->num_flatten_imports_charset_mismatch_->Add(1);
129  } else {
130  rewriter_->RewriteCssFromNested(this, hierarchy_);
131  }
132 
133  if (!ok) {
134  hierarchy_->set_flattening_succeeded(false);
135  hierarchy_->AddFlatteningFailureReason(failure_reason);
137  } else if (num_nested() > 0) {
138  StartNestedTasks();
139  } else {
140  Harvest();
141  }
142  }
143 
144  void Harvest() {
145  DCHECK_EQ(1, num_output_partitions());
146 
149 
155  hierarchy_->RollUpContents();
156 
158  mutable_output_partition(0)->set_inlined_data(
159  hierarchy_->minified_contents());
160 
161  ServerContext* server_context = FindServerContext();
162  server_context->MergeNonCachingResponseHeaders(input_resource_,
163  output_resource_);
164  if (Driver()->Write(ResourceVector(1, input_resource_),
165  hierarchy_->minified_contents(),
166  &kContentTypeCss,
167  input_resource_->charset(),
168  output_resource_.get())) {
170  } else {
172  }
173  }
174 
175  bool PolicyPermitsRendering() const {
177  return true;
178  }
179 
180  virtual void Render() {
182  if (num_output_partitions() == 1 && output_partition(0)->optimizable()) {
192  if (hierarchy_->minified_contents().empty()) {
193  hierarchy_->set_minified_contents(
194  output_partition(0)->inlined_data());
195  hierarchy_->set_input_contents(hierarchy_->minified_contents());
198  hierarchy_->Parse();
199  }
200  } else {
206  hierarchy_->set_flattening_succeeded(false);
207  }
208  }
209 
210  virtual const char* id() const {
211  return RewriteOptions::kCssImportFlattenerId;
212  }
213  virtual OutputResourceKind kind() const { return kRewrittenResource; }
214 
215  private:
216  CssFilter* filter_;
217  CssFilter::Context* rewriter_;
218  CssHierarchy* hierarchy_;
219  ResourcePtr input_resource_;
220  OutputResourcePtr output_resource_;
221 
222 };
223 
224 }
225 
226 #endif
RewriteContext * parent()
Allows a nested rewriter to walk up its parent hierarchy.
Definition: rewrite_context.h:280
Context used by CssFilter under async flow.
Definition: css_filter.h:251
void MergeNonCachingResponseHeaders(const ResourcePtr &input, const OutputResourcePtr &output)
Definition: server_context.h:494
virtual void Render()
Definition: css_flatten_imports_context.h:180
Definition: css_tag_scanner.h:147
static bool TransformUrls(StringPiece contents, Writer *writer, Transformer *transformer, MessageHandler *handler)
virtual bool trim_urls_in_css() const
Return false in a subclass if you want to disallow all URL trimming in CSS.
Definition: rewrite_options.h:2278
virtual OutputResourceKind kind() const
Definition: css_flatten_imports_context.h:213
Definition: css_hierarchy.h:64
bool CheckCharsetOk(const ResourcePtr &resource, GoogleString *failure_reason)
int num_output_partitions() const
rewrite went fine
Definition: rewrite_result.h:29
bool PolicyPermitsRendering() const
Definition: css_flatten_imports_context.h:175
Context used by CssFilter under async flow that flattens .
Definition: css_flatten_imports_context.h:48
virtual void RewriteSingle(const ResourcePtr &input_resource, const OutputResourcePtr &output_resource)
Subclasses of SingleRewriteContext must override this:
Definition: css_flatten_imports_context.h:78
rewrite is impossible or undesirable
Definition: rewrite_result.h:28
void RewriteDone(RewriteResult result, int partition_index)
virtual GoogleString CacheKeySuffix() const
Definition: css_flatten_imports_context.h:61
void Harvest()
Definition: css_flatten_imports_context.h:144
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
ServerContext * FindServerContext() const
virtual const char * id() const
Returns the filter ID.
Definition: css_flatten_imports_context.h:210
void set_input_contents(const StringPiece input_contents)
Definition: css_hierarchy.h:113
int num_nested() const
Accessors for the nested rewrites.
Definition: rewrite_context.h:284
virtual ProxyExtent ProxyMode() const
Definition: url_namer.h:91
Definition: rewrite_driver.h:100
Definition: server_context.h:99
StringPiece ExtractUncompressedContents() const
Definition: rewrite_context.h:155
Derived from some input resource URL or URLs.
Definition: output_resource_kind.h:27
Definition: single_rewrite_context.h:36
StringPiece charset() const
Note: this is empty if the header is not specified.
Definition: resource.h:193
static void InheritChildImageInfo(RewriteContext *context)
Writer implementation for directing HTML output to a string.
Definition: string_writer.h:32
void RewriteCssFromNested(RewriteContext *parent, CssHierarchy *hierarchy)
Definition: css_filter.h:74
const RewriteOptions * options() const
Return the options used for this RewriteDriver.
Definition: rewrite_driver.h:496
CssFlattenImportsContext(RewriteContext *parent, CssFilter *filter, CssFilter::Context *rewriter, CssHierarchy *hierarchy)
Definition: css_flatten_imports_context.h:50
int64 Add(int64 non_negative_delta)
Adds 'delta' to the variable's value, returning the result.
Definition: statistics.h:53
OutputResourceKind
Definition: output_resource_kind.h:26
void AddFlatteningFailureReason(const GoogleString &reason)