| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/peripheral_content_heuristic.h" | 5 #include "content/renderer/peripheral_content_heuristic.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "ui/gfx/geometry/size.h" | |
| 10 | |
| 11 namespace content { | 9 namespace content { |
| 12 | 10 |
| 13 namespace { | 11 namespace { |
| 14 | 12 |
| 15 // Content below this size in height and width is considered "tiny". | 13 // Content below this size in height and width is considered "tiny". |
| 16 // Tiny content is never peripheral, as tiny plugins often serve a critical | 14 // Tiny content is never peripheral, as tiny plugins often serve a critical |
| 17 // purpose, and the user often cannot find and click to unthrottle it. | 15 // purpose, and the user often cannot find and click to unthrottle it. |
| 18 const int kTinyContentSize = 5; | 16 const int kTinyContentSize = 5; |
| 19 | 17 |
| 20 // Cross-origin content must have a width and height both exceeding these | 18 // Cross-origin content must have a width and height both exceeding these |
| 21 // minimums to be considered "large", and thus not peripheral. | 19 // minimums to be considered "large", and thus not peripheral. |
| 22 const int kLargeContentMinWidth = 398; | 20 const int kLargeContentMinWidth = 398; |
| 23 const int kLargeContentMinHeight = 298; | 21 const int kLargeContentMinHeight = 298; |
| 24 | 22 |
| 25 // Mark some 16:9 aspect ratio content as essential (not peripheral). This is to | 23 // Mark some 16:9 aspect ratio content as essential (not peripheral). This is to |
| 26 // mark as "large" some medium sized video content that meets a minimum area | 24 // mark as "large" some medium sized video content that meets a minimum area |
| 27 // requirement, even if it is below the max width/height above. | 25 // requirement, even if it is below the max width/height above. |
| 28 const double kEssentialVideoAspectRatio = 16.0 / 9.0; | 26 const double kEssentialVideoAspectRatio = 16.0 / 9.0; |
| 29 const double kAspectRatioEpsilon = 0.01; | 27 const double kAspectRatioEpsilon = 0.01; |
| 30 const int kEssentialVideoMinimumArea = 120000; | 28 const int kEssentialVideoMinimumArea = 120000; |
| 31 | 29 |
| 32 } // namespace | 30 } // namespace |
| 33 | 31 |
| 34 // static | 32 // static |
| 35 RenderFrame::PeripheralContentStatus | 33 PeripheralContentHeuristic::Decision |
| 36 PeripheralContentHeuristic::GetPeripheralStatus( | 34 PeripheralContentHeuristic::GetPeripheralStatus( |
| 37 const std::set<url::Origin>& origin_whitelist, | 35 const std::set<url::Origin>& origin_whitelist, |
| 38 const url::Origin& main_frame_origin, | 36 const url::Origin& main_frame_origin, |
| 39 const url::Origin& content_origin, | 37 const url::Origin& content_origin, |
| 40 const gfx::Size& unobscured_size) { | 38 int width, |
| 39 int height) { |
| 41 if (main_frame_origin.IsSameOriginWith(content_origin)) | 40 if (main_frame_origin.IsSameOriginWith(content_origin)) |
| 42 return RenderFrame::CONTENT_STATUS_ESSENTIAL_SAME_ORIGIN; | 41 return HEURISTIC_DECISION_ESSENTIAL_SAME_ORIGIN; |
| 42 |
| 43 if (width <= 0 || height <= 0) |
| 44 return HEURISTIC_DECISION_ESSENTIAL_UNKNOWN_SIZE; |
| 43 | 45 |
| 44 if (origin_whitelist.count(content_origin)) | 46 if (origin_whitelist.count(content_origin)) |
| 45 return RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED; | 47 return HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_WHITELISTED; |
| 46 | 48 |
| 47 if (unobscured_size.IsEmpty()) | 49 if (width <= kTinyContentSize && height <= kTinyContentSize) |
| 48 return RenderFrame::CONTENT_STATUS_ESSENTIAL_UNKNOWN_SIZE; | 50 return HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_TINY; |
| 49 | 51 |
| 50 if (unobscured_size.width() <= kTinyContentSize && | 52 if (IsLargeContent(width, height)) |
| 51 unobscured_size.height() <= kTinyContentSize) { | 53 return HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_BIG; |
| 52 return RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_TINY; | |
| 53 } | |
| 54 | 54 |
| 55 if (IsLargeContent(unobscured_size)) | 55 return HEURISTIC_DECISION_PERIPHERAL; |
| 56 return RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG; | |
| 57 | |
| 58 return RenderFrame::CONTENT_STATUS_PERIPHERAL; | |
| 59 } | 56 } |
| 60 | 57 |
| 61 // static | 58 // static |
| 62 bool PeripheralContentHeuristic::IsLargeContent( | 59 bool PeripheralContentHeuristic::IsLargeContent(int width, int height) { |
| 63 const gfx::Size& unobscured_size) { | |
| 64 int width = unobscured_size.width(); | |
| 65 int height = unobscured_size.height(); | |
| 66 if (width >= kLargeContentMinWidth && height >= kLargeContentMinHeight) | 60 if (width >= kLargeContentMinWidth && height >= kLargeContentMinHeight) |
| 67 return true; | 61 return true; |
| 68 | 62 |
| 69 double aspect_ratio = static_cast<double>(width) / height; | 63 double aspect_ratio = static_cast<double>(width) / height; |
| 70 if (std::abs(aspect_ratio - kEssentialVideoAspectRatio) < | 64 if (std::abs(aspect_ratio - kEssentialVideoAspectRatio) < |
| 71 kAspectRatioEpsilon && | 65 kAspectRatioEpsilon && |
| 72 width * height >= kEssentialVideoMinimumArea) { | 66 width * height >= kEssentialVideoMinimumArea) { |
| 73 return true; | 67 return true; |
| 74 } | 68 } |
| 75 | 69 |
| 76 return false; | 70 return false; |
| 77 } | 71 } |
| 78 | 72 |
| 79 } // namespace content | 73 } // namespace content |
| OLD | NEW |