Page Speed Optimization Libraries
1.7.30.2
|
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 00030 class DeviceProperties { 00031 public: 00032 explicit DeviceProperties(UserAgentMatcher* matcher); 00033 virtual ~DeviceProperties(); 00034 00035 void set_user_agent(const StringPiece& user_agent_string); 00036 bool SupportsImageInlining() const; 00037 bool SupportsLazyloadImages() const; 00038 bool SupportsCriticalImagesBeacon() const; 00039 bool SupportsJsDefer(bool enable_mobile) const; 00040 bool SupportsWebp() const; 00041 bool SupportsWebpLosslessAlpha() const; 00042 bool IsBot() const; 00043 bool SupportsSplitHtml(bool enable_mobile) const; 00044 bool CanPreloadResources() const; 00045 bool GetScreenResolution(int* width, int* height) const; 00046 UserAgentMatcher::DeviceType GetDeviceType() const; 00047 bool IsMobile() const { 00048 return GetDeviceType() == UserAgentMatcher::kMobile; 00049 } 00050 00051 enum ImageQualityPreference { 00053 kImageQualityDefault, 00055 kImageQualityLow, 00057 kImageQualityMedium, 00059 kImageQualityHigh, 00060 }; 00061 static const int kMediumScreenWidthThreshold = 720; 00062 static const int kLargeScreenWidthThreshold = 1500; 00063 00066 void SetPreferredImageQualities( 00067 const std::vector<int>* webp, const std::vector<int>* jpeg); 00069 bool GetPreferredImageQualities( 00070 ImageQualityPreference preference, int* webp, int* jpeg) const; 00071 static int GetPreferredImageQualityCount(); 00072 00073 private: 00074 friend class ImageRewriteTest; 00075 friend class RequestProperties; 00076 FRIEND_TEST(ImageRewriteTest, SquashImagesForMobileScreen); 00077 FRIEND_TEST(DevicePropertiesTest, GetScreenGroupIndex); 00078 00081 static bool GetScreenGroupIndex(int screen_width, int* screen_index); 00082 void SetScreenResolution(int width, int height) const; 00084 bool HasPreferredImageQualities() const; 00085 00086 GoogleString user_agent_; 00087 UserAgentMatcher* ua_matcher_; 00088 00089 mutable LazyBool supports_image_inlining_; 00090 mutable LazyBool supports_js_defer_; 00091 mutable LazyBool supports_lazyload_images_; 00092 mutable LazyBool supports_webp_; 00093 mutable LazyBool supports_webp_lossless_alpha_; 00094 mutable LazyBool is_bot_; 00095 mutable LazyBool is_mobile_user_agent_; 00096 mutable LazyBool supports_split_html_; 00097 mutable LazyBool supports_flush_early_; 00098 mutable LazyBool screen_dimensions_set_; 00099 mutable int screen_width_; 00100 mutable int screen_height_; 00101 const std::vector<int>* preferred_webp_qualities_; 00102 const std::vector<int>* preferred_jpeg_qualities_; 00104 mutable LazyBool device_type_set_; 00105 mutable UserAgentMatcher::DeviceType device_type_; 00106 00107 DISALLOW_COPY_AND_ASSIGN(DeviceProperties); 00108 }; 00109 00110 } 00111 00112 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_DEVICE_PROPERTIES_H_