summaryrefslogtreecommitdiff
path: root/chromium/pdf/ppapi_migration/value_conversions.cc
blob: c0b894eadf2472f82d6cc0caf3a85082beca4341 (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
// Copyright 2020 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 "pdf/ppapi_migration/value_conversions.h"

#include <algorithm>

#include "base/containers/span.h"
#include "base/notreached.h"
#include "base/values.h"
#include "ppapi/cpp/var.h"
#include "ppapi/cpp/var_array.h"
#include "ppapi/cpp/var_array_buffer.h"
#include "ppapi/cpp/var_dictionary.h"

namespace chrome_pdf {

pp::Var VarFromValue(const base::Value& value) {
  switch (value.type()) {
    case base::Value::Type::NONE:
      return pp::Var::Null();
    case base::Value::Type::BOOLEAN:
      return pp::Var(value.GetBool());
    case base::Value::Type::INTEGER:
      return pp::Var(value.GetInt());
    case base::Value::Type::DOUBLE:
      return pp::Var(value.GetDouble());
    case base::Value::Type::STRING:
      return pp::Var(value.GetString());
    case base::Value::Type::BINARY: {
      const base::Value::BlobStorage& blob = value.GetBlob();
      pp::VarArrayBuffer buffer(blob.size());
      std::copy(blob.begin(), blob.end(), static_cast<uint8_t*>(buffer.Map()));
      return buffer;
    }
    case base::Value::Type::DICTIONARY: {
      pp::VarDictionary var_dict;
      for (const auto& value_pair : value.DictItems()) {
        var_dict.Set(value_pair.first, VarFromValue(value_pair.second));
      }
      return var_dict;
    }
    case base::Value::Type::LIST: {
      pp::VarArray var_array;
      uint32_t i = 0;
      for (const auto& val : value.GetList()) {
        var_array.Set(i, VarFromValue(val));
        i++;
      }
      return var_array;
    }
    // TODO(crbug.com/859477): Remove after root cause is found.
    case base::Value::Type::DEAD:
      CHECK(false);
      return pp::Var();
  }
}

base::Value ValueFromVar(const pp::Var& var) {
  switch (var.pp_var().type) {
    case PP_VARTYPE_UNDEFINED:
      return base::Value();
    case PP_VARTYPE_NULL:
      return base::Value();
    case PP_VARTYPE_BOOL:
      return base::Value(var.AsBool());
    case PP_VARTYPE_INT32:
      return base::Value(var.AsInt());
    case PP_VARTYPE_DOUBLE:
      return base::Value(var.AsDouble());
    case PP_VARTYPE_STRING:
      return base::Value(var.AsString());
    case PP_VARTYPE_OBJECT:
      // There is no valid conversion from PP_VARTYPE_OBJECT to a base::Value
      // type. This should not be called to convert this type.
      NOTREACHED();
      return base::Value();
    case PP_VARTYPE_ARRAY: {
      pp::VarArray var_array(var);
      base::Value::ListStorage list_storage(var_array.GetLength());
      for (uint32_t i = 0; i < var_array.GetLength(); ++i) {
        list_storage[i] = ValueFromVar(var_array.Get(i));
      }
      return base::Value(std::move(list_storage));
    }
    case PP_VARTYPE_DICTIONARY: {
      base::Value val_dictionary(base::Value::Type::DICTIONARY);
      pp::VarDictionary var_dict(var);
      pp::VarArray dict_keys = var_dict.GetKeys();
      for (uint32_t i = 0; i < dict_keys.GetLength(); ++i) {
        pp::Var key = dict_keys.Get(i);
        val_dictionary.SetKey(key.AsString(), ValueFromVar(var_dict.Get(key)));
      }
      return val_dictionary;
    }
    case PP_VARTYPE_ARRAY_BUFFER: {
      pp::VarArrayBuffer var_array_buffer(var);
      base::Value value(
          base::make_span(static_cast<uint8_t*>(var_array_buffer.Map()),
                          var_array_buffer.ByteLength()));
      var_array_buffer.Unmap();
      return value;
    }
    case PP_VARTYPE_RESOURCE:
      // There is no valid conversion from PP_VARTYPE_RESOURCE to a base::Value
      // type. This should not be called to convert this type.
      NOTREACHED();
      return base::Value();
  }
}

}  // namespace chrome_pdf