~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/rewriter/public/delay_images_filter.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: pulkitg@google.com (Pulkit Goyal)
18
 
//
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...">.
23
 
//
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
28
 
// embedded.
29
 
//
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.
33
 
//
34
 
// To avoid drastic reflows, we also need to switch on insert_image_dimensions.
35
 
//
36
 
// Html input to this filter looks like:
37
 
// <html>
38
 
//  <head>
39
 
//  </head>
40
 
//  <body>
41
 
//   <img src="1.jpeg" pagespeed_low_res_src="data:base64..." />
42
 
//  </body>
43
 
// </html>
44
 
//
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..." />.
48
 
//
49
 
// Output for the above html will be:
50
 
// <html>
51
 
//  <head>
52
 
//   <script>
53
 
//    Script code registers an onload event handler which replaces low res
54
 
//    images with high res images.
55
 
//   </script>
56
 
//  </head>
57
 
//  <body>
58
 
//   <img pagespeed_high_res_src="1.jpeg" />
59
 
//   <script>
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.
62
 
//   </script>
63
 
//  </body>
64
 
// </html>
65
 
//
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
69
 
// by delay script.
70
 
 
71
 
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_
72
 
#define NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_
73
 
 
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"
77
 
 
78
 
namespace net_instaweb {
79
 
 
80
 
class HtmlElement;
81
 
class RewriteDriver;
82
 
class StaticAssetManager;
83
 
class Statistics;
84
 
 
85
 
class DelayImagesFilter : public CommonFilter {
86
 
 public:
87
 
  static const char kDelayImagesSuffix[];
88
 
  static const char kDelayImagesInlineSuffix[];
89
 
  static const char kImageOnloadCode[];
90
 
  static const char kImageOnloadJsSnippet[];
91
 
 
92
 
  explicit DelayImagesFilter(RewriteDriver* driver);
93
 
  virtual ~DelayImagesFilter();
94
 
 
95
 
  virtual void StartDocumentImpl();
96
 
  virtual void StartElementImpl(HtmlElement* element) { }
97
 
  virtual void EndElementImpl(HtmlElement* element);
98
 
 
99
 
  virtual void EndDocument();
100
 
 
101
 
  virtual const char* Name() const { return "DelayImages"; }
102
 
 
103
 
  virtual void DetermineEnabled(GoogleString* disabled_reason);
104
 
 
105
 
  static void InitStats(Statistics* statistics);
106
 
  static void Terminate();
107
 
 
108
 
 private:
109
 
  // Add the js snippet to be used for image-onload if it has not already been
110
 
  // added.
111
 
  void MaybeAddImageOnloadJsSnippet(HtmlElement* element);
112
 
 
113
 
  // Insert low resolution images, and the script needed to load them if any.
114
 
  void InsertLowResImagesAndJs(HtmlElement* element, bool insert_after_element);
115
 
 
116
 
  // Insert the script to load the high resolution images.
117
 
  void InsertHighResJs(HtmlElement* element);
118
 
 
119
 
  // Returns a boolean indicating whether we should insert low resolution images
120
 
  // in place.
121
 
  bool ShouldRewriteInplace() const;
122
 
 
123
 
  RewriteDriver* driver_;
124
 
  StaticAssetManager* static_asset_manager_;
125
 
 
126
 
  int num_low_res_inlined_images_;
127
 
  StringStringMap low_res_data_map_;
128
 
 
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
131
 
  // end of body tag.
132
 
  bool insert_low_res_images_inplace_;
133
 
 
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
137
 
  // of mobile.
138
 
  bool lazyload_highres_images_;
139
 
 
140
 
  bool is_script_inserted_;
141
 
 
142
 
  bool added_image_onload_js_;
143
 
 
144
 
  DISALLOW_COPY_AND_ASSIGN(DelayImagesFilter);
145
 
};
146
 
 
147
 
}  // namespace net_instaweb
148
 
 
149
 
#endif  // NET_INSTAWEB_REWRITER_PUBLIC_DELAY_IMAGES_FILTER_H_