Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: ui/base/clipboard/custom_data_helper.cc

Issue 8803005: Helpers to pickle custom data for web copy/paste and drag/drop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove clunky StringPiece usage. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4 //
5 // TODO(dcheng): For efficiency reasons, consider passing custom data around
6 // as a vector instead. It allows us to append a
7 // std::pair<string16, string16> and swap the deserialized values in.
8
9 #include "ui/base/clipboard/custom_data_helper.h"
10
11 #include <utility>
12
13 #include "base/pickle.h"
14
15 namespace ui {
16
17 namespace {
18
19 class SkippablePickle : public Pickle {
20 public:
21 SkippablePickle(const void* data, size_t data_len);
22 bool SkipString16(void** iter);
23 };
24
25 SkippablePickle::SkippablePickle(const void* data, size_t data_len)
26 : Pickle(reinterpret_cast<const char*>(data), data_len) {
27 }
28
29 bool SkippablePickle::SkipString16(void** iter) {
30 DCHECK(iter);
31
32 int len;
33 if (!ReadLength(iter, &len))
34 return false;
35 if (!IteratorHasRoomFor(*iter, len * sizeof(char16)))
36 return false;
37
38 UpdateIter(iter, len * sizeof(char16));
39 return true;
40 }
41
42 } // namespace
43
44 void ReadCustomDataTypes(const void* data,
45 size_t data_length,
46 std::vector<string16>* types) {
47 SkippablePickle pickle(data, data_length);
48 void* iter = NULL;
49
50 size_t size = 0;
51 if (!pickle.ReadSize(&iter, &size))
52 return;
53
54 // Keep track of the original elements in the types vector. On failure, we
55 // truncate the vector to the original size since we want to ignore corrupt
56 // custom data pickles.
57 size_t original_size = types->size();
58
59 for (size_t i = 0; i < size; ++i) {
60 types->push_back(string16());
61 if (!pickle.ReadString16(&iter, &types->back()) ||
62 !pickle.SkipString16(&iter)) {
63 types->resize(original_size);
64 return;
65 }
66 }
67 }
68
69 void ReadCustomDataForType(const void* data,
70 size_t data_length,
71 const string16& type,
72 string16* result) {
73 SkippablePickle pickle(data, data_length);
74 void* iter = NULL;
75
76 size_t size = 0;
77 if (!pickle.ReadSize(&iter, &size))
78 return;
79
80 for (size_t i = 0; i < size; ++i) {
81 string16 deserialized_type;
82 if (!pickle.ReadString16(&iter, &deserialized_type))
83 return;
84 if (deserialized_type == type) {
85 pickle.ReadString16(&iter, result);
86 return;
87 }
88 if (!pickle.SkipString16(&iter))
89 return;
90 }
91 }
92
93 void ReadCustomDataIntoMap(const void* data,
94 size_t data_length,
95 std::map<string16, string16>* result) {
96 Pickle pickle(reinterpret_cast<const char*>(data), data_length);
97 void* iter = NULL;
98
99 size_t size = 0;
100 if (!pickle.ReadSize(&iter, &size))
101 return;
102
103 for (size_t i = 0; i < size; ++i) {
104 string16 type;
105 string16 data;
tony 2011/12/05 18:20:16 Nit: string16 data shadows the function arg data.
dcheng 2011/12/05 18:34:10 Done. Not needed any more since we saved a copy be
106 if (!pickle.ReadString16(&iter, &type) ||
107 !pickle.ReadString16(&iter, &data)) {
108 // Data is corrupt, return an empty map.
109 result->clear();
110 return;
111 }
112 result->insert(std::make_pair(type, data));
tony 2011/12/05 18:20:16 Nit: I think we can save a copy of |data| by readi
dcheng 2011/12/05 18:34:10 Done.
113 }
114 }
115
116 void WriteCustomDataToPickle(const std::map<string16, string16>& data,
117 Pickle* pickle) {
118 pickle->WriteSize(data.size());
119 for (std::map<string16, string16>::const_iterator it = data.begin();
120 it != data.end();
121 ++it) {
122 pickle->WriteString16(it->first);
123 pickle->WriteString16(it->second);
124 }
125 }
126
127 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/clipboard/custom_data_helper.h ('k') | ui/base/clipboard/custom_data_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698