2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: pulkitg@google.com (Pulkit Goyal)
19
// Contains implementation of DelayImagesFilter, which delays all the high
20
// quality images whose low quality inlined data url are available within their
21
// respective image tag like
22
// <img src="1.jpeg" pagespeed_low_res_src="data:base64...">.
24
// This filter extracts such low res data urls and generates a map from them.
25
// This map will be embedded inside HTML at the end of body tag with a script
26
// whose function is to put low res src into respective image tag. Another
27
// script which replaces low quality images with high quality images is also
30
// This filter will work in conjunction with image_rewrite_filter which
31
// generates data url for low quality images and embeds them with their
32
// respective img tags.
34
// To avoid drastic reflows, we also need to switch on insert_image_dimensions.
36
// Html input to this filter looks like:
41
// <img src="1.jpeg" pagespeed_low_res_src="data:base64..." />
45
// Above input html input looks like this because the image_rewrite_filter has
46
// already replaced <img src="1.jpeg" /> with
47
// <img src="1.jpeg" pagespeed_low_res_src="data:base64..." />.
49
// Output for the above html will be:
53
// Script code registers an onload event handler which replaces low res
54
// images with high res images.
58
// <img pagespeed_high_res_src="1.jpeg" />
60
// This block contains a map from url to their respective data urls and
61
// script which put these inline_src to their respective img tags.
66
// Bottom-of-page script actually includes the image data for the low-resolution
67
// images, and those are put in place as soon as control reaches there. High
68
// quality images are downloaded after all the low quality images are placed
71
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_
72
#define NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_
74
#include "net/instaweb/rewriter/public/common_filter.h"
75
#include "net/instaweb/util/public/basictypes.h"
76
#include "net/instaweb/util/public/string_util.h"
78
namespace net_instaweb {
82
class StaticAssetManager;
85
class DelayImagesFilter : public CommonFilter {
87
static const char kDelayImagesSuffix[];
88
static const char kDelayImagesInlineSuffix[];
89
static const char kImageOnloadCode[];
90
static const char kImageOnloadJsSnippet[];
92
explicit DelayImagesFilter(RewriteDriver* driver);
93
virtual ~DelayImagesFilter();
95
virtual void StartDocumentImpl();
96
virtual void StartElementImpl(HtmlElement* element) { }
97
virtual void EndElementImpl(HtmlElement* element);
99
virtual void EndDocument();
101
virtual const char* Name() const { return "DelayImages"; }
103
virtual void DetermineEnabled(GoogleString* disabled_reason);
105
static void InitStats(Statistics* statistics);
106
static void Terminate();
109
// Add the js snippet to be used for image-onload if it has not already been
111
void MaybeAddImageOnloadJsSnippet(HtmlElement* element);
113
// Insert low resolution images, and the script needed to load them if any.
114
void InsertLowResImagesAndJs(HtmlElement* element, bool insert_after_element);
116
// Insert the script to load the high resolution images.
117
void InsertHighResJs(HtmlElement* element);
119
// Returns a boolean indicating whether we should insert low resolution images
121
bool ShouldRewriteInplace() const;
123
RewriteDriver* driver_;
124
StaticAssetManager* static_asset_manager_;
126
int num_low_res_inlined_images_;
127
StringStringMap low_res_data_map_;
129
// Replace the image url with low res base64 encoded url inplace if it is
130
// true, else low_res_data_map_ containing low res images is inserted at the
132
bool insert_low_res_images_inplace_;
134
// lazyload_highres_images_ is set to true if lazyload_highres flag is true.
135
// It enables the feature that lazily loads the high res images after their
136
// low res versions are rendered. This flag is used especially in the case
138
bool lazyload_highres_images_;
140
bool is_script_inserted_;
142
bool added_image_onload_js_;
144
DISALLOW_COPY_AND_ASSIGN(DelayImagesFilter);
147
} // namespace net_instaweb
149
#endif // NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_