summaryrefslogtreecommitdiffstats
path: root/ash/display/json_converter.cc
blob: 91017b63bb6ce7c27f06e4f12df916f7d2486303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/display/json_converter.h"

#include <string>

#include "ash/display/display_layout.h"
#include "ash/display/display_pref_util.h"
#include "base/logging.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"

namespace ash {

namespace {

// Persistent key names
const char kMirroredKey[] = "mirrored";
const char kDefaultUnifiedKey[] = "default_unified";
const char kPrimaryIdKey[] = "primary-id";
const char kDisplayPlacementKey[] = "display_placement";

// DisplayPlacement key names
const char kPositionKey[] = "position";
const char kOffsetKey[] = "offset";
const char kDisplayPlacementDisplayIdKey[] = "display_id";
const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id";

bool AddLegacyValuesFromValue(const base::Value& value, DisplayLayout* layout) {
  const base::DictionaryValue* dict_value = nullptr;
  if (!value.GetAsDictionary(&dict_value))
    return false;
  int offset;
  if (dict_value->GetInteger(kOffsetKey, &offset)) {
    DisplayPlacement::Position position;
    std::string position_str;
    if (!dict_value->GetString(kPositionKey, &position_str))
      return false;
    DisplayPlacement::StringToPosition(position_str, &position);
    layout->placement_list.push_back(new DisplayPlacement(position, offset));
  }
  return true;
}

// Returns true if
//     The key is missing - output is left unchanged
//     The key matches the type - output is updated to the value.
template <typename Getter, typename Output>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    Getter getter,
                    Output* output) {
  const base::Value* field = nullptr;
  if (!dict_value->Get(field_name, &field)) {
    LOG(WARNING) << "Missing field: " << field_name;
    return true;
  }

  return (field->*getter)(output);
}

// No implementation here as specialization is required.
template <typename Output>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    Output* output);

template <>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    bool* output) {
  return UpdateFromDict(dict_value, field_name, &base::Value::GetAsBoolean,
                        output);
}

template <>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    int* output) {
  return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger,
                        output);
}

template <>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    DisplayPlacement::Position* output) {
  bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString;
  std::string value;
  if (!UpdateFromDict(dict_value, field_name, getter, &value))
    return false;

  return value.empty() ? true
                       : DisplayPlacement::StringToPosition(value, output);
}

template <>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    int64_t* output) {
  bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString;
  std::string value;
  if (!UpdateFromDict(dict_value, field_name, getter, &value))
    return false;

  return value.empty() ? true : base::StringToInt64(value, output);
}

template <>
bool UpdateFromDict(const base::DictionaryValue* dict_value,
                    const std::string& field_name,
                    ScopedVector<DisplayPlacement>* output) {
  bool (base::Value::*getter)(const base::ListValue**) const =
      &base::Value::GetAsList;
  const base::ListValue* list = nullptr;
  if (!UpdateFromDict(dict_value, field_name, getter, &list))
    return false;

  if (list == nullptr)
    return true;

  output->reserve(list->GetSize());
  for (const auto& list_item : *list) {
    const base::DictionaryValue* item_values = nullptr;
    if (!list_item->GetAsDictionary(&item_values))
      return false;

    scoped_ptr<DisplayPlacement> item(new DisplayPlacement);
    if (!UpdateFromDict(item_values, kOffsetKey, &item->offset) ||
        !UpdateFromDict(item_values, kPositionKey, &item->position) ||
        !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey,
                        &item->display_id) ||
        !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey,
                        &item->parent_display_id)) {
      return false;
    }

    output->push_back(std::move(item));
  }
  return true;
}

}  // namespace

bool JsonToDisplayLayout(const base::Value& value, DisplayLayout* layout) {
  layout->placement_list.clear();
  const base::DictionaryValue* dict_value = nullptr;
  if (!value.GetAsDictionary(&dict_value))
    return false;

  if (!UpdateFromDict(dict_value, kMirroredKey, &layout->mirrored) ||
      !UpdateFromDict(dict_value, kDefaultUnifiedKey,
                      &layout->default_unified) ||
      !UpdateFromDict(dict_value, kPrimaryIdKey, &layout->primary_id)) {
    return false;
  }

  UpdateFromDict(dict_value, kDisplayPlacementKey, &layout->placement_list);

  if (layout->placement_list.size() != 0u)
    return true;

  // For compatibility with old format.
  return AddLegacyValuesFromValue(value, layout);
}

bool DisplayLayoutToJson(const DisplayLayout& layout, base::Value* value) {
  base::DictionaryValue* dict_value = nullptr;
  if (!value->GetAsDictionary(&dict_value))
    return false;

  dict_value->SetBoolean(kMirroredKey, layout.mirrored);
  dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified);
  dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id));

  scoped_ptr<base::ListValue> placement_list(new base::ListValue);
  for (const auto* placement : layout.placement_list) {
    scoped_ptr<base::DictionaryValue> placement_value(
        new base::DictionaryValue);
    placement_value->SetString(
        kPositionKey, DisplayPlacement::PositionToString(placement->position));
    placement_value->SetInteger(kOffsetKey, placement->offset);
    placement_value->SetString(kDisplayPlacementDisplayIdKey,
                               base::Int64ToString(placement->display_id));
    placement_value->SetString(
        kDisplayPlacementParentDisplayIdKey,
        base::Int64ToString(placement->parent_display_id));
    placement_list->Append(std::move(placement_value));
  }
  dict_value->Set(kDisplayPlacementKey, std::move(placement_list));
  return true;
}

}  // namespace ash