OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/web/public/web_state/page_display_state.h" | 5 #import "ios/web/public/web_state/page_display_state.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
10 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
11 #endif | 11 #endif |
12 | 12 |
13 namespace web { | 13 namespace web { |
14 | 14 |
15 namespace { | 15 namespace { |
| 16 // Serialiation keys. |
| 17 NSString* const kXOffsetKey = @"scrollX"; |
| 18 NSString* const kYOffsetKey = @"scrollY"; |
| 19 NSString* const kMinZoomKey = @"minZoom"; |
| 20 NSString* const kMaxZoomKey = @"maxZoom"; |
| 21 NSString* const kZoomKey = @"zoom"; |
16 // Returns true if: | 22 // Returns true if: |
17 // - both |value1| and |value2| are NAN, or | 23 // - both |value1| and |value2| are NAN, or |
18 // - |value1| and |value2| are equal non-NAN values. | 24 // - |value1| and |value2| are equal non-NAN values. |
19 inline bool StateValuesAreEqual(double value1, double value2) { | 25 inline bool StateValuesAreEqual(double value1, double value2) { |
20 return std::isnan(value1) ? std::isnan(value2) : value1 == value2; | 26 return std::isnan(value1) ? std::isnan(value2) : value1 == value2; |
21 } | 27 } |
| 28 // Returns the double stored under |key| in |serialization|, or NAN if it is not |
| 29 // set. |
| 30 inline double GetValue(NSString* key, NSDictionary* serialization) { |
| 31 NSNumber* value = serialization[key]; |
| 32 return value ? [value doubleValue] : NAN; |
| 33 } |
22 } // namespace | 34 } // namespace |
23 | 35 |
24 PageScrollState::PageScrollState() : offset_x_(NAN), offset_y_(NAN) { | 36 PageScrollState::PageScrollState() : offset_x_(NAN), offset_y_(NAN) { |
25 } | 37 } |
26 | 38 |
27 PageScrollState::PageScrollState(double offset_x, double offset_y) | 39 PageScrollState::PageScrollState(double offset_x, double offset_y) |
28 : offset_x_(offset_x), offset_y_(offset_y) { | 40 : offset_x_(offset_x), offset_y_(offset_y) { |
29 } | 41 } |
30 | 42 |
31 PageScrollState::~PageScrollState() { | 43 PageScrollState::~PageScrollState() { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 98 |
87 PageDisplayState::PageDisplayState(double offset_x, | 99 PageDisplayState::PageDisplayState(double offset_x, |
88 double offset_y, | 100 double offset_y, |
89 double minimum_zoom_scale, | 101 double minimum_zoom_scale, |
90 double maximum_zoom_scale, | 102 double maximum_zoom_scale, |
91 double zoom_scale) | 103 double zoom_scale) |
92 : scroll_state_(offset_x, offset_y), | 104 : scroll_state_(offset_x, offset_y), |
93 zoom_state_(minimum_zoom_scale, maximum_zoom_scale, zoom_scale) { | 105 zoom_state_(minimum_zoom_scale, maximum_zoom_scale, zoom_scale) { |
94 } | 106 } |
95 | 107 |
| 108 PageDisplayState::PageDisplayState(NSDictionary* serialization) |
| 109 : PageDisplayState(GetValue(kXOffsetKey, serialization), |
| 110 GetValue(kYOffsetKey, serialization), |
| 111 GetValue(kMinZoomKey, serialization), |
| 112 GetValue(kMaxZoomKey, serialization), |
| 113 GetValue(kZoomKey, serialization)) {} |
| 114 |
96 PageDisplayState::~PageDisplayState() { | 115 PageDisplayState::~PageDisplayState() { |
97 } | 116 } |
98 | 117 |
99 bool PageDisplayState::IsValid() const { | 118 bool PageDisplayState::IsValid() const { |
100 return scroll_state_.IsValid() && zoom_state_.IsValid(); | 119 return scroll_state_.IsValid() && zoom_state_.IsValid(); |
101 } | 120 } |
102 | 121 |
103 bool PageDisplayState::operator==(const PageDisplayState& other) const { | 122 bool PageDisplayState::operator==(const PageDisplayState& other) const { |
104 return scroll_state_ == other.scroll_state_ && | 123 return scroll_state_ == other.scroll_state_ && |
105 zoom_state_ == other.zoom_state_; | 124 zoom_state_ == other.zoom_state_; |
106 } | 125 } |
107 | 126 |
108 bool PageDisplayState::operator!=(const PageDisplayState& other) const { | 127 bool PageDisplayState::operator!=(const PageDisplayState& other) const { |
109 return !(*this == other); | 128 return !(*this == other); |
110 } | 129 } |
111 | 130 |
| 131 NSDictionary* PageDisplayState::GetSerialization() const { |
| 132 return @{ |
| 133 kXOffsetKey : @(scroll_state_.offset_x()), |
| 134 kYOffsetKey : @(scroll_state_.offset_y()), |
| 135 kMinZoomKey : @(zoom_state_.minimum_zoom_scale()), |
| 136 kMaxZoomKey : @(zoom_state_.maximum_zoom_scale()), |
| 137 kZoomKey : @(zoom_state_.zoom_scale()) |
| 138 }; |
| 139 } |
| 140 |
| 141 NSString* PageDisplayState::GetDescription() const { |
| 142 NSString* const kPageScrollStateDescriptionFormat = |
| 143 @"{ scrollOffset:(%0.2f, %0.2f), zoomScaleRange:(%0.2f, %0.2f), " |
| 144 @"zoomScale:%0.2f }"; |
| 145 return [NSString stringWithFormat:kPageScrollStateDescriptionFormat, |
| 146 scroll_state_.offset_x(), |
| 147 scroll_state_.offset_y(), |
| 148 zoom_state_.minimum_zoom_scale(), |
| 149 zoom_state_.maximum_zoom_scale(), |
| 150 zoom_state_.zoom_scale()]; |
| 151 } |
| 152 |
112 } // namespace web | 153 } // namespace web |
OLD | NEW |