1 // Copyright (c) 2013 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.
5 #include "ui/base/x/selection_requestor.h"
7 #include "base/message_loop/message_pump_x11.h"
8 #include "base/run_loop.h"
9 #include "ui/base/x/selection_utils.h"
10 #include "ui/base/x/x11_util.h"
16 const char kChromeSelection[] = "CHROME_SELECTION";
18 const char* kAtomsToCache[] = {
25 SelectionRequestor::SelectionRequestor(Display* x_display,
28 : x_display_(x_display),
30 selection_name_(selection_name),
31 atom_cache_(x_display_, kAtomsToCache) {
34 SelectionRequestor::~SelectionRequestor() {}
36 bool SelectionRequestor::PerformBlockingConvertSelection(
38 scoped_refptr<base::RefCountedMemory>* out_data,
39 size_t* out_data_bytes,
40 size_t* out_data_items,
42 // The name of the property we're asking to be set on |x_window_|.
43 Atom property_to_set = atom_cache_.GetAtom(kChromeSelection);
45 XConvertSelection(x_display_,
52 // Now that we've thrown our message off to the X11 server, we block waiting
54 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
55 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
56 base::RunLoop run_loop;
58 // Stop waiting for a response after a certain amount of time.
59 const int kMaxWaitTimeForClipboardResponse = 300;
60 loop->PostDelayedTask(
62 run_loop.QuitClosure(),
63 base::TimeDelta::FromMilliseconds(kMaxWaitTimeForClipboardResponse));
65 PendingRequest pending_request(target, run_loop.QuitClosure());
66 pending_requests_.push_back(&pending_request);
68 DCHECK(!pending_requests_.empty());
69 DCHECK_EQ(&pending_request, pending_requests_.back());
70 pending_requests_.pop_back();
72 if (pending_request.returned_property != property_to_set)
75 return ui::GetRawBytesOfProperty(x_window_, pending_request.returned_property,
76 out_data, out_data_bytes, out_data_items,
80 SelectionData SelectionRequestor::RequestAndWaitForTypes(
81 const std::vector< ::Atom>& types) {
82 for (std::vector< ::Atom>::const_iterator it = types.begin();
83 it != types.end(); ++it) {
84 scoped_refptr<base::RefCountedMemory> data;
85 size_t data_bytes = 0;
87 if (PerformBlockingConvertSelection(*it,
93 return SelectionData(type, data);
97 return SelectionData();
100 void SelectionRequestor::OnSelectionNotify(const XSelectionEvent& event) {
101 // Find the PendingRequest for the corresponding XConvertSelection call. If
102 // there are multiple pending requests on the same target, satisfy them in
104 PendingRequest* request_notified = NULL;
105 if (selection_name_ == event.selection) {
106 for (std::list<PendingRequest*>::iterator iter = pending_requests_.begin();
107 iter != pending_requests_.end(); ++iter) {
108 PendingRequest* request = *iter;
109 if (request->returned)
111 if (request->target != event.target)
113 request_notified = request;
118 // This event doesn't correspond to any XConvertSelection calls that we
119 // issued in PerformBlockingConvertSelection. This shouldn't happen, but any
120 // client can send any message, so it can happen.
121 if (!request_notified)
124 request_notified->returned_property = event.property;
125 request_notified->returned = true;
126 request_notified->quit_closure.Run();
129 SelectionRequestor::PendingRequest::PendingRequest(Atom target,
130 base::Closure quit_closure)
132 quit_closure(quit_closure),
133 returned_property(None),
137 SelectionRequestor::PendingRequest::~PendingRequest() {