Page Speed Optimization Libraries
1.3.25.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 Histogram; 00042 class ResourceContext; 00043 class RewriteContext; 00044 class RewriteDriver; 00045 class Statistics; 00046 class TimedVariable; 00047 class UrlSegmentEncoder; 00048 class Variable; 00049 class WorkBound; 00050 00054 class ImageRewriteFilter : public RewriteFilter { 00055 public: 00057 static const char kImageOngoingRewrites[]; 00058 00060 static const char kImageNoRewritesHighResolution[]; 00061 00064 static const char kImageRewritesDroppedDueToLoad[]; 00065 00067 static const char kImageRewritesDroppedMIMETypeUnknown[]; 00068 00071 static const char kImageRewritesDroppedServerWriteFail[]; 00072 00075 static const char kImageRewritesDroppedNoSavingResize[]; 00076 00079 static const char kImageRewritesDroppedNoSavingNoResize[]; 00080 00082 static const char kImageRewritesSquashingForMobileScreen[]; 00083 00085 static const char kImageRewriteLatencyOkMs[]; 00086 00088 static const char kImageRewriteLatencyFailedMs[]; 00089 00093 static const char kInlinableImageUrlsPropertyName[]; 00094 00095 static const RewriteOptions::Filter kRelatedFilters[]; 00096 static const int kRelatedFiltersSize; 00097 static const RewriteOptions::OptionEnum kRelatedOptions[]; 00098 static const int kRelatedOptionsSize; 00099 00100 explicit ImageRewriteFilter(RewriteDriver* driver); 00101 virtual ~ImageRewriteFilter(); 00102 static void InitStats(Statistics* statistics); 00103 virtual void StartDocumentImpl(); 00104 virtual void StartElementImpl(HtmlElement* element) {} 00105 virtual void EndElementImpl(HtmlElement* element); 00106 virtual const char* Name() const { return "ImageRewrite"; } 00107 virtual const char* id() const { return RewriteOptions::kImageCompressionId; } 00108 virtual void EncodeUserAgentIntoResourceContext( 00109 ResourceContext* context) const; 00110 00113 bool TryInline( 00114 int64 image_inline_max_bytes, const CachedResult* cached_result, 00115 ResourceSlot* slot, GoogleString* data_url); 00116 00136 static bool ParseDimensionAttribute(const char* position, int* value); 00137 00141 RewriteContext* MakeNestedRewriteContextForCss( 00142 int64 css_image_inline_max_bytes, 00143 RewriteContext* parent, 00144 const ResourceSlotPtr& slot); 00145 00148 virtual RewriteContext* MakeNestedRewriteContext(RewriteContext* parent, 00149 const ResourceSlotPtr& slot); 00150 00153 bool UpdateDesiredImageDimsIfNecessary( 00154 const ImageDim& image_dim, const ResourceContext& resource_context, 00155 ImageDim* desired_dim); 00156 00160 bool ShouldResize(const ResourceContext& context, 00161 Image* image, 00162 ImageDim* desired_dimensions); 00163 00166 bool ResizeImageIfNecessary( 00167 const RewriteContext* rewrite_context, const GoogleString& url, 00168 ResourceContext* context, Image* image, CachedResult* cached); 00169 00172 Image::CompressionOptions* ImageOptionsForLoadedResource( 00173 const ResourceContext& context, const ResourcePtr& input_resource, 00174 bool is_css); 00175 00176 virtual const RewriteOptions::Filter* RelatedFilters(int* num_filters) const; 00177 virtual const RewriteOptions::OptionEnum* RelatedOptions( 00178 int* num_options) const; 00179 00180 protected: 00181 virtual const UrlSegmentEncoder* encoder() const; 00182 00183 virtual RewriteContext* MakeRewriteContext(); 00184 00185 private: 00186 class Context; 00187 friend class Context; 00188 00190 const ContentType* ImageToContentType(const GoogleString& origin_url, 00191 Image* image); 00192 void BeginRewriteImageUrl(HtmlElement* element, HtmlElement::Attribute* src); 00193 00194 RewriteResult RewriteLoadedResourceImpl(Context* context, 00195 const ResourcePtr& input_resource, 00196 const OutputResourcePtr& result); 00197 00199 bool FinishRewriteCssImageUrl( 00200 int64 css_image_inline_max_bytes, 00201 const CachedResult* cached, ResourceSlot* slot); 00202 00204 bool FinishRewriteImageUrl( 00205 const CachedResult* cached, const ResourceContext* resource_context, 00206 HtmlElement* element, HtmlElement::Attribute* src, int image_index, 00207 ResourceSlot* slot); 00208 00210 void SaveIfInlinable(const StringPiece& contents, 00211 const Image::Type image_type, 00212 CachedResult* cached); 00213 00216 void GetDimensions(HtmlElement* element, ImageDim* page_dim); 00217 00220 bool HasAnyDimensions(HtmlElement* element); 00221 00224 void ResizeLowQualityImage( 00225 Image* low_image, const ResourcePtr& input_resource, 00226 CachedResult* cached); 00227 00232 bool IsCriticalImage(const StringPiece& image_url) const; 00233 00237 bool StoreUrlInPropertyCache(const StringPiece& url); 00238 00239 bool SquashImagesForMobileScreenEnabled() const; 00240 00241 scoped_ptr<WorkBound> work_bound_; 00242 00244 00246 Variable* image_rewrites_; 00248 Variable* image_norewrites_high_resolution_; 00252 Variable* image_rewrites_dropped_intentionally_; 00254 Variable* image_rewrites_dropped_mime_type_unknown_; 00257 Variable* image_rewrites_dropped_server_write_fail_; 00260 Variable* image_rewrites_dropped_nosaving_resize_; 00263 Variable* image_rewrites_dropped_nosaving_noresize_; 00265 TimedVariable* image_rewrites_dropped_due_to_load_; 00269 TimedVariable* image_rewrites_squashing_for_mobile_screen_; 00273 Variable* image_rewrite_total_bytes_saved_; 00277 Variable* image_rewrite_total_original_bytes_; 00280 Variable* image_rewrite_uses_; 00282 Variable* image_inline_count_; 00284 Variable* image_webp_rewrites_; 00286 Histogram* image_rewrite_latency_ok_ms_; 00288 Histogram* image_rewrite_latency_failed_ms_; 00289 00290 ImageUrlEncoder encoder_; 00291 00294 int image_counter_; 00295 00299 StringSet inlinable_urls_; 00300 00301 DISALLOW_COPY_AND_ASSIGN(ImageRewriteFilter); 00302 }; 00303 00304 } 00305 00306 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_