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 #include "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 // Returns true if: | 16 // Returns true if: |
17 // - both |value1| and |value2| are NAN, or | 17 // - both |value1| and |value2| are NAN, or |
18 // - |value1| and |value2| are equal non-NAN values. | 18 // - |value1| and |value2| are equal non-NAN values. |
19 inline bool StateValuesAreEqual(double value1, double value2) { | 19 inline bool StateValuesAreEqual(double value1, double value2) { |
20 return std::isnan(value1) ? std::isnan(value2) : value1 == value2; | 20 return std::isnan(value1) ? std::isnan(value2) : value1 == value2; |
21 } | 21 } |
| 22 // Returns the double stored under |key| in |serialization|, or NAN if it is not |
| 23 // set. |
| 24 inline double GetValue(NSString* key, NSDictionary* serialization) { |
| 25 NSNumber* value = serialization[key]; |
| 26 return value ? [value doubleValue] : NAN; |
| 27 } |
22 } // namespace | 28 } // namespace |
23 | 29 |
24 PageScrollState::PageScrollState() : offset_x_(NAN), offset_y_(NAN) { | 30 PageScrollState::PageScrollState() : offset_x_(NAN), offset_y_(NAN) { |
25 } | 31 } |
26 | 32 |
27 PageScrollState::PageScrollState(double offset_x, double offset_y) | 33 PageScrollState::PageScrollState(double offset_x, double offset_y) |
28 : offset_x_(offset_x), offset_y_(offset_y) { | 34 : offset_x_(offset_x), offset_y_(offset_y) { |
29 } | 35 } |
30 | 36 |
31 PageScrollState::~PageScrollState() { | 37 PageScrollState::~PageScrollState() { |
32 } | 38 } |
33 | 39 |
34 bool PageScrollState::IsValid() const { | 40 bool PageScrollState::IsValid() const { |
35 return !std::isnan(offset_x_) && !std::isnan(offset_y_); | 41 return !std::isnan(offset_x_) && !std::isnan(offset_y_); |
36 } | 42 } |
37 | 43 |
38 bool PageScrollState::operator==(const PageScrollState& other) const { | 44 bool PageScrollState::operator==(const PageScrollState& other) const { |
39 return StateValuesAreEqual(offset_x_, other.offset_x_) && | 45 return StateValuesAreEqual(offset_x_, other.offset_x_) && |
40 StateValuesAreEqual(offset_y_, other.offset_y_); | 46 StateValuesAreEqual(offset_y_, other.offset_y_); |
41 } | 47 } |
42 | 48 |
43 bool PageScrollState::operator!=(const PageScrollState& other) const { | 49 bool PageScrollState::operator!=(const PageScrollState& other) const { |
44 return !(*this == other); | 50 return !(*this == other); |
45 } | 51 } |
46 | 52 |
| 53 NSString* PageScrollState::XOffsetKey() { |
| 54 static NSString* const kXOffsetKey = @"scrollX"; |
| 55 return kXOffsetKey; |
| 56 } |
| 57 |
| 58 NSString* PageScrollState::YOffsetKey() { |
| 59 static NSString* const kYOffsetKey = @"scrollY"; |
| 60 return kYOffsetKey; |
| 61 } |
| 62 |
47 PageZoomState::PageZoomState() | 63 PageZoomState::PageZoomState() |
48 : minimum_zoom_scale_(NAN), maximum_zoom_scale_(NAN), zoom_scale_(NAN) { | 64 : minimum_zoom_scale_(NAN), maximum_zoom_scale_(NAN), zoom_scale_(NAN) { |
49 } | 65 } |
50 | 66 |
51 PageZoomState::PageZoomState(double minimum_zoom_scale, | 67 PageZoomState::PageZoomState(double minimum_zoom_scale, |
52 double maximum_zoom_scale, | 68 double maximum_zoom_scale, |
53 double zoom_scale) | 69 double zoom_scale) |
54 : minimum_zoom_scale_(minimum_zoom_scale), | 70 : minimum_zoom_scale_(minimum_zoom_scale), |
55 maximum_zoom_scale_(maximum_zoom_scale), | 71 maximum_zoom_scale_(maximum_zoom_scale), |
56 zoom_scale_(zoom_scale) { | 72 zoom_scale_(zoom_scale) { |
(...skipping 12 matching lines...) Expand all Loading... |
69 bool PageZoomState::operator==(const PageZoomState& other) const { | 85 bool PageZoomState::operator==(const PageZoomState& other) const { |
70 return StateValuesAreEqual(minimum_zoom_scale_, other.minimum_zoom_scale_) && | 86 return StateValuesAreEqual(minimum_zoom_scale_, other.minimum_zoom_scale_) && |
71 StateValuesAreEqual(maximum_zoom_scale_, other.maximum_zoom_scale_) && | 87 StateValuesAreEqual(maximum_zoom_scale_, other.maximum_zoom_scale_) && |
72 StateValuesAreEqual(zoom_scale_, other.zoom_scale_); | 88 StateValuesAreEqual(zoom_scale_, other.zoom_scale_); |
73 } | 89 } |
74 | 90 |
75 bool PageZoomState::operator!=(const PageZoomState& other) const { | 91 bool PageZoomState::operator!=(const PageZoomState& other) const { |
76 return !(*this == other); | 92 return !(*this == other); |
77 } | 93 } |
78 | 94 |
| 95 // static |
| 96 NSString* PageZoomState::MinZoomKey() { |
| 97 static NSString* const kMinZoomKey = @"minZoom"; |
| 98 return kMinZoomKey; |
| 99 } |
| 100 |
| 101 // static |
| 102 NSString* PageZoomState::MaxZoomKey() { |
| 103 static NSString* const kMaxZoomKey = @"maxZoom"; |
| 104 return kMaxZoomKey; |
| 105 } |
| 106 |
| 107 // static |
| 108 NSString* PageZoomState::ZoomKey() { |
| 109 static NSString* const kZoomKey = @"zoom"; |
| 110 return kZoomKey; |
| 111 } |
| 112 |
79 PageDisplayState::PageDisplayState() { | 113 PageDisplayState::PageDisplayState() { |
80 } | 114 } |
81 | 115 |
82 PageDisplayState::PageDisplayState(const PageScrollState& scroll_state, | 116 PageDisplayState::PageDisplayState(const PageScrollState& scroll_state, |
83 const PageZoomState& zoom_state) | 117 const PageZoomState& zoom_state) |
84 : scroll_state_(scroll_state), zoom_state_(zoom_state) { | 118 : scroll_state_(scroll_state), zoom_state_(zoom_state) { |
85 } | 119 } |
86 | 120 |
87 PageDisplayState::PageDisplayState(double offset_x, | 121 PageDisplayState::PageDisplayState(double offset_x, |
88 double offset_y, | 122 double offset_y, |
89 double minimum_zoom_scale, | 123 double minimum_zoom_scale, |
90 double maximum_zoom_scale, | 124 double maximum_zoom_scale, |
91 double zoom_scale) | 125 double zoom_scale) |
92 : scroll_state_(offset_x, offset_y), | 126 : scroll_state_(offset_x, offset_y), |
93 zoom_state_(minimum_zoom_scale, maximum_zoom_scale, zoom_scale) { | 127 zoom_state_(minimum_zoom_scale, maximum_zoom_scale, zoom_scale) { |
94 } | 128 } |
95 | 129 |
| 130 PageDisplayState::PageDisplayState(NSDictionary* serialization) |
| 131 : PageDisplayState(GetValue(PageScrollState::XOffsetKey(), serialization), |
| 132 GetValue(PageScrollState::YOffsetKey(), serialization), |
| 133 GetValue(PageZoomState::MinZoomKey(), serialization), |
| 134 GetValue(PageZoomState::MaxZoomKey(), serialization), |
| 135 GetValue(PageZoomState::ZoomKey(), serialization)) {} |
| 136 |
96 PageDisplayState::~PageDisplayState() { | 137 PageDisplayState::~PageDisplayState() { |
97 } | 138 } |
98 | 139 |
99 bool PageDisplayState::IsValid() const { | 140 bool PageDisplayState::IsValid() const { |
100 return scroll_state_.IsValid() && zoom_state_.IsValid(); | 141 return scroll_state_.IsValid() && zoom_state_.IsValid(); |
101 } | 142 } |
102 | 143 |
103 bool PageDisplayState::operator==(const PageDisplayState& other) const { | 144 bool PageDisplayState::operator==(const PageDisplayState& other) const { |
104 return scroll_state_ == other.scroll_state_ && | 145 return scroll_state_ == other.scroll_state_ && |
105 zoom_state_ == other.zoom_state_; | 146 zoom_state_ == other.zoom_state_; |
106 } | 147 } |
107 | 148 |
108 bool PageDisplayState::operator!=(const PageDisplayState& other) const { | 149 bool PageDisplayState::operator!=(const PageDisplayState& other) const { |
109 return !(*this == other); | 150 return !(*this == other); |
110 } | 151 } |
111 | 152 |
| 153 NSDictionary* PageDisplayState::GetSerialization() const { |
| 154 return @{ |
| 155 PageScrollState::XOffsetKey() : @(scroll_state_.offset_x()), |
| 156 PageScrollState::YOffsetKey() : @(scroll_state_.offset_y()), |
| 157 PageZoomState::MinZoomKey() : @(zoom_state_.minimum_zoom_scale()), |
| 158 PageZoomState::MaxZoomKey() : @(zoom_state_.maximum_zoom_scale()), |
| 159 PageZoomState::ZoomKey() : @(zoom_state_.zoom_scale()) |
| 160 }; |
| 161 } |
| 162 |
| 163 NSString* PageDisplayState::GetDescription() const { |
| 164 NSString* const kPageScrollStateDescriptionFormat = |
| 165 @"{ scrollOffset:(%0.2f, %0.2f), zoomScaleRange:(%0.2f, %0.2f), " |
| 166 @"zoomScale:%0.2f }"; |
| 167 return [NSString stringWithFormat:kPageScrollStateDescriptionFormat, |
| 168 scroll_state_.offset_x(), |
| 169 scroll_state_.offset_y(), |
| 170 zoom_state_.minimum_zoom_scale(), |
| 171 zoom_state_.maximum_zoom_scale(), |
| 172 zoom_state_.zoom_scale()]; |
| 173 } |
| 174 |
112 } // namespace web | 175 } // namespace web |
OLD | NEW |