Page Speed Optimization Libraries  1.6.29.3
net/instaweb/rewriter/public/image_rewrite_filter.h
Go to the documentation of this file.
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 <map>
00023 #include <utility>
00024 
00025 #include "net/instaweb/htmlparse/public/html_element.h"
00026 #include "net/instaweb/rewriter/image_types.pb.h"
00027 #include "net/instaweb/rewriter/public/image.h"
00028 #include "net/instaweb/rewriter/public/image_url_encoder.h"
00029 #include "net/instaweb/rewriter/public/resource.h"
00030 #include "net/instaweb/rewriter/public/server_context.h"
00031 #include "net/instaweb/rewriter/public/resource_slot.h"
00032 #include "net/instaweb/rewriter/public/rewrite_filter.h"
00033 #include "net/instaweb/rewriter/public/rewrite_options.h"
00034 #include "net/instaweb/rewriter/public/rewrite_result.h"
00035 #include "net/instaweb/util/public/basictypes.h"
00036 #include "net/instaweb/util/public/scoped_ptr.h"
00037 #include "net/instaweb/util/public/string.h"
00038 #include "net/instaweb/util/public/string_util.h"
00039 
00040 namespace net_instaweb {
00041 
00042 class CachedResult;
00043 class ImageDim;
00044 class Histogram;
00045 class RenderedImages;
00046 class ResourceContext;
00047 class RewriteContext;
00048 class RewriteDriver;
00049 class Statistics;
00050 class TimedVariable;
00051 class UrlSegmentEncoder;
00052 class Variable;
00053 class WorkBound;
00054 struct ContentType;
00055 
00056 typedef std::map<GoogleString, std::pair<int32, int32> >
00057     RenderedImageDimensionsMap;
00058 
00062 class ImageRewriteFilter : public RewriteFilter {
00063  public:
00065   static const char kImageNoRewritesHighResolution[];
00066   static const char kImageOngoingRewrites[];
00067   static const char kImageResizedUsingRenderedDimensions[];
00068   static const char kImageRewriteLatencyFailedMs[];
00069   static const char kImageRewriteLatencyOkMs[];
00070   static const char kImageRewritesDroppedDecodeFailure[];
00071   static const char kImageRewritesDroppedDueToLoad[];
00072   static const char kImageRewritesDroppedMIMETypeUnknown[];
00073   static const char kImageRewritesDroppedNoSavingNoResize[];
00074   static const char kImageRewritesDroppedNoSavingResize[];
00075   static const char kImageRewritesDroppedServerWriteFail[];
00076   static const char kImageRewritesSquashingForMobileScreen[];
00077   static const char kImageRewrites[];
00078   static const char kImageWebpFromGifFailureMs[];
00079   static const char kImageWebpFromGifSuccessMs[];
00080   static const char kImageWebpFromGifTimeouts[];
00081   static const char kImageWebpFromJpegFailureMs[];
00082   static const char kImageWebpFromJpegSuccessMs[];
00083   static const char kImageWebpFromJpegTimeouts[];
00084   static const char kImageWebpFromPngFailureMs[];
00085   static const char kImageWebpFromPngSuccessMs[];
00086   static const char kImageWebpFromPngTimeouts[];
00087   static const char kImageWebpOpaqueFailureMs[];
00088   static const char kImageWebpOpaqueSuccessMs[];
00089   static const char kImageWebpOpaqueTimeouts[];
00090   static const char kImageWebpWithAlphaFailureMs[];
00091   static const char kImageWebpWithAlphaSuccessMs[];
00092   static const char kImageWebpWithAlphaTimeouts[];
00093 
00097   static const char kInlinableImageUrlsPropertyName[];
00098 
00099   static const RewriteOptions::Filter kRelatedFilters[];
00100   static const int kRelatedFiltersSize;
00101 
00102   explicit ImageRewriteFilter(RewriteDriver* driver);
00103   virtual ~ImageRewriteFilter();
00104   static void InitStats(Statistics* statistics);
00105   static void Initialize();
00106   static void Terminate();
00107   static void AddRelatedOptions(StringPieceVector* target);
00108   virtual void StartDocumentImpl();
00109   virtual void StartElementImpl(HtmlElement* element) {}
00110   virtual void EndElementImpl(HtmlElement* element);
00111   virtual const char* Name() const { return "ImageRewrite"; }
00112   virtual const char* id() const { return RewriteOptions::kImageCompressionId; }
00113   virtual void EncodeUserAgentIntoResourceContext(
00114       ResourceContext* context) const;
00115 
00118   bool TryInline(
00119       int64 image_inline_max_bytes, const CachedResult* cached_result,
00120       ResourceSlot* slot, GoogleString* data_url);
00121 
00123   void SetupRenderedImageDimensionsMap(
00124       const RenderedImages& rendered_images);
00125 
00145   static bool ParseDimensionAttribute(const char* position, int* value);
00146 
00150   RewriteContext* MakeNestedRewriteContextForCss(
00151       int64 css_image_inline_max_bytes,
00152       RewriteContext* parent,
00153       const ResourceSlotPtr& slot);
00154 
00157   virtual RewriteContext* MakeNestedRewriteContext(RewriteContext* parent,
00158                                                    const ResourceSlotPtr& slot);
00159 
00162   bool UpdateDesiredImageDimsIfNecessary(
00163       const ImageDim& image_dim, const ResourceContext& resource_context,
00164       ImageDim* desired_dim);
00165 
00169   bool ShouldResize(const ResourceContext& context,
00170                     const GoogleString& url,
00171                     Image* image,
00172                     ImageDim* desired_dimensions);
00173 
00176   bool ResizeImageIfNecessary(
00177       const RewriteContext* rewrite_context, const GoogleString& url,
00178       ResourceContext* context, Image* image, CachedResult* cached);
00179 
00182   Image::CompressionOptions* ImageOptionsForLoadedResource(
00183       const ResourceContext& context, const ResourcePtr& input_resource,
00184       bool is_css);
00185 
00186   virtual const RewriteOptions::Filter* RelatedFilters(int* num_filters) const;
00187   virtual const StringPieceVector* RelatedOptions() const {
00188     return related_options_;
00189   }
00190 
00192   static void DisableRelatedFilters(RewriteOptions* options);
00193 
00194  protected:
00195   virtual const UrlSegmentEncoder* encoder() const;
00196 
00197   virtual RewriteContext* MakeRewriteContext();
00198 
00199  private:
00200   class Context;
00201   friend class Context;
00202 
00204   const ContentType* ImageToContentType(const GoogleString& origin_url,
00205                                         Image* image);
00206   void BeginRewriteImageUrl(HtmlElement* element, HtmlElement::Attribute* src);
00207 
00208   RewriteResult RewriteLoadedResourceImpl(Context* context,
00209                                           const ResourcePtr& input_resource,
00210                                           const OutputResourcePtr& result);
00211 
00213   bool FinishRewriteCssImageUrl(
00214       int64 css_image_inline_max_bytes,
00215       const CachedResult* cached, ResourceSlot* slot);
00216 
00218   bool FinishRewriteImageUrl(
00219       const CachedResult* cached, const ResourceContext* resource_context,
00220       HtmlElement* element, HtmlElement::Attribute* src, int image_index,
00221       ResourceSlot* slot);
00222 
00224   void SaveIfInlinable(const StringPiece& contents,
00225                        const ImageType image_type,
00226                        CachedResult* cached);
00227 
00232   void GetDimensions(HtmlElement* element, ImageDim* page_dim,
00233                      const HtmlElement::Attribute* src,
00234                      bool* is_resized_using_rendered_dimensions);
00235 
00238   bool HasAnyDimensions(HtmlElement* element);
00239 
00242   void ResizeLowQualityImage(
00243       Image* low_image, const ResourcePtr& input_resource,
00244       CachedResult* cached);
00245 
00250   bool IsHtmlCriticalImage(StringPiece image_url) const;
00251 
00255   bool StoreUrlInPropertyCache(const StringPiece& url);
00256 
00257   bool SquashImagesForMobileScreenEnabled() const;
00258 
00259   scoped_ptr<WorkBound> work_bound_;
00260 
00262 
00264   scoped_ptr<RenderedImageDimensionsMap> rendered_images_map_;
00266   Variable* image_rewrites_;
00268   Variable* image_resized_using_rendered_dimensions_;
00270   Variable* image_norewrites_high_resolution_;
00275   Variable* image_rewrites_dropped_intentionally_;
00277   Variable* image_rewrites_dropped_decode_failure_;
00279   Variable* image_rewrites_dropped_mime_type_unknown_;
00282   Variable* image_rewrites_dropped_server_write_fail_;
00285   Variable* image_rewrites_dropped_nosaving_resize_;
00288   Variable* image_rewrites_dropped_nosaving_noresize_;
00290   TimedVariable* image_rewrites_dropped_due_to_load_;
00294   TimedVariable* image_rewrites_squashing_for_mobile_screen_;
00298   Variable* image_rewrite_total_bytes_saved_;
00302   Variable* image_rewrite_total_original_bytes_;
00305   Variable* image_rewrite_uses_;
00307   Variable* image_inline_count_;
00309   Variable* image_webp_rewrites_;
00310 
00312   Histogram* image_rewrite_latency_ok_ms_;
00314   Histogram* image_rewrite_latency_failed_ms_;
00315 
00316   ImageUrlEncoder encoder_;
00317 
00320   int image_counter_;
00321 
00325   StringSet inlinable_urls_;
00326 
00328   Image::ConversionVariables webp_conversion_variables_;
00329 
00331   static StringPieceVector* related_options_;
00332 
00333   DISALLOW_COPY_AND_ASSIGN(ImageRewriteFilter);
00334 };
00335 
00336 }  
00337 
00338 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_REWRITE_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines