Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Friends | List of all members
net_instaweb::ImageRewriteFilter Class Reference

#include "image_rewrite_filter.h"

Inheritance diagram for net_instaweb::ImageRewriteFilter:
net_instaweb::RewriteFilter net_instaweb::CommonFilter net_instaweb::EmptyHtmlFilter net_instaweb::HtmlFilter

Public Types

typedef std::map< GoogleString,
AssociatedImageInfo > 
- Public Types inherited from net_instaweb::HtmlFilter
enum  ScriptUsage { kWillInjectScripts, kMayInjectScripts, kNeverInjectsScripts }
 Describes a filter's relationship with scripts. More...

Public Member Functions

 ImageRewriteFilter (RewriteDriver *driver)
virtual void StartDocumentImpl ()
virtual void EndDocument ()
virtual void RenderDone ()
virtual void StartElementImpl (HtmlElement *element)
virtual void EndElementImpl (HtmlElement *element)
virtual const char * Name () const
 The name of this filter – used for logging and debugging.
virtual const char * id () const
virtual void EncodeUserAgentIntoResourceContext (ResourceContext *context) const
void RegisterImageInfo (const AssociatedImageInfo &image_info)
InlineResult TryInline (bool is_html, bool is_critical, int64 image_inline_max_bytes, const CachedResult *cached_result, ResourceSlot *slot, GoogleString *data_url)
RewriteContextMakeNestedRewriteContextForCss (int64 css_image_inline_max_bytes, RewriteContext *parent, const ResourceSlotPtr &slot)
virtual RewriteContextMakeNestedRewriteContext (RewriteContext *parent, const ResourceSlotPtr &slot)
bool UpdateDesiredImageDimsIfNecessary (const ImageDim &image_dim, const ResourceContext &resource_context, ImageDim *desired_dim)
bool ShouldResize (const ResourceContext &context, const GoogleString &url, Image *image, ImageDim *desired_dimensions)
bool ResizeImageIfNecessary (const Context *rewrite_context, const GoogleString &url, ResourceContext *context, Image *image, CachedResult *cached)
Image::CompressionOptionsImageOptionsForLoadedResource (const ResourceContext &context, const ResourcePtr &input_resource)
virtual const
RelatedFilters (int *num_filters) const
virtual const StringPieceVector * RelatedOptions () const
void ReportDroppedRewrite ()
 Update stats for a rewrite dropped due to load.
- Public Member Functions inherited from net_instaweb::RewriteFilter
 RewriteFilter (RewriteDriver *driver)
virtual void DetermineEnabled (GoogleString *disabled_reason)
virtual bool CanModifyUrls ()
virtual bool ComputeOnTheFly () const
- Public Member Functions inherited from net_instaweb::CommonFilter
 CommonFilter (RewriteDriver *driver)
const GoogleUrlbase_url () const
 Getters. More...
const GoogleUrldecoded_base_url () const
RewriteDriverdriver () const
HtmlElementnoscript_element () const
void InsertNodeAtBodyEnd (HtmlNode *data)
virtual void StartDocument ()
 Note: Don't overload these methods, overload the implementers instead!
virtual void StartElement (HtmlElement *element)
virtual void EndElement (HtmlElement *element)
virtual void Characters (HtmlCharactersNode *characters)
ResourcePtr CreateInputResource (StringPiece input_url, RewriteDriver::InputRole role, bool *is_authorized)
ResourcePtr CreateInputResourceOrInsertDebugComment (StringPiece input_url, RewriteDriver::InputRole role, HtmlElement *element)
void ResolveUrl (StringPiece input_url, GoogleUrl *out_url)
bool IsRelativeUrlLoadPermittedByCsp (StringPiece url, CspDirective role)
bool BaseUrlIsValid () const
bool DebugMode () const
bool CanAddPagespeedOnloadToImage (const HtmlElement &)
virtual void LogFilterModifiedContent ()
AllowUnauthorizedDomain () const
virtual bool IntendedForInlining () const
void AddJsToElement (StringPiece js, HtmlElement *script)
- Public Member Functions inherited from net_instaweb::EmptyHtmlFilter
virtual void Cdata (HtmlCdataNode *cdata)
 Called for CDATA blocks (e.g. <![CDATA[foobar]]>)
virtual void Comment (HtmlCommentNode *comment)
 Called for HTML comments that aren't IE directives (e.g. ).
virtual void IEDirective (HtmlIEDirectiveNode *directive)
virtual void Directive (HtmlDirectiveNode *directive)
 Called for HTML directives (e.g. <!doctype foobar>).
virtual void Flush ()
ScriptUsage GetScriptUsage () const override
- Public Member Functions inherited from net_instaweb::HtmlFilter
bool is_enabled () const
void set_is_enabled (bool is_enabled)

Static Public Member Functions

static void InitStats (Statistics *statistics)
static void Initialize ()
static void Terminate ()
static void AddRelatedOptions (StringPieceVector *target)
static bool ExtractAssociatedImageInfo (const CachedResult *in, RewriteContext *context, AssociatedImageInfo *out)
static bool ParseDimensionAttribute (const char *position, int *value)
static void DisableRelatedFilters (RewriteOptions *options)
 Disable all filters listed in kRelatedFilters in options.
- Static Public Member Functions inherited from net_instaweb::RewriteFilter
static StringPiece GetCharsetForScript (const Resource *script, const StringPiece attribute_charset, const StringPiece enclosing_charset)
static GoogleString GetCharsetForStylesheet (const Resource *stylesheet, const StringPiece attribute_charset, const StringPiece enclosing_charset)
- Static Public Member Functions inherited from net_instaweb::CommonFilter
static bool ExtractMetaTagDetails (const HtmlElement &element, const ResponseHeaders *headers, GoogleString *content, GoogleString *mime_type, GoogleString *charset)

Static Public Attributes

static const char kImageNoRewritesHighResolution []
 Statistic names:
static const char kImageOngoingRewrites []
static const char kImageResizedUsingRenderedDimensions []
static const char kImageRewriteLatencyFailedMs []
static const char kImageRewriteLatencyOkMs []
static const char kImageRewriteLatencyTotalMs []
static const char kImageRewritesDroppedDecodeFailure []
static const char kImageRewritesDroppedDueToLoad []
static const char kImageRewritesDroppedMIMETypeUnknown []
static const char kImageRewritesDroppedNoSavingNoResize []
static const char kImageRewritesDroppedNoSavingResize []
static const char kImageRewritesDroppedServerWriteFail []
static const char kImageRewritesSquashingForMobileScreen []
static const char kImageRewrites []
static const char kImageWebpRewrites []
static const char kImageWebpFromGifFailureMs []
static const char kImageWebpFromGifSuccessMs []
static const char kImageWebpFromGifTimeouts []
static const char kImageWebpFromJpegFailureMs []
static const char kImageWebpFromJpegSuccessMs []
static const char kImageWebpFromJpegTimeouts []
static const char kImageWebpFromPngFailureMs []
static const char kImageWebpFromPngSuccessMs []
static const char kImageWebpFromPngTimeouts []
static const char kImageWebpOpaqueFailureMs []
static const char kImageWebpOpaqueSuccessMs []
static const char kImageWebpOpaqueTimeouts []
static const char kImageWebpWithAlphaFailureMs []
static const char kImageWebpWithAlphaSuccessMs []
static const char kImageWebpWithAlphaTimeouts []
static const char kImageWebpFromGifAnimatedFailureMs []
static const char kImageWebpFromGifAnimatedSuccessMs []
static const char kImageWebpFromGifAnimatedTimeouts []
static const char kInlinableImageUrlsPropertyName []
static const RewriteOptions::Filter kRelatedFilters []
static const int kRelatedFiltersSize
- Static Public Attributes inherited from net_instaweb::CommonFilter
static const char kCreateResourceFailedDebugMsg []
 Debug message to be inserted when resource creation fails.

Protected Member Functions

virtual const UrlSegmentEncoderencoder () const
virtual RewriteContextMakeRewriteContext ()
- Protected Member Functions inherited from net_instaweb::RewriteFilter
virtual const char * LoggingId ()
 This class logs using id().
- Protected Member Functions inherited from net_instaweb::CommonFilter
ServerContextserver_context () const
const RewriteOptionsrewrite_options ()


class Context

Detailed Description

Identify img tags in html and optimize them.

TODO(jmaessen): Big open question: how best to link pulled-in resources to rewritten urls, when in general those urls will be in a different domain.

Member Function Documentation

virtual const UrlSegmentEncoder* net_instaweb::ImageRewriteFilter::encoder ( ) const

All RewriteFilters define how they encode URLs and other associated information needed for a rewrite into a URL. The default implementation handles a single URL with no extra data. The filter owns the encoder.

Reimplemented from net_instaweb::RewriteFilter.

virtual void net_instaweb::ImageRewriteFilter::EncodeUserAgentIntoResourceContext ( ResourceContext *  context) const

Encodes user agent information needed by the filter into ResourceContext. See additional header document for RewriteContext::EncodeUserAgentIntoResourceContext.

Reimplemented from net_instaweb::RewriteFilter.

virtual void net_instaweb::ImageRewriteFilter::EndDocument ( )

Note: EndDocument will be called immediately before the last Flush call. (which also means that in the RewriteDriver use it is called before rendering for the last flush window).

Reimplemented from net_instaweb::EmptyHtmlFilter.

static bool net_instaweb::ImageRewriteFilter::ExtractAssociatedImageInfo ( const CachedResult *  in,
RewriteContext context,
AssociatedImageInfo *  out 

Tries to extract information stored by the image rewrite filter in *in, and to store it in *out. Returns whether successful.

The context is used to try to find a name for the image if it was not optimized.

Image::CompressionOptions* net_instaweb::ImageRewriteFilter::ImageOptionsForLoadedResource ( const ResourceContext &  context,
const ResourcePtr input_resource 

Allocate and initialize CompressionOptions object based on RewriteOptions and ResourceContext.

virtual RewriteContext* net_instaweb::ImageRewriteFilter::MakeNestedRewriteContext ( RewriteContext parent,
const ResourceSlotPtr slot 

Creates a nested rewrite for the given parent and slot and returns it. The result is not registered with the parent.

Reimplemented from net_instaweb::RewriteFilter.

RewriteContext* net_instaweb::ImageRewriteFilter::MakeNestedRewriteContextForCss ( int64  css_image_inline_max_bytes,
RewriteContext parent,
const ResourceSlotPtr slot 

Creates a nested rewrite for an image inside a CSS file with the given parent and slot, and returns it. The result is not registered with the parent.

virtual RewriteContext* net_instaweb::ImageRewriteFilter::MakeRewriteContext ( )

Generates a RewriteContext appropriate for this filter. Default implementation returns NULL. This must be overridden by filters. This is used to implement Fetch.

Reimplemented from net_instaweb::RewriteFilter.

static bool net_instaweb::ImageRewriteFilter::ParseDimensionAttribute ( const char *  position,
int *  value 

The valid contents of a dimension attribute on an image element have one of the following forms: "45%" "45%px" "+45.0%" [45% of browser width; we can't handle this] "45", "+45", "45px", "45arbitraryjunk" "45px%" [45 pixels regardless of junk] Technically 0 is an invalid dimension, so we'll reject those as well; note that 0 dimensions occur in the wild and Safari and Chrome at least don't display anything.

We actually reject the arbitraryjunk cases, as older browsers (eg FF9, which isn't that old) don't deal with them at all. So the only trailing stuff we allow is px possibly with some white space. Note that some older browsers (like FF9) accept other units such as "in" or "pt" as synonyms for px!

We round fractions, as fractional pixels appear to be rounded in practice (and our image resize algorithms require integer pixel sizes).

Far more detail in the spec at: common-microsyntaxes.html#percentages-and-dimensions

void net_instaweb::ImageRewriteFilter::RegisterImageInfo ( const AssociatedImageInfo &  image_info)

Registers image information associated with a URL, for use by experiment_collect_mob_image_info. Should be called from DOM-safe context: the parser thread or a Render() method.

virtual const RewriteOptions::Filter* net_instaweb::ImageRewriteFilter::RelatedFilters ( int *  num_filters) const

Determines which filters are related to this RewriteFilter. Note, for example, that the ImageRewriteFilter class implements lots of different RewriteOptions::Filters.

This is used for embedding the relevant enabled filter IDs. See the doc for RewriteOptions::add_options_to_urls_. We want to support that without bloating URLs excessively adding unrelated filter settings.

The vector is returned in numerically increasing order so binary_search is possible.

*num_filters is set to the size of this array.

Ownership of the filter-vector is not transferred to the caller; it is expected to return a pointer to a static vector.

Reimplemented from net_instaweb::RewriteFilter.

virtual const StringPieceVector* net_instaweb::ImageRewriteFilter::RelatedOptions ( ) const

Return the names of options related to this RewriteFilter in case-insensitive alphabetical order. NULL means there are none. Ownership of the vector is not transferred to the caller.

Reimplemented from net_instaweb::RewriteFilter.

virtual void net_instaweb::ImageRewriteFilter::RenderDone ( )

Notifies a filter that an asynchronous rewrite & render computation phase has finished. This is not used by HtmlParse itself, but only by RewriteDriver for pre-render filters. Happens after the corresponding flush, for every flush window. Default implementation does nothing.

TODO(morlovich): Push this down into CommonFilter and convert all the pre-render filters to inherit off it.

Reimplemented from net_instaweb::HtmlFilter.

bool net_instaweb::ImageRewriteFilter::ResizeImageIfNecessary ( const Context *  rewrite_context,
const GoogleString url,
ResourceContext *  context,
Image image,
CachedResult *  cached 

Resize image if necessary, returning true if this resizing succeeds and false if it's unnecessary or fails.

bool net_instaweb::ImageRewriteFilter::ShouldResize ( const ResourceContext &  context,
const GoogleString url,
Image image,
ImageDim *  desired_dimensions 

Determines whether an image should be resized based on the current options.

Returns the dimensions to resize to in *desired_dimensions.

virtual void net_instaweb::ImageRewriteFilter::StartDocumentImpl ( )

Overload these implementer methods: Intentionally left abstract so that implementers don't forget to change the name from Blah to BlahImpl.

Implements net_instaweb::CommonFilter.

InlineResult net_instaweb::ImageRewriteFilter::TryInline ( bool  is_html,
bool  is_critical,
int64  image_inline_max_bytes,
const CachedResult *  cached_result,
ResourceSlot slot,
GoogleString data_url 

Can we inline resource? If so, encode its contents into the data_url, otherwise leave data_url alone.

bool net_instaweb::ImageRewriteFilter::UpdateDesiredImageDimsIfNecessary ( const ImageDim &  image_dim,
const ResourceContext &  resource_context,
ImageDim *  desired_dim 

Update desired image dimensions if necessary. Returns true if it is updated.

Member Data Documentation

const char net_instaweb::ImageRewriteFilter::kInlinableImageUrlsPropertyName[]

The property cache property name used to store URLs discovered when image_inlining_identify_and_cache_without_rewriting() is set in the RewriteOptions.

The documentation for this class was generated from the following file: