Index: remoting/host/linux/x_server_clipboard.cc |
diff --git a/remoting/host/linux/x_server_clipboard.cc b/remoting/host/linux/x_server_clipboard.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..32786729989e060a2fab3018ff31b75727c33929 |
--- /dev/null |
+++ b/remoting/host/linux/x_server_clipboard.cc |
@@ -0,0 +1,448 @@ |
+// 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. |
+ |
+#include "remoting/host/linux/x_server_clipboard.h" |
+ |
+#include <X11/extensions/Xfixes.h> |
+ |
+#include "base/callback.h" |
+#include "base/logging.h" |
+#include "remoting/base/constants.h" |
+ |
+namespace { |
+ |
+// Returns true if |a| is later than |b|, treating 0 as being a long time ago. |
+// This is different from the usual meaning of CurrentTime (which is defined |
+// to be 0). |
Wez
2012/09/17 22:13:29
As you comment, 0 is normally CurrentTime; isn't i
Lambros
2012/09/19 23:58:19
This is only called from one place, where 0 means
Wez
2012/09/20 23:37:46
And are X timestamps guaranteed never to wrap thro
|
+bool IsTimeLater(Time a, Time b) { |
Wez
2012/09/17 22:13:29
nit: IsTimeLaterThan
But... this function is just
Lambros
2012/09/19 23:58:19
Done
|
+ if (a == 0) { |
+ return false; |
+ } |
+ if (b == 0) { |
Wez
2012/09/17 22:13:29
nit: You don't need this clause, since at this poi
Lambros
2012/09/19 23:58:19
I meant to test (a - b) > 0, to deal with wrap-aro
|
+ return true; |
+ } |
+ return a > b; |
+} |
+ |
+} // namespace |
+ |
+namespace remoting { |
+ |
+XServerClipboard::XServerClipboard() |
+ : display_(NULL), |
+ clipboard_window_(BadValue), |
+ have_xfixes_(false), |
+ xfixes_event_base_(-1), |
+ xfixes_error_base_(-1), |
+ clipboard_atom_(None), |
+ large_selection_atom_(None), |
+ selection_string_atom_(None), |
+ targets_atom_(None), |
+ timestamp_atom_(None), |
+ utf8_string_atom_(None), |
+ large_selection_property_(None), |
+ current_selection_time_(0), |
+ new_selection_(None), |
+ getting_initial_selection_(true), |
+ new_cut_text_(false), |
+ had_valid_timestamp_(false) { |
+} |
+ |
+XServerClipboard::~XServerClipboard() { |
+} |
+ |
+void XServerClipboard::Init(Display* display, |
+ const ClipboardChangedCallback& callback) { |
+ display_ = display; |
+ callback_ = callback; |
+ |
+ // If any of these X API calls fail, an X Error will be raised, crashing the |
+ // process. This is unlikely to occur in practice, and even if it does, it |
+ // would mean the X server is in a bad state, so it's not worth trying to |
+ // trap such errors here. |
Wez
2012/09/17 22:13:29
Jamie's XRANDR CL adds ScopedXErrorHandler; use th
Lambros
2012/09/19 23:58:19
Added TODO. X Error handlers are tricky, since (I
Wez
2012/09/20 23:37:46
That's decidedly unhelpful! Would we be better off
|
+ clipboard_window_ = XCreateSimpleWindow(display_, |
+ DefaultRootWindow(display_), |
+ 0, 0, 1, 1, // x, y, width, height |
+ 0, 0, 0); |
+ |
+ // TODO(lambroslambrou): Use ui::X11AtomCache for this. |
Wez
2012/09/17 22:13:29
nit: Why are you not already using it in this CL?
Lambros
2012/09/19 23:58:19
Clarified reason for not using it right now.
|
+ const int kNumAtoms = 6; |
+ const char* names[kNumAtoms] = { |
+ "CLIPBOARD", |
+ "INCR", |
+ "SELECTION_STRING", |
+ "TARGETS", |
+ "TIMESTAMP", |
+ "UTF8_STRING" }; |
+ Atom atoms[kNumAtoms]; |
+ if (XInternAtoms(display_, const_cast<char**>(names), kNumAtoms, False, |
+ atoms)) { |
+ clipboard_atom_ = atoms[0]; |
+ large_selection_atom_ = atoms[1]; |
+ selection_string_atom_ = atoms[2]; |
+ targets_atom_ = atoms[3]; |
+ timestamp_atom_ = atoms[4]; |
+ utf8_string_atom_ = atoms[5]; |
+ } else { |
+ LOG(ERROR) << "XInternAtoms failed"; |
+ } |
+ |
+ if (XFixesQueryExtension(display_, &xfixes_event_base_, |
+ &xfixes_error_base_)) { |
+ have_xfixes_ = true; |
+ XFixesSelectSelectionInput(display_, clipboard_window_, XA_PRIMARY, |
+ XFixesSetSelectionOwnerNotifyMask); |
+ XFixesSelectSelectionInput(display_, clipboard_window_, clipboard_atom_, |
+ XFixesSetSelectionOwnerNotifyMask); |
+ } else { |
+ LOG(INFO) << "X server does not support XFixes."; |
+ } |
+ |
+ GetSelections(None, 0); |
+} |
+ |
+void XServerClipboard::SetClipboard(const std::string& mime_type, |
+ const std::string& data) { |
+ if (!display_) { |
Wez
2012/09/17 22:13:29
Under what circumstances is it ever valid to call
Lambros
2012/09/19 23:58:19
Done.
|
+ return; |
+ } |
+ |
+ // Currently only UTF-8 is supported. |
+ if (mime_type != kMimeTypeTextUtf8) { |
+ return; |
+ } |
+ |
+ data_ = data; |
+ |
+ OwnSelection(XA_PRIMARY, CurrentTime); |
Wez
2012/09/17 22:13:29
You're calling XSetSelectionOwner() with CurrentTi
Lambros
2012/09/19 23:58:19
I don't think so - the man-page and this web-site
|
+ OwnSelection(clipboard_atom_, CurrentTime); |
+ current_selection_time_ = CurrentTime; |
Wez
2012/09/17 22:13:29
CurrentTime == 0, so this doesn't do what it looks
Lambros
2012/09/19 23:58:19
You're right, this will cause any other timestamp
Wez
2012/09/20 23:37:46
Leaving the most-recently-seen time is probably fi
|
+} |
+ |
+void XServerClipboard::ProcessXEvent(XEvent* event) { |
+ if (event->xany.window != clipboard_window_) { |
+ return; |
+ } |
+ |
+ switch (event->type) { |
+ case PropertyNotify: |
+ OnPropertyNotify(event); |
+ break; |
+ case SelectionNotify: |
+ OnSelectionNotify(event); |
+ break; |
+ case SelectionRequest: |
+ OnSelectionRequest(event); |
+ break; |
+ default: |
+ break; |
+ } |
+ |
+ if (have_xfixes_) { |
+ if (event->type == xfixes_event_base_ + XFixesSetSelectionOwnerNotify) { |
+ XFixesSelectionNotifyEvent* notify_event = |
+ reinterpret_cast<XFixesSelectionNotifyEvent*>(event); |
+ GetSelections(notify_event->selection, notify_event->selection_timestamp); |
Wez
2012/09/17 22:13:29
|selection_timestamp| is the timestamp used to gen
Lambros
2012/09/19 23:58:19
I think the X Server is supposed to substitute the
|
+ } |
+ } |
+} |
+ |
+void XServerClipboard::GetSelections(Atom selection, Time timestamp) { |
+ // There are several different "targets" for which we request information: |
+ // the timestamp, the target list and the string value of the selection. |
Wez
2012/09/17 22:13:29
I don't understand this sentence; are you saying t
Lambros
2012/09/19 23:58:19
Clarified.
|
+ // |
+ // The process takes some time since there are several round-trips to the X |
+ // server and the selection-owning app(s). We need to protect against |
+ // restarting the process when we are in the middle of it since we store |
+ // state between the various selectionNotify() callbacks we will get. We set |
Wez
2012/09/17 22:13:29
Where are we storing this problem state?
|
+ // a timer to flag when it is happening. Where the selection owner(s) are |
+ // behaving themselves we cancel the timer in FinishGetSelections(). If they |
+ // don't respond in reasonable time then we assume something went wrong and |
+ // will allow a retry after the timer expires. |
Wez
2012/09/17 22:13:29
How does this resolve the issue of leaving stale s
Lambros
2012/09/19 23:58:19
I've rephrased this, ptal.
|
+ // |
+ // We start out trying to establish the timestamp(s) on the selections to |
+ // decide which one to retrieve. If a selection and timestamp are passed in |
Wez
2012/09/17 22:13:29
A timestamp is always passed it; is there some spe
Lambros
2012/09/19 23:58:19
Clarified.
|
+ // then we always use that selection and treat it as new. Otherwise we |
Wez
2012/09/17 22:13:29
You haven't introduced a concept of "new" up to th
|
+ // request the timestamp(s) from the selection owner. If we get a valid |
+ // timestamp and one of the selections is new, then we use that selection. |
Wez
2012/09/17 22:13:29
What does it mean for a selection to be "new"?
Lambros
2012/09/19 23:58:19
Newer than |current_selection_time_|
|
+ // If we get a valid timestamp but the selections are not new, then we just |
+ // stop processing there. |
+ // |
+ // If we don't get a valid timestamp (for example an app like Mozilla which |
+ // sets a zero timestamp, or an app like VMWare which doesn't support the |
+ // timestamp target), then we can't use the timestamp optimisation so we have |
+ // to get the actual value of the selection each time. We use either the |
+ // PRIMARY or CLIPBOARD selection as appropriate. |
+ // |
+ // Before getting the value of the chosen selection, we request the list of |
+ // targets supported for that selection. If UTF8_STRING is supported then we |
+ // use that, otherwise we fall back to using STRING. |
Wez
2012/09/17 22:13:29
As with the other parts of this big block of comme
Lambros
2012/09/19 23:58:19
Done.
Lambros
2012/09/19 23:58:19
Done.
|
+ // |
+ // As a final minor complication, when one of the selections is actually |
+ // owned by us, we don't request the details for it. |
+ if (!get_selections_time_.is_null() && |
Wez
2012/09/17 22:13:29
Add a comment, separate from this big block commen
Lambros
2012/09/19 23:58:19
Done.
|
+ (base::TimeTicks::Now() - get_selections_time_) < |
+ base::TimeDelta::FromSeconds(5)) { |
+ return; |
+ } |
+ |
+ if (selection != None) { |
+ if (selection != clipboard_atom_ && selection != XA_PRIMARY) { |
+ return; |
+ } |
+ } else { |
+ selection = XA_PRIMARY; |
+ } |
+ |
+ if (IsSelectionOwner(selection)) { |
Wez
2012/09/17 22:13:29
Add a comment to this to indicate why we ignore se
Lambros
2012/09/19 23:58:19
Done.
|
+ FinishGetSelections(); |
+ } |
+ |
+ get_selections_time_ = base::TimeTicks::Now(); |
+ |
+ if (timestamp) { |
+ current_selection_time_ = timestamp; |
+ GetSelectionTargets(selection); |
+ } else { |
+ new_selection_ = 0; |
+ had_valid_timestamp_ = false; |
+ GetSelectionTimestamp(selection, CurrentTime); |
+ } |
+} |
+ |
+void XServerClipboard::FinishGetSelections() { |
+ getting_initial_selection_ = false; |
+ get_selections_time_ = base::TimeTicks(); |
+} |
+ |
+void XServerClipboard::OnPropertyNotify(XEvent* event) { |
+ if (large_selection_property_ != None && |
+ event->xproperty.atom == large_selection_property_ && |
+ event->xproperty.state == PropertyNewValue) { |
+ Atom type; |
+ int format; |
+ unsigned long item_count, after; |
+ unsigned char *data; |
+ XGetWindowProperty(display_, clipboard_window_, large_selection_property_, |
+ 0, ~0L, True, AnyPropertyType, &type, &format, |
+ &item_count, &after, &data); |
+ if (type != None) { |
+ XFree(data); |
Wez
2012/09/17 22:13:29
Should you actually be doing something with this d
Lambros
2012/09/19 23:58:19
Added TODO for large transfers.
|
+ |
+ // If the property is zero-length then the large transfer is complete. |
+ if (item_count == 0) { |
+ large_selection_property_ = None; |
+ } |
+ } |
+ } |
+} |
+ |
+void XServerClipboard::OnSelectionNotify(XEvent* event) { |
+ if (event->xselection.property != None) { |
+ Atom type; |
+ int format; |
+ unsigned long item_count, after; |
+ unsigned char *data; |
+ XGetWindowProperty(display_, clipboard_window_, |
+ event->xselection.property, 0, ~0L, True, |
+ AnyPropertyType, &type, &format, |
+ &item_count, &after, &data); |
+ if (type == large_selection_atom_) { |
+ // Large selection - just read and ignore these for now. |
+ large_selection_property_ = event->xselection.property; |
+ } else { |
+ // Standard selection - call the selection notifier. |
+ large_selection_property_ = None; |
+ if (type != None) { |
+ DoSelectionNotify(&event->xselection, type, format, item_count, data); |
+ XFree(data); |
+ } |
+ } |
+ } |
+ DoSelectionNotify(&event->xselection, 0, 0, 0, 0); |
Wez
2012/09/17 22:13:29
Are you missing a closing } after this?
Wez
2012/09/17 22:13:29
What does this DoSelectionNotify() call mean? Shou
Lambros
2012/09/19 23:58:19
No, this is an indentation fail! Fixed.
Lambros
2012/09/19 23:58:19
Good catch! Added missing return.
|
+} |
+ |
+void XServerClipboard::OnSelectionRequest(XEvent* event) { |
+ XSelectionEvent selection_event; |
+ selection_event.type = SelectionNotify; |
+ selection_event.display = event->xselectionrequest.display; |
+ selection_event.requestor = event->xselectionrequest.requestor; |
+ selection_event.selection = event->xselectionrequest.selection; |
+ selection_event.time = event->xselectionrequest.time; |
+ selection_event.target = event->xselectionrequest.target; |
+ if (event->xselectionrequest.property == None) { |
+ event->xselectionrequest.property = event->xselectionrequest.target; |
+ } |
+ if (!IsSelectionOwner(selection_event.selection)) { |
+ selection_event.property = None; |
+ } else { |
+ selection_event.property = event->xselectionrequest.property; |
+ if (selection_event.target == targets_atom_) { |
+ Atom targets[3]; |
+ targets[0] = timestamp_atom_; |
+ targets[1] = utf8_string_atom_; |
+ targets[2] = XA_STRING; |
+ XChangeProperty(display_, selection_event.requestor, |
+ selection_event.property, XA_ATOM, 32, PropModeReplace, |
+ reinterpret_cast<unsigned char*>(targets), 3); |
+ } else if (selection_event.target == timestamp_atom_) { |
+ Time time = selection_own_time_[selection_event.selection]; |
+ XChangeProperty(display_, selection_event.requestor, |
+ selection_event.property, XA_INTEGER, 32, |
+ PropModeReplace, reinterpret_cast<unsigned char*>(&time), |
+ 1); |
+ } else if (selection_event.target == utf8_string_atom_ || |
+ selection_event.target == XA_STRING) { |
+ if (!data_.empty()) { |
+ XChangeProperty(display_, selection_event.requestor, |
+ selection_event.property, selection_event.target, 8, |
+ PropModeReplace, |
+ reinterpret_cast<unsigned char*>( |
+ const_cast<char*>(data_.data())), |
+ data_.size()); |
+ } |
+ } |
+ } |
+ XSendEvent(display_, selection_event.requestor, False, 0, |
+ reinterpret_cast<XEvent*>(&selection_event)); |
+} |
+ |
+void XServerClipboard::DoSelectionNotify(XSelectionEvent* event, |
+ Atom type, |
+ int format, |
+ int item_count, |
+ void* data) { |
+ bool finished = false; |
+ |
+ if (event->target == timestamp_atom_) { |
+ finished = GotSelectionTimestamp(event, format, item_count, data); |
+ } else if (event->target == targets_atom_) { |
+ finished = GotSelectionTargets(event, format, item_count, data); |
+ } else if (event->target == utf8_string_atom_ || |
+ event->target == XA_STRING) { |
+ finished = GotSelectionString(event, format, item_count, data); |
+ } |
+ |
+ if (finished) { |
+ FinishGetSelections(); |
+ } |
+} |
+ |
+bool XServerClipboard::GotSelectionTimestamp(XSelectionEvent* event, |
+ int format, |
+ int item_count, |
+ void* data) { |
+ if (event->property == timestamp_atom_) { |
Wez
2012/09/17 22:13:29
What does it mean for property not to be TIMESTAMP
Lambros
2012/09/19 23:58:19
Added comments (some copied from elsewhere as you
|
+ if (data && format == 32 && item_count == 1) { |
+ Time selection_time = *static_cast<Time*>(data); |
+ if (selection_time != 0) { |
+ had_valid_timestamp_ = true; |
+ } |
+ if (IsTimeLater(selection_time, current_selection_time_)) { |
+ current_selection_time_ = selection_time; |
+ new_selection_ = event->selection; |
+ } |
+ } |
+ } |
+ |
+ if (event->selection == XA_PRIMARY) { |
+ if (!IsSelectionOwner(clipboard_atom_)) { |
+ GetSelectionTimestamp(clipboard_atom_, event->time); |
+ return false; |
+ } |
+ } else if (event->selection != clipboard_atom_) { |
+ return false; |
+ } |
+ |
+ if (getting_initial_selection_ && had_valid_timestamp_) { |
+ return true; |
+ } |
+ |
+ if (new_selection_) { |
+ GetSelectionTargets(new_selection_); |
+ } else if (!had_valid_timestamp_) { |
+ GetSelectionTargets(XA_PRIMARY); |
+ } else { |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool XServerClipboard::GotSelectionTargets(XSelectionEvent* event, |
Wez
2012/09/17 22:13:29
nit: HandleSelectionTargetsEvent?
Lambros
2012/09/19 23:58:19
Done.
|
+ int format, |
+ int item_count, |
+ void* data) { |
+ if (event->property == targets_atom_) { |
+ if (data && format == 32) { |
+ // The XGetWindowProperty man-page specifies that the returned |
+ // property data will be an array of |long|s in the case where |
+ // |format| == 32. On a 64-bit system, each item gets padded to |
+ // 8 bytes, the size of a |long|. |
Wez
2012/09/17 22:13:29
nit: This is an Xlib-ism; the protocol uses CARD32
Lambros
2012/09/19 23:58:19
Clarified that it's Xlib that does the padding.
|
+ const long* targets = static_cast<const long*>(data); |
+ for (int i = 0; i < item_count; i++) { |
+ if (targets[i] == static_cast<long>(utf8_string_atom_)) { |
+ GetSelectionString(event->selection, utf8_string_atom_); |
+ return false; |
+ } |
+ } |
+ } |
+ } |
+ GetSelectionString(event->selection, XA_STRING); |
+ return false; |
+} |
+ |
+bool XServerClipboard::GotSelectionString(XSelectionEvent* event, |
Wez
2012/09/17 22:13:29
nit: HandleSelectionStringEvent?
Lambros
2012/09/19 23:58:19
Done.
|
+ int format, |
+ int item_count, |
+ void* data) { |
+ if (event->property != selection_string_atom_ || !data || format != 8) { |
+ return true; |
+ } |
+ |
+ std::string text(static_cast<char*>(data), item_count); |
+ |
+ if (event->target == XA_STRING || event->target == utf8_string_atom_) { |
+ GotCutTextUtf8(text); |
+ } |
+ return true; |
+} |
+ |
+void XServerClipboard::GotCutTextUtf8(const std::string& text) { |
Wez
2012/09/17 22:13:29
nit: NotifyClipboardText()?
Lambros
2012/09/19 23:58:19
Done.
|
+ data_ = text; |
+ new_cut_text_ = false; |
+ if (!getting_initial_selection_) { |
+ callback_.Run(kMimeTypeTextUtf8, data_); |
+ } |
+} |
+ |
+bool XServerClipboard::IsSelectionOwner(Atom selection) { |
+ return selections_owned_.find(selection) != selections_owned_.end(); |
+} |
+ |
+void XServerClipboard::GetSelectionTimestamp(Atom selection, Time time) { |
Wez
2012/09/17 22:13:29
nit: RequestSelectionTimestamp, since it's async?
Lambros
2012/09/19 23:58:19
Done.
|
+ XConvertSelection(display_, selection, timestamp_atom_, timestamp_atom_, |
+ clipboard_window_, time); |
+} |
+ |
+void XServerClipboard::GetSelectionTargets(Atom selection) { |
Wez
2012/09/17 22:13:29
As above
Lambros
2012/09/19 23:58:19
Done.
|
+ XConvertSelection(display_, selection, targets_atom_, targets_atom_, |
+ clipboard_window_, CurrentTime); |
+} |
+ |
+void XServerClipboard::GetSelectionString(Atom selection, Atom target) { |
Wez
2012/09/17 22:13:29
As above
Lambros
2012/09/19 23:58:19
Done.
|
+ XConvertSelection(display_, selection, target, selection_string_atom_, |
+ clipboard_window_, CurrentTime); |
+} |
+ |
+void XServerClipboard::OwnSelection(Atom selection, Time time) { |
Wez
2012/09/17 22:13:29
nit: AssertSelectionOwnership()
Lambros
2012/09/19 23:58:19
Done.
|
+ XSetSelectionOwner(display_, selection, clipboard_window_, time); |
+ if (XGetSelectionOwner(display_, selection) == clipboard_window_) { |
+ selections_owned_.insert(selection); |
+ selection_own_time_[selection] = time; |
+ } else { |
+ LOG(ERROR) << "XSetSelectionOwner failed for selection " << selection; |
+ } |
+} |
+ |
+} // namespace remoting |