Don't preload rarely seen large images
[chromium-blink-merge.git] / ash / display / display_layout.cc
blob2cf54a6f2d51e41400d26e1c0e740d829128b963
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/display/display_layout.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_pref_util.h"
9 #include "base/json/json_value_converter.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/values.h"
15 #include "ui/gfx/display.h"
17 namespace ash {
18 namespace {
20 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
21 // to change this value in case to support even larger displays.
22 const int kMaxValidOffset = 10000;
24 // Persistent key names
25 const char kPositionKey[] = "position";
26 const char kOffsetKey[] = "offset";
27 const char kMirroredKey[] = "mirrored";
28 const char kDefaultUnifiedKey[] = "default_unified";
29 const char kPrimaryIdKey[] = "primary-id";
31 typedef std::map<DisplayLayout::Position, std::string> PositionToStringMap;
33 const PositionToStringMap* GetPositionToStringMap() {
34 static const PositionToStringMap* map = CreateToStringMap(
35 DisplayLayout::TOP, "top",
36 DisplayLayout::BOTTOM, "bottom",
37 DisplayLayout::RIGHT, "right",
38 DisplayLayout::LEFT, "left");
39 return map;
42 bool GetPositionFromString(const base::StringPiece& position,
43 DisplayLayout::Position* field) {
44 if (ReverseFind(GetPositionToStringMap(), position, field))
45 return true;
46 LOG(ERROR) << "Invalid position value:" << position;
47 return false;
50 std::string GetStringFromPosition(DisplayLayout::Position position) {
51 const PositionToStringMap* map = GetPositionToStringMap();
52 PositionToStringMap::const_iterator iter = map->find(position);
53 return iter != map->end() ? iter->second : std::string("unknown");
56 bool GetDisplayIdFromString(const base::StringPiece& position, int64* field) {
57 return base::StringToInt64(position, field);
60 } // namespace
62 ////////////////////////////////////////////////////////////////////////////////
63 // DisplayLayout
65 // static
66 DisplayLayout DisplayLayout::FromInts(int position, int offsets) {
67 return DisplayLayout(static_cast<Position>(position), offsets);
70 DisplayLayout::DisplayLayout()
71 : position(RIGHT),
72 offset(0),
73 mirrored(false),
74 #if defined(OS_CHROMEOS)
75 default_unified(switches::UnifiedDesktopEnabled()),
76 #else
77 default_unified(false),
78 #endif
79 primary_id(gfx::Display::kInvalidDisplayID) {
82 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset)
83 : position(position),
84 offset(offset),
85 mirrored(false),
86 #if defined(OS_CHROMEOS)
87 default_unified(switches::UnifiedDesktopEnabled()),
88 #else
89 default_unified(false),
90 #endif
91 primary_id(gfx::Display::kInvalidDisplayID) {
92 DCHECK_LE(TOP, position);
93 DCHECK_GE(LEFT, position);
95 // Set the default value to |position| in case position is invalid. DCHECKs
96 // above doesn't stop in Release builds.
97 if (TOP > position || LEFT < position)
98 this->position = RIGHT;
100 DCHECK_GE(kMaxValidOffset, abs(offset));
103 DisplayLayout DisplayLayout::Invert() const {
104 Position inverted_position = RIGHT;
105 switch (position) {
106 case TOP:
107 inverted_position = BOTTOM;
108 break;
109 case BOTTOM:
110 inverted_position = TOP;
111 break;
112 case RIGHT:
113 inverted_position = LEFT;
114 break;
115 case LEFT:
116 inverted_position = RIGHT;
117 break;
119 DisplayLayout ret = DisplayLayout(inverted_position, -offset);
120 ret.primary_id = primary_id;
121 return ret;
124 // static
125 bool DisplayLayout::ConvertFromValue(const base::Value& value,
126 DisplayLayout* layout) {
127 base::JSONValueConverter<DisplayLayout> converter;
128 return converter.Convert(value, layout);
131 // static
132 bool DisplayLayout::ConvertToValue(const DisplayLayout& layout,
133 base::Value* value) {
134 base::DictionaryValue* dict_value = NULL;
135 if (!value->GetAsDictionary(&dict_value) || dict_value == NULL)
136 return false;
138 const std::string position_str = GetStringFromPosition(layout.position);
139 dict_value->SetString(kPositionKey, position_str);
140 dict_value->SetInteger(kOffsetKey, layout.offset);
141 dict_value->SetBoolean(kMirroredKey, layout.mirrored);
142 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified);
143 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id));
144 return true;
147 std::string DisplayLayout::ToString() const {
148 const std::string position_str = GetStringFromPosition(position);
149 return base::StringPrintf("%s, %d%s%s", position_str.c_str(), offset,
150 mirrored ? ", mirrored" : "",
151 default_unified ? ", unified" : "");
154 // static
155 void DisplayLayout::RegisterJSONConverter(
156 base::JSONValueConverter<DisplayLayout>* converter) {
157 converter->RegisterCustomField<Position>(
158 kPositionKey, &DisplayLayout::position, &GetPositionFromString);
159 converter->RegisterIntField(kOffsetKey, &DisplayLayout::offset);
160 converter->RegisterBoolField(kMirroredKey, &DisplayLayout::mirrored);
161 converter->RegisterBoolField(kDefaultUnifiedKey,
162 &DisplayLayout::default_unified);
163 converter->RegisterCustomField<int64>(
164 kPrimaryIdKey, &DisplayLayout::primary_id, &GetDisplayIdFromString);
167 } // namespace ash