Page Speed Optimization Libraries
1.2.24.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_CODE_BLOCK_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_CODE_BLOCK_H_ 00021 00022 #include <cstddef> 00023 00024 #include "base/logging.h" 00025 #include "net/instaweb/util/public/basictypes.h" 00026 #include "net/instaweb/util/public/escaping.h" 00027 #include "net/instaweb/util/public/google_url.h" 00028 #include "net/instaweb/util/public/hasher.h" 00029 #include "net/instaweb/util/public/string.h" 00030 #include "net/instaweb/util/public/string_util.h" 00031 00032 namespace net_instaweb { 00033 00034 class JavascriptLibraryIdentification; 00035 class MessageHandler; 00036 class Statistics; 00037 class Variable; 00038 00042 class JavascriptRewriteConfig { 00043 public: 00044 JavascriptRewriteConfig( 00045 Statistics* statistics, bool minify, 00046 const JavascriptLibraryIdentification* identification); 00047 00048 static void InitStats(Statistics* statistics); 00049 00052 bool minify() const { return minify_; } 00053 const JavascriptLibraryIdentification* library_identification() const { 00054 return library_identification_; 00055 } 00056 00057 Variable* blocks_minified() { return blocks_minified_; } 00058 Variable* libraries_identified() { return libraries_identified_; } 00059 Variable* minification_failures() { return minification_failures_; } 00060 Variable* total_bytes_saved() { return total_bytes_saved_; } 00061 Variable* total_original_bytes() { return total_original_bytes_; } 00062 Variable* num_uses() { return num_uses_; } 00063 00064 Variable* minification_disabled() { return minification_disabled_; } 00065 Variable* did_not_shrink() { return did_not_shrink_; } 00066 Variable* failed_to_write() { return failed_to_write_; } 00067 00069 static const char kBlocksMinified[]; 00070 static const char kLibrariesIdentified[]; 00071 static const char kMinificationFailures[]; 00072 static const char kTotalBytesSaved[]; 00073 static const char kTotalOriginalBytes[]; 00074 static const char kMinifyUses[]; 00075 00077 static const char kJSMinificationDisabled[]; 00078 static const char kJSDidNotShrink[]; 00079 static const char kJSFailedToWrite[]; 00080 00081 private: 00082 bool minify_; 00084 const JavascriptLibraryIdentification* library_identification_; 00085 00088 Variable* blocks_minified_; 00090 Variable* libraries_identified_; 00092 Variable* minification_failures_; 00094 Variable* total_bytes_saved_; 00098 Variable* total_original_bytes_; 00101 Variable* num_uses_; 00102 00105 Variable* minification_disabled_; 00107 Variable* did_not_shrink_; 00109 Variable* failed_to_write_; 00110 00111 DISALLOW_COPY_AND_ASSIGN(JavascriptRewriteConfig); 00112 }; 00113 00121 class JavascriptCodeBlock { 00122 public: 00123 JavascriptCodeBlock(const StringPiece& original_code, 00124 JavascriptRewriteConfig* config, 00125 const StringPiece& message_id, 00126 MessageHandler* handler); 00127 00128 virtual ~JavascriptCodeBlock(); 00129 00132 static bool UnsafeToRename(const StringPiece& script); 00133 00136 bool ProfitableToRewrite() const { 00137 RewriteIfNecessary(); 00138 return (output_code_.size() < original_code_.size()); 00139 } 00140 00143 const StringPiece Rewritten() const { 00144 RewriteIfNecessary(); 00145 return output_code_; 00146 } 00147 00151 GoogleString* RewrittenString() const { 00152 RewriteIfNecessary(); 00153 DCHECK(rewritten_code_.size() < original_code_.size()); 00154 return &rewritten_code_; 00155 } 00156 00161 StringPiece ComputeJavascriptLibrary() const; 00162 00166 static void ToJsStringLiteral(const StringPiece& original, 00167 GoogleString* escaped) { 00168 EscapeToJsStringLiteral(original, true , escaped); 00169 } 00170 00174 static GoogleString JsUrlHash(const GoogleString &url, Hasher *hasher) { 00175 GoogleString url_hash = hasher->Hash(GoogleUrl(url).PathAndLeaf()); 00178 size_t pos = 0; 00179 while ((pos = url_hash.find_first_of('-', pos)) != GoogleString::npos) { 00180 url_hash[pos] = '$'; 00181 } 00182 return url_hash; 00183 } 00184 00186 const GoogleString& message_id() const { 00187 return message_id_; 00188 } 00189 00190 protected: 00192 void Rewrite() const; 00193 00194 JavascriptRewriteConfig* config_; 00195 const GoogleString message_id_; 00196 MessageHandler* handler_; 00197 const GoogleString original_code_; 00198 00199 private: 00200 void RewriteIfNecessary() const { 00201 if (!rewritten_) { 00202 Rewrite(); 00203 rewritten_ = true; 00204 } 00205 } 00206 00209 mutable bool rewritten_; 00210 00214 mutable StringPiece output_code_; 00215 mutable GoogleString rewritten_code_; 00216 00217 DISALLOW_COPY_AND_ASSIGN(JavascriptCodeBlock); 00218 }; 00219 00220 } 00221 00222 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_JAVASCRIPT_CODE_BLOCK_H_