summaryrefslogtreecommitdiffstats
path: root/ui/base/clipboard/custom_data_helper.cc
blob: a50a0a072c712ce347e30466e3bddcdee5432525 (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
// Copyright (c) 2012 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.
//
// TODO(dcheng): For efficiency reasons, consider passing custom data around
// as a vector instead. It allows us to append a
// std::pair<base::string16, base::string16> and swap the deserialized values.

#include "ui/base/clipboard/custom_data_helper.h"

#include <utility>

#include "base/pickle.h"

namespace ui {

namespace {

class SkippablePickle : public base::Pickle {
 public:
  SkippablePickle(const void* data, size_t data_len);
  bool SkipString16(base::PickleIterator* iter);
};

SkippablePickle::SkippablePickle(const void* data, size_t data_len)
    : base::Pickle(reinterpret_cast<const char*>(data), data_len) {
}

bool SkippablePickle::SkipString16(base::PickleIterator* iter) {
  DCHECK(iter);

  int len;
  if (!iter->ReadLength(&len))
    return false;
  return iter->SkipBytes(len * sizeof(base::char16));
}

}  // namespace

void ReadCustomDataTypes(const void* data,
                         size_t data_length,
                         std::vector<base::string16>* types) {
  SkippablePickle pickle(data, data_length);
  base::PickleIterator iter(pickle);

  size_t size = 0;
  if (!iter.ReadSizeT(&size))
    return;

  // Keep track of the original elements in the types vector. On failure, we
  // truncate the vector to the original size since we want to ignore corrupt
  // custom data pickles.
  size_t original_size = types->size();

  for (size_t i = 0; i < size; ++i) {
    types->push_back(base::string16());
    if (!iter.ReadString16(&types->back()) || !pickle.SkipString16(&iter)) {
      types->resize(original_size);
      return;
    }
  }
}

void ReadCustomDataForType(const void* data,
                           size_t data_length,
                           const base::string16& type,
                           base::string16* result) {
  SkippablePickle pickle(data, data_length);
  base::PickleIterator iter(pickle);

  size_t size = 0;
  if (!iter.ReadSizeT(&size))
    return;

  for (size_t i = 0; i < size; ++i) {
    base::string16 deserialized_type;
    if (!iter.ReadString16(&deserialized_type))
      return;
    if (deserialized_type == type) {
      ignore_result(iter.ReadString16(result));
      return;
    }
    if (!pickle.SkipString16(&iter))
      return;
  }
}

void ReadCustomDataIntoMap(const void* data,
                           size_t data_length,
                           std::map<base::string16, base::string16>* result) {
  base::Pickle pickle(reinterpret_cast<const char*>(data), data_length);
  base::PickleIterator iter(pickle);

  size_t size = 0;
  if (!iter.ReadSizeT(&size))
    return;

  for (size_t i = 0; i < size; ++i) {
    base::string16 type;
    if (!iter.ReadString16(&type)) {
      // Data is corrupt, return an empty map.
      result->clear();
      return;
    }
    std::pair<std::map<base::string16, base::string16>::iterator, bool>
        insert_result = result->insert(std::make_pair(type, base::string16()));
    if (!iter.ReadString16(&insert_result.first->second)) {
      // Data is corrupt, return an empty map.
      result->clear();
      return;
    }
  }
}

void WriteCustomDataToPickle(
    const std::map<base::string16, base::string16>& data,
    base::Pickle* pickle) {
  pickle->WriteSizeT(data.size());
  for (std::map<base::string16, base::string16>::const_iterator it =
           data.begin();
       it != data.end();
       ++it) {
    pickle->WriteString16(it->first);
    pickle->WriteString16(it->second);
  }
}

}  // namespace ui