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

Side by Side Diff: ui/base/dragdrop/os_exchange_data_provider_aurax11.cc

Issue 17029020: linux_aura: Redo how memory is handled in clipboard/drag code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes for sky; ptal Created 7 years, 5 months 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
« no previous file with comments | « ui/base/dragdrop/os_exchange_data_provider_aurax11.h ('k') | ui/base/x/selection_owner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/dragdrop/os_exchange_data_provider_aurax11.h" 5 #include "ui/base/dragdrop/os_exchange_data_provider_aurax11.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ref_counted_memory.h"
8 #include "base/message_loop/message_pump_aurax11.h" 9 #include "base/message_loop/message_pump_aurax11.h"
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/net_util.h" 12 #include "net/base/net_util.h"
12 #include "ui/base/clipboard/clipboard.h" 13 #include "ui/base/clipboard/clipboard.h"
13 #include "ui/base/clipboard/scoped_clipboard_writer.h" 14 #include "ui/base/clipboard/scoped_clipboard_writer.h"
14 #include "ui/base/x/selection_utils.h" 15 #include "ui/base/x/selection_utils.h"
15 #include "ui/base/x/x11_util.h" 16 #include "ui/base/x/x11_util.h"
16 17
17 // Note: the GetBlah() methods are used immediately by the 18 // Note: the GetBlah() methods are used immediately by the
(...skipping 15 matching lines...) Expand all
33 Clipboard::kMimeTypeURIList, 34 Clipboard::kMimeTypeURIList,
34 kMimeTypeMozillaURL, 35 kMimeTypeMozillaURL,
35 Clipboard::kMimeTypeText, 36 Clipboard::kMimeTypeText,
36 NULL 37 NULL
37 }; 38 };
38 39
39 } // namespace 40 } // namespace
40 41
41 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11( 42 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11(
42 ::Window x_window, 43 ::Window x_window,
43 scoped_ptr<SelectionFormatMap> selection) 44 const SelectionFormatMap& selection)
44 : x_display_(GetXDisplay()), 45 : x_display_(GetXDisplay()),
45 x_root_window_(DefaultRootWindow(x_display_)), 46 x_root_window_(DefaultRootWindow(x_display_)),
46 own_window_(false), 47 own_window_(false),
47 x_window_(x_window), 48 x_window_(x_window),
48 atom_cache_(x_display_, kAtomsToCache), 49 atom_cache_(x_display_, kAtomsToCache),
49 format_map_(selection.Pass()), 50 format_map_(selection),
50 selection_owner_(x_display_, x_window_, 51 selection_owner_(x_display_, x_window_,
51 atom_cache_.GetAtom(kDndSelection)) { 52 atom_cache_.GetAtom(kDndSelection)) {
52 // We don't know all possible MIME types at compile time. 53 // We don't know all possible MIME types at compile time.
53 atom_cache_.allow_uncached_atoms(); 54 atom_cache_.allow_uncached_atoms();
54 } 55 }
55 56
56 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11() 57 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11()
57 : x_display_(GetXDisplay()), 58 : x_display_(GetXDisplay()),
58 x_root_window_(DefaultRootWindow(x_display_)), 59 x_root_window_(DefaultRootWindow(x_display_)),
59 own_window_(true), 60 own_window_(true),
60 x_window_(XCreateWindow( 61 x_window_(XCreateWindow(
61 x_display_, 62 x_display_,
62 x_root_window_, 63 x_root_window_,
63 -100, -100, 10, 10, // x, y, width, height 64 -100, -100, 10, 10, // x, y, width, height
64 0, // border width 65 0, // border width
65 CopyFromParent, // depth 66 CopyFromParent, // depth
66 InputOnly, 67 InputOnly,
67 CopyFromParent, // visual 68 CopyFromParent, // visual
68 0, 69 0,
69 NULL)), 70 NULL)),
70 atom_cache_(x_display_, kAtomsToCache), 71 atom_cache_(x_display_, kAtomsToCache),
71 format_map_(new SelectionFormatMap), 72 format_map_(),
72 selection_owner_(x_display_, x_window_, 73 selection_owner_(x_display_, x_window_,
73 atom_cache_.GetAtom(kDndSelection)) { 74 atom_cache_.GetAtom(kDndSelection)) {
74 // We don't know all possible MIME types at compile time. 75 // We don't know all possible MIME types at compile time.
75 atom_cache_.allow_uncached_atoms(); 76 atom_cache_.allow_uncached_atoms();
76 77
77 XStoreName(x_display_, x_window_, "Chromium Drag & Drop Window"); 78 XStoreName(x_display_, x_window_, "Chromium Drag & Drop Window");
78 79
79 base::MessagePumpAuraX11::Current()->AddDispatcherForWindow(this, x_window_); 80 base::MessagePumpAuraX11::Current()->AddDispatcherForWindow(this, x_window_);
80 } 81 }
81 82
82 OSExchangeDataProviderAuraX11::~OSExchangeDataProviderAuraX11() { 83 OSExchangeDataProviderAuraX11::~OSExchangeDataProviderAuraX11() {
83 if (own_window_) { 84 if (own_window_) {
84 base::MessagePumpAuraX11::Current()->RemoveDispatcherForWindow(x_window_); 85 base::MessagePumpAuraX11::Current()->RemoveDispatcherForWindow(x_window_);
85 XDestroyWindow(x_display_, x_window_); 86 XDestroyWindow(x_display_, x_window_);
86 } 87 }
87 } 88 }
88 89
89 void OSExchangeDataProviderAuraX11::TakeOwnershipOfSelection() const { 90 void OSExchangeDataProviderAuraX11::TakeOwnershipOfSelection() const {
90 selection_owner_.TakeOwnershipOfSelection( 91 selection_owner_.TakeOwnershipOfSelection(format_map_);
91 scoped_ptr<SelectionFormatMap>(format_map_->Clone()));
92 } 92 }
93 93
94 void OSExchangeDataProviderAuraX11::RetrieveTargets( 94 void OSExchangeDataProviderAuraX11::RetrieveTargets(
95 std::vector<Atom>* targets) const { 95 std::vector<Atom>* targets) const {
96 selection_owner_.RetrieveTargets(targets); 96 selection_owner_.RetrieveTargets(targets);
97 } 97 }
98 98
99 scoped_ptr<SelectionFormatMap> 99 SelectionFormatMap OSExchangeDataProviderAuraX11::GetFormatMap() const {
100 OSExchangeDataProviderAuraX11::CloneFormatMap() const { 100 // We return the |selection_owner_|'s format map instead of our own in case
101 // We clone the |selection_owner_|'s format map instead of our own in case
102 // ours has been modified since TakeOwnershipOfSelection() was called. 101 // ours has been modified since TakeOwnershipOfSelection() was called.
103 return selection_owner_.selection_format_map()->Clone(); 102 return selection_owner_.selection_format_map();
104 } 103 }
105 104
106 void OSExchangeDataProviderAuraX11::SetString(const string16& text_data) { 105 void OSExchangeDataProviderAuraX11::SetString(const string16& text_data) {
107 std::string utf8 = UTF16ToUTF8(text_data); 106 std::string utf8 = UTF16ToUTF8(text_data);
107 scoped_refptr<base::RefCountedMemory> mem(
108 base::RefCountedString::TakeString(&utf8));
108 109
109 // Ownership of |data| is passed to |format_map_|. 110 format_map_.Insert(atom_cache_.GetAtom(Clipboard::kMimeTypeText), mem);
110 size_t text_len = utf8.size(); 111 format_map_.Insert(atom_cache_.GetAtom(kText), mem);
111 char* data = new char[text_len]; 112 format_map_.Insert(atom_cache_.GetAtom(kString), mem);
112 memcpy(data, utf8.c_str(), text_len); 113 format_map_.Insert(atom_cache_.GetAtom(kUtf8String), mem);
113
114 format_map_->Insert(
115 atom_cache_.GetAtom(Clipboard::kMimeTypeText), data, text_len);
116 format_map_->Insert(
117 atom_cache_.GetAtom(kText), data, text_len);
118 format_map_->Insert(
119 atom_cache_.GetAtom(kString), data, text_len);
120 format_map_->Insert(
121 atom_cache_.GetAtom(kUtf8String), data, text_len);
122 } 114 }
123 115
124 void OSExchangeDataProviderAuraX11::SetURL(const GURL& url, 116 void OSExchangeDataProviderAuraX11::SetURL(const GURL& url,
125 const string16& title) { 117 const string16& title) {
126 NOTIMPLEMENTED(); 118 NOTIMPLEMENTED();
127 } 119 }
128 120
129 void OSExchangeDataProviderAuraX11::SetFilename(const base::FilePath& path) { 121 void OSExchangeDataProviderAuraX11::SetFilename(const base::FilePath& path) {
130 NOTIMPLEMENTED(); 122 NOTIMPLEMENTED();
131 } 123 }
132 124
133 void OSExchangeDataProviderAuraX11::SetFilenames( 125 void OSExchangeDataProviderAuraX11::SetFilenames(
134 const std::vector<OSExchangeData::FileInfo>& filenames) { 126 const std::vector<OSExchangeData::FileInfo>& filenames) {
135 NOTIMPLEMENTED(); 127 NOTIMPLEMENTED();
136 } 128 }
137 129
138 void OSExchangeDataProviderAuraX11::SetPickledData( 130 void OSExchangeDataProviderAuraX11::SetPickledData(
139 const OSExchangeData::CustomFormat& format, 131 const OSExchangeData::CustomFormat& format,
140 const Pickle& data) { 132 const Pickle& data) {
141 NOTIMPLEMENTED(); 133 NOTIMPLEMENTED();
142 } 134 }
143 135
144 bool OSExchangeDataProviderAuraX11::GetString(string16* result) const { 136 bool OSExchangeDataProviderAuraX11::GetString(string16* result) const {
145 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_); 137 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_);
146 std::vector< ::Atom> requested_types; 138 std::vector< ::Atom> requested_types;
147 ui::GetAtomIntersection(text_atoms, GetTargets(), &requested_types); 139 ui::GetAtomIntersection(text_atoms, GetTargets(), &requested_types);
148 140
149 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); 141 ui::SelectionData data(format_map_.GetFirstOf(requested_types));
150 if (data) { 142 if (data.IsValid()) {
151 std::string text = data->GetText(); 143 std::string text = data.GetText();
152 *result = UTF8ToUTF16(text); 144 *result = UTF8ToUTF16(text);
153 return true; 145 return true;
154 } 146 }
155 147
156 return false; 148 return false;
157 } 149 }
158 150
159 bool OSExchangeDataProviderAuraX11::GetURLAndTitle(GURL* url, 151 bool OSExchangeDataProviderAuraX11::GetURLAndTitle(GURL* url,
160 string16* title) const { 152 string16* title) const {
161 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_); 153 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_);
162 std::vector< ::Atom> requested_types; 154 std::vector< ::Atom> requested_types;
163 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); 155 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types);
164 156
165 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); 157 ui::SelectionData data(format_map_.GetFirstOf(requested_types));
166 if (data) { 158 if (data.IsValid()) {
167 // TODO(erg): Technically, both of these forms can accept multiple URLs, 159 // TODO(erg): Technically, both of these forms can accept multiple URLs,
168 // but that doesn't match the assumptions of the rest of the system which 160 // but that doesn't match the assumptions of the rest of the system which
169 // expect single types. 161 // expect single types.
170 162
171 if (data->type() == atom_cache_.GetAtom(kMimeTypeMozillaURL)) { 163 if (data.GetType() == atom_cache_.GetAtom(kMimeTypeMozillaURL)) {
172 // Mozilla URLs are (UTF16: URL, newline, title). 164 // Mozilla URLs are (UTF16: URL, newline, title).
173 string16 unparsed; 165 string16 unparsed;
174 data->AssignTo(&unparsed); 166 data.AssignTo(&unparsed);
175 167
176 std::vector<string16> tokens; 168 std::vector<string16> tokens;
177 size_t num_tokens = Tokenize(unparsed, ASCIIToUTF16("\n"), &tokens); 169 size_t num_tokens = Tokenize(unparsed, ASCIIToUTF16("\n"), &tokens);
178 if (num_tokens >= 2) { 170 if (num_tokens >= 2) {
179 *url = GURL(tokens[0]); 171 *url = GURL(tokens[0]);
180 *title = tokens[1]; 172 *title = tokens[1];
181 return true; 173 return true;
182 } else { 174 } else {
183 NOTREACHED() << "Data that claimed to be a Mozilla URL has " 175 NOTREACHED() << "Data that claimed to be a Mozilla URL has "
184 << num_tokens << " tokens instead of 2."; 176 << num_tokens << " tokens instead of 2.";
185 } 177 }
186 } else if (data->type() == atom_cache_.GetAtom( 178 } else if (data.GetType() == atom_cache_.GetAtom(
187 Clipboard::kMimeTypeURIList)) { 179 Clipboard::kMimeTypeURIList)) {
188 // uri-lists are newline separated file lists in URL encoding. 180 // uri-lists are newline separated file lists in URL encoding.
189 std::string unparsed; 181 std::string unparsed;
190 data->AssignTo(&unparsed); 182 data.AssignTo(&unparsed);
191 183
192 std::vector<std::string> tokens; 184 std::vector<std::string> tokens;
193 size_t num_tokens = Tokenize(unparsed, "\n", &tokens); 185 size_t num_tokens = Tokenize(unparsed, "\n", &tokens);
194 if (!num_tokens) { 186 if (!num_tokens) {
195 NOTREACHED() << "Empty URI list"; 187 NOTREACHED() << "Empty URI list";
196 return false; 188 return false;
197 } 189 }
198 190
199 *url = GURL(tokens[0]); 191 *url = GURL(tokens[0]);
200 *title = string16(); 192 *title = string16();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 NOTIMPLEMENTED(); 250 NOTIMPLEMENTED();
259 } 251 }
260 252
261 bool OSExchangeDataProviderAuraX11::GetHtml(string16* html, 253 bool OSExchangeDataProviderAuraX11::GetHtml(string16* html,
262 GURL* base_url) const { 254 GURL* base_url) const {
263 std::vector< ::Atom> url_atoms; 255 std::vector< ::Atom> url_atoms;
264 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); 256 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML));
265 std::vector< ::Atom> requested_types; 257 std::vector< ::Atom> requested_types;
266 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); 258 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types);
267 259
268 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); 260 ui::SelectionData data(format_map_.GetFirstOf(requested_types));
269 if (data) { 261 if (data.IsValid()) {
270 *html = data->GetHtml(); 262 *html = data.GetHtml();
271 *base_url = GURL(); 263 *base_url = GURL();
272 return true; 264 return true;
273 } 265 }
274 266
275 return false; 267 return false;
276 } 268 }
277 269
278 bool OSExchangeDataProviderAuraX11::HasHtml() const { 270 bool OSExchangeDataProviderAuraX11::HasHtml() const {
279 std::vector< ::Atom> url_atoms; 271 std::vector< ::Atom> url_atoms;
280 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); 272 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML));
(...skipping 30 matching lines...) Expand all
311 303
312 return true; 304 return true;
313 } 305 }
314 306
315 bool OSExchangeDataProviderAuraX11::GetPlainTextURL(GURL* url) const { 307 bool OSExchangeDataProviderAuraX11::GetPlainTextURL(GURL* url) const {
316 NOTIMPLEMENTED(); 308 NOTIMPLEMENTED();
317 return false; 309 return false;
318 } 310 }
319 311
320 std::vector< ::Atom> OSExchangeDataProviderAuraX11::GetTargets() const { 312 std::vector< ::Atom> OSExchangeDataProviderAuraX11::GetTargets() const {
321 return format_map_->GetTypes(); 313 return format_map_.GetTypes();
322 } 314 }
323 315
324 /////////////////////////////////////////////////////////////////////////////// 316 ///////////////////////////////////////////////////////////////////////////////
325 // OSExchangeData, public: 317 // OSExchangeData, public:
326 318
327 // static 319 // static
328 OSExchangeData::Provider* OSExchangeData::CreateProvider() { 320 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
329 return new OSExchangeDataProviderAuraX11(); 321 return new OSExchangeDataProviderAuraX11();
330 } 322 }
331 323
332 } // namespace ui 324 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/dragdrop/os_exchange_data_provider_aurax11.h ('k') | ui/base/x/selection_owner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698