Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
javascript_code_block.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_JAVASCRIPT_CODE_BLOCK_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_CODE_BLOCK_H_
21 
22 #include <cstddef>
23 
24 #include "base/logging.h"
32 
33 namespace pagespeed { namespace js { struct JsTokenizerPatterns; } }
34 
35 namespace net_instaweb {
36 
37 class JavascriptLibraryIdentification;
38 class MessageHandler;
39 class Statistics;
40 class Variable;
41 
46  public:
48  static const char kBlocksMinified[];
49  static const char kLibrariesIdentified[];
50  static const char kMinificationFailures[];
51  static const char kTotalBytesSaved[];
52  static const char kTotalOriginalBytes[];
53  static const char kMinifyUses[];
54  static const char kNumReducingMinifications[];
55 
57  static const char kJSMinificationDisabled[];
58  static const char kJSDidNotShrink[];
59  static const char kJSFailedToWrite[];
60 
62  Statistics* statistics, bool minify, bool use_experimental_minifier,
63  const JavascriptLibraryIdentification* identification,
64  const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns);
65 
66  static void InitStats(Statistics* statistics);
67 
69  bool minify() const { return minify_; }
73  bool use_experimental_minifier() const { return use_experimental_minifier_; }
74  const JavascriptLibraryIdentification* library_identification() const {
75  return library_identification_;
76  }
77  const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns() const {
78  return js_tokenizer_patterns_;
79  }
80 
81  Variable* blocks_minified() { return blocks_minified_; }
82  Variable* libraries_identified() { return libraries_identified_; }
83  Variable* minification_failures() { return minification_failures_; }
84  Variable* total_bytes_saved() { return total_bytes_saved_; }
85  Variable* total_original_bytes() { return total_original_bytes_; }
86  Variable* num_uses() { return num_uses_; }
87  Variable* num_reducing_uses() { return num_reducing_minifications_; }
88 
89  Variable* minification_disabled() { return minification_disabled_; }
90  Variable* did_not_shrink() { return did_not_shrink_; }
91  Variable* failed_to_write() { return failed_to_write_; }
92 
93  private:
94  bool minify_;
95  bool use_experimental_minifier_;
97  const JavascriptLibraryIdentification* library_identification_;
98  const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
99 
104  Variable* blocks_minified_;
106  Variable* libraries_identified_;
108  Variable* minification_failures_;
110  Variable* total_bytes_saved_;
114  Variable* total_original_bytes_;
117  Variable* num_uses_;
119  Variable* num_reducing_minifications_;
120 
123  Variable* minification_disabled_;
125  Variable* did_not_shrink_;
127  Variable* failed_to_write_;
128 
129 
130 };
131 
140  public:
144  static const char kIntrospectionComment[];
145 
146  JavascriptCodeBlock(const StringPiece& original_code,
147  JavascriptRewriteConfig* config,
148  const StringPiece& message_id,
149  MessageHandler* handler);
150 
151  virtual ~JavascriptCodeBlock();
152 
156  bool Rewrite();
157 
160  bool successfully_rewritten() const {
161  DCHECK(rewritten_);
162  return successfully_rewritten_;
163  }
166  StringPiece rewritten_code() const {
167  DCHECK(rewritten_);
168  DCHECK(successfully_rewritten_);
169  return rewritten_code_;
170  }
171 
175  const source_map::MappingVector& SourceMappings() const {
176  DCHECK(rewritten_);
177  DCHECK(successfully_rewritten_);
178  return source_mappings_;
179  }
180 
187  void AppendSourceMapUrl(StringPiece url);
188 
195  StringPiece ComputeJavascriptLibrary() const;
196 
201  void SwapRewrittenString(GoogleString* other);
202 
205  static bool UnsafeToRename(const StringPiece& script);
206 
210  static void ToJsStringLiteral(const StringPiece& original,
211  GoogleString* escaped) {
212  EscapeToJsStringLiteral(original, true , escaped);
213  }
214 
218  static GoogleString JsUrlHash(const GoogleString &url, Hasher *hasher) {
219  GoogleString url_hash = hasher->Hash(GoogleUrl(url).PathAndLeaf());
222  size_t pos = 0;
223  while ((pos = url_hash.find_first_of('-', pos)) != GoogleString::npos) {
224  url_hash[pos] = '$';
225  }
226  return url_hash;
227  }
228 
230  const GoogleString& message_id() const { return message_id_; }
231 
232  private:
234  static bool IsSanitarySourceMapUrl(StringPiece url);
235 
237  bool MinifyJs(StringPiece input, GoogleString* output,
238  source_map::MappingVector* source_mappings);
239 
240  JavascriptRewriteConfig* config_;
241  const GoogleString message_id_;
242  const GoogleString original_code_;
243  GoogleString rewritten_code_;
244  source_map::MappingVector source_mappings_;
245 
248  bool rewritten_;
249  bool successfully_rewritten_;
250 
251  MessageHandler* handler_;
252 
253 
254 };
255 
256 }
257 
258 #endif
class GoogleUrl
Definition: google_url.h:58
void AppendSourceMapUrl(StringPiece url)
bool use_experimental_minifier() const
Definition: javascript_code_block.h:73
static void ToJsStringLiteral(const StringPiece &original, GoogleString *escaped)
Definition: javascript_code_block.h:210
static const char kJSMinificationDisabled[]
Those are JS rewrite failure type statistics.
Definition: javascript_code_block.h:57
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
GoogleString Hash(const StringPiece &content) const
StringPiece rewritten_code() const
Definition: javascript_code_block.h:166
static bool UnsafeToRename(const StringPiece &script)
void EscapeToJsStringLiteral(const StringPiece &original, bool add_quotes, GoogleString *escaped)
void SwapRewrittenString(GoogleString *other)
Definition: javascript_library_identification.h:34
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
StringPiece ComputeJavascriptLibrary() const
Definition: js_tokenizer.h:194
const source_map::MappingVector & SourceMappings() const
Definition: javascript_code_block.h:175
bool minify() const
Whether to minify javascript output.
Definition: javascript_code_block.h:69
const GoogleString & message_id() const
Get message id passed in at creation time, for external diagnostics.
Definition: javascript_code_block.h:230
bool successfully_rewritten() const
Definition: javascript_code_block.h:160
static GoogleString JsUrlHash(const GoogleString &url, Hasher *hasher)
Definition: javascript_code_block.h:218
Definition: javascript_code_block.h:45
Definition: message_handler.h:39
Definition: hasher.h:30
static const char kIntrospectionComment[]
Definition: javascript_code_block.h:144
static const char kBlocksMinified[]
Statistics names.
Definition: javascript_code_block.h:48
Definition: javascript_code_block.h:139