Page Speed Optimization Libraries  1.8.31.2
net/instaweb/rewriter/public/device_properties.h
00001 // Copyright 2012 Google Inc. All Rights Reserved.
00014 
00015 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_DEVICE_PROPERTIES_H_
00016 #define NET_INSTAWEB_REWRITER_PUBLIC_DEVICE_PROPERTIES_H_
00017 
00018 #include <vector>
00019 
00020 #include "net/instaweb/http/public/user_agent_matcher.h"
00021 #include "net/instaweb/util/public/basictypes.h"
00022 #include "net/instaweb/util/public/gtest_prod.h"
00023 #include "net/instaweb/util/public/string.h"
00024 #include "net/instaweb/util/public/string_util.h"
00025 
00026 namespace net_instaweb {
00027 
00028 class RequestHeaders;
00029 
00032 class DeviceProperties {
00033  public:
00034   explicit DeviceProperties(UserAgentMatcher* matcher);
00035   virtual ~DeviceProperties();
00036 
00037   void SetUserAgent(const StringPiece& user_agent_string);
00040   void ParseRequestHeaders(const RequestHeaders& request_headers);
00041   bool SupportsImageInlining() const;
00042   bool SupportsLazyloadImages() const;
00043   bool SupportsCriticalCss() const;
00044   bool SupportsCriticalImagesBeacon() const;
00045   bool SupportsJsDefer(bool enable_mobile) const;
00049   bool SupportsWebpInPlace() const;
00056   bool SupportsWebpRewrittenUrls() const;
00057   bool SupportsWebpLosslessAlpha() const;
00058   bool IsBot() const;
00059   bool SupportsSplitHtml(bool enable_mobile) const;
00060   bool CanPreloadResources() const;
00061   bool GetScreenResolution(int* width, int* height) const;
00062   UserAgentMatcher::DeviceType GetDeviceType() const;
00063   bool IsMobile() const {
00064     return GetDeviceType() == UserAgentMatcher::kMobile;
00065   }
00066 
00067   enum ImageQualityPreference {
00069     kImageQualityDefault,
00071     kImageQualityLow,
00073     kImageQualityMedium,
00075     kImageQualityHigh,
00076   };
00077   static const int kMediumScreenWidthThreshold = 720;
00078   static const int kLargeScreenWidthThreshold = 1500;
00079 
00082   void SetPreferredImageQualities(
00083       const std::vector<int>* webp,  const std::vector<int>* jpeg);
00085   bool GetPreferredImageQualities(
00086       ImageQualityPreference preference, int* webp, int* jpeg) const;
00087   static int GetPreferredImageQualityCount();
00088 
00089  private:
00090   friend class ImageRewriteTest;
00091   friend class RequestProperties;
00092   FRIEND_TEST(ImageRewriteTest, SquashImagesForMobileScreen);
00093   FRIEND_TEST(DevicePropertiesTest, GetScreenGroupIndex);
00094 
00097   static bool GetScreenGroupIndex(int screen_width, int* screen_index);
00098   void SetScreenResolution(int width, int height) const;
00100   bool HasPreferredImageQualities() const;
00101 
00102   GoogleString user_agent_;
00103   GoogleString accept_header_;
00104   UserAgentMatcher* ua_matcher_;
00105 
00106   mutable LazyBool supports_critical_css_;
00107   mutable LazyBool supports_image_inlining_;
00108   mutable LazyBool supports_js_defer_;
00109   mutable LazyBool supports_lazyload_images_;
00110   mutable LazyBool accepts_webp_;
00111   mutable LazyBool supports_webp_rewritten_urls_;
00112   mutable LazyBool supports_webp_lossless_alpha_;
00113   mutable LazyBool is_bot_;
00114   mutable LazyBool is_mobile_user_agent_;
00115   mutable LazyBool supports_split_html_;
00116   mutable LazyBool supports_flush_early_;
00117   mutable LazyBool screen_dimensions_set_;
00118   mutable int screen_width_;
00119   mutable int screen_height_;
00120   const std::vector<int>* preferred_webp_qualities_;
00121   const std::vector<int>* preferred_jpeg_qualities_;
00123   mutable LazyBool device_type_set_;
00124   mutable UserAgentMatcher::DeviceType device_type_;
00125 
00126   DISALLOW_COPY_AND_ASSIGN(DeviceProperties);
00127 };
00128 
00129 }  
00130 
00131 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_DEVICE_PROPERTIES_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines