Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image_url_encoder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 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  */
17 
18 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_URL_ENCODER_H_
19 #define NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_URL_ENCODER_H_
20 
21 #include "net/instaweb/rewriter/cached_result.pb.h"
22 #include "pagespeed/kernel/base/basictypes.h"
23 #include "pagespeed/kernel/base/gtest_prod.h"
24 #include "pagespeed/kernel/base/string.h"
25 #include "pagespeed/kernel/base/string_util.h"
26 #include "pagespeed/kernel/util/url_segment_encoder.h"
27 
28 namespace net_instaweb {
29 
30 class GoogleUrl;
31 class RequestProperties;
32 class RewriteDriver;
33 class RewriteOptions;
34 class MessageHandler;
35 
62 class ImageUrlEncoder : public UrlSegmentEncoder {
63  public:
68 
69  ImageUrlEncoder() {}
70  virtual ~ImageUrlEncoder();
71 
72  virtual void Encode(const StringVector& urls,
73  const ResourceContext* dim,
74  GoogleString* rewritten_url) const;
75 
76  virtual bool Decode(const StringPiece& url_segment,
77  StringVector* urls,
78  ResourceContext* dim,
79  MessageHandler* handler) const;
80 
85  static void SetLibWebpLevel(const RewriteOptions& options,
86  const RequestProperties& request_properties,
87  ResourceContext* resource_context);
88 
94  static void SetWebpAndMobileUserAgent(const RewriteDriver& driver,
95  ResourceContext* context);
96 
98  static bool IsWebpRewrittenUrl(const GoogleUrl& gurl);
99 
102  static void SetSmallScreen(const RewriteDriver& driver,
103  ResourceContext* context);
104 
106  static void SetUserAgentScreenResolution(
107  RewriteDriver* driver, ResourceContext* context);
108 
110  static GoogleString CacheKeyFromResourceContext(
111  const ResourceContext& resource_context);
112 
113  static bool HasDimensions(const ResourceContext& data) {
114  return (data.has_desired_image_dims() &&
115  HasValidDimensions(data.desired_image_dims()));
116  }
117 
118  static bool HasValidDimensions(const ImageDim& dims) {
119  return (dims.has_width() && dims.has_height());
120  }
121 
122  static bool HasDimension(const ResourceContext& data) {
123  return (data.has_desired_image_dims() &&
124  HasValidDimension(data.desired_image_dims()));
125  }
126 
127  static bool HasValidDimension(const ImageDim& dims) {
128  return (dims.has_width() || dims.has_height());
129  }
130 
131  private:
132  FRIEND_TEST(ImageRewriteTest, SquashImagesForMobileScreen);
133  FRIEND_TEST(ImageUrlEncoderTest, UserAgentScreenResolution);
134 
138  static bool GetNormalizedScreenResolution(
139  int screen_width, int screen_height, int* normalized_width,
140  int* normalized_height);
141 
142  DISALLOW_COPY_AND_ASSIGN(ImageUrlEncoder);
143 };
144 
145 }
146 
147 #endif
static GoogleString CacheKeyFromResourceContext(const ResourceContext &resource_context)
Helper function to generate Metadata cache key from ResourceContext.
static void SetSmallScreen(const RewriteDriver &driver, ResourceContext *context)
static void SetWebpAndMobileUserAgent(const RewriteDriver &driver, ResourceContext *context)
static void SetLibWebpLevel(const RewriteOptions &options, const RequestProperties &request_properties, ResourceContext *resource_context)
static void SetUserAgentScreenResolution(RewriteDriver *driver, ResourceContext *context)
Set context for screen resolution.
Definition: rewrite_driver.h:98
static bool IsWebpRewrittenUrl(const GoogleUrl &gurl)
Determines whether the given URL is a pagespeed-rewritten webp URL.
Definition: rewrite_options.h:83
static const int kSmallScreenSizeThresholdArea
Definition: image_url_encoder.h:67
Definition: request_properties.h:39
Definition: image_url_encoder.h:62