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_IMAGE_REWRITE_FILTER_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_ 00021 00022 #include "net/instaweb/htmlparse/public/html_element.h" 00023 #include "net/instaweb/rewriter/public/image.h" 00024 #include "net/instaweb/rewriter/public/image_url_encoder.h" 00025 #include "net/instaweb/rewriter/public/resource.h" 00026 #include "net/instaweb/rewriter/public/server_context.h" 00027 #include "net/instaweb/rewriter/public/resource_slot.h" 00028 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00029 #include "net/instaweb/rewriter/public/rewrite_options.h" 00030 #include "net/instaweb/rewriter/public/rewrite_result.h" 00031 #include "net/instaweb/util/public/basictypes.h" 00032 #include "net/instaweb/util/public/scoped_ptr.h" 00033 #include "net/instaweb/util/public/string.h" 00034 #include "net/instaweb/util/public/string_util.h" 00035 00036 namespace net_instaweb { 00037 00038 class CachedResult; 00039 class ContentType; 00040 class ImageDim; 00041 class ResourceContext; 00042 class RewriteContext; 00043 class RewriteDriver; 00044 class Statistics; 00045 class TimedVariable; 00046 class UrlSegmentEncoder; 00047 class Variable; 00048 class WorkBound; 00049 00053 class ImageRewriteFilter : public RewriteFilter { 00054 public: 00055 explicit ImageRewriteFilter(RewriteDriver* driver); 00056 virtual ~ImageRewriteFilter(); 00057 static void InitStats(Statistics* statistics); 00058 virtual void StartDocumentImpl(); 00059 virtual void StartElementImpl(HtmlElement* element) {} 00060 virtual void EndElementImpl(HtmlElement* element); 00061 virtual const char* Name() const { return "ImageRewrite"; } 00062 virtual const char* id() const { return RewriteOptions::kImageCompressionId; } 00063 00066 bool TryInline( 00067 int64 image_inline_max_bytes, const CachedResult* cached_result, 00068 ResourceSlot* slot, GoogleString* data_url); 00069 00089 static bool ParseDimensionAttribute(const char* position, int* value); 00090 00094 RewriteContext* MakeNestedRewriteContextForCss( 00095 int64 css_image_inline_max_bytes, 00096 RewriteContext* parent, 00097 const ResourceSlotPtr& slot); 00098 00101 virtual RewriteContext* MakeNestedRewriteContext(RewriteContext* parent, 00102 const ResourceSlotPtr& slot); 00103 00106 static bool UpdateDesiredImageDimsIfNecessary( 00107 const ImageDim& image_dim, RewriteDriver* driver, ImageDim* desired_dim); 00108 00110 static const char kImageOngoingRewrites[]; 00111 00113 static const char kImageNoRewritesHighResolution[]; 00114 00117 static const char kImageRewritesDroppedDueToLoad[]; 00118 00120 static const char kImageRewritesDroppedMIMETypeUnknown[]; 00121 00124 static const char kImageRewritesDroppedServerWriteFail[]; 00125 00128 static const char kImageRewritesDroppedNoSavingResize[]; 00129 00132 static const char kImageRewritesDroppedNoSavingNoResize[]; 00133 00137 static const char kInlinableImageUrlsPropertyName[]; 00138 00139 protected: 00140 virtual const UrlSegmentEncoder* encoder() const; 00141 00142 virtual RewriteContext* MakeRewriteContext(); 00143 00144 private: 00145 class Context; 00146 friend class Context; 00147 00149 const ContentType* ImageToContentType(const GoogleString& origin_url, 00150 Image* image); 00151 void BeginRewriteImageUrl(HtmlElement* element, HtmlElement::Attribute* src); 00152 00153 RewriteResult RewriteLoadedResourceImpl(Context* context, 00154 const ResourcePtr& input_resource, 00155 const OutputResourcePtr& result); 00156 00158 bool FinishRewriteCssImageUrl( 00159 int64 css_image_inline_max_bytes, 00160 const CachedResult* cached, ResourceSlot* slot); 00161 00163 bool FinishRewriteImageUrl( 00164 const CachedResult* cached, const ResourceContext* resource_context, 00165 HtmlElement* element, HtmlElement::Attribute* src, int image_index, 00166 ResourceSlot* slot); 00167 00169 void SaveIfInlinable(const StringPiece& contents, 00170 const Image::Type image_type, 00171 CachedResult* cached); 00172 00175 void GetDimensions(HtmlElement* element, ImageDim* page_dim); 00176 00179 bool HasAnyDimensions(HtmlElement* element); 00180 00183 void ResizeLowQualityImage( 00184 Image* low_image, const ResourcePtr& input_resource, 00185 CachedResult* cached); 00186 00191 bool IsCriticalImage(const StringPiece& image_url) const; 00192 00196 bool StoreUrlInPropertyCache(const StringPiece& url); 00197 00199 void SetAttemptWebp(StringPiece url, ResourceContext* resource_context); 00200 00201 scoped_ptr<WorkBound> work_bound_; 00202 00204 00206 Variable* image_rewrites_; 00208 Variable* image_norewrites_high_resolution_; 00212 Variable* image_rewrites_dropped_intentionally_; 00214 Variable* image_rewrites_dropped_mime_type_unknown_; 00217 Variable* image_rewrites_dropped_server_write_fail_; 00220 Variable* image_rewrites_dropped_nosaving_resize_; 00223 Variable* image_rewrites_dropped_nosaving_noresize_; 00225 TimedVariable* image_rewrites_dropped_due_to_load_; 00229 Variable* image_rewrite_total_bytes_saved_; 00233 Variable* image_rewrite_total_original_bytes_; 00236 Variable* image_rewrite_uses_; 00238 Variable* image_inline_count_; 00240 Variable* image_webp_rewrites_; 00241 00242 ImageUrlEncoder encoder_; 00243 00246 int image_counter_; 00247 00251 StringSet inlinable_urls_; 00252 00253 DISALLOW_COPY_AND_ASSIGN(ImageRewriteFilter); 00254 }; 00255 00256 } 00257 00258 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_