1 // Copyright 2014 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 "remoting/host/chromeos/clipboard_aura.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "remoting/base/constants.h"
9 #include "remoting/proto/event.pb.h"
10 #include "remoting/protocol/clipboard_stub.h"
11 #include "ui/base/clipboard/clipboard.h"
12 #include "ui/base/clipboard/scoped_clipboard_writer.h"
16 // Clipboard polling interval in milliseconds.
17 const int64 kClipboardPollingIntervalMs
= 500;
23 ClipboardAura::ClipboardAura()
24 : current_change_count_(0),
26 base::TimeDelta::FromMilliseconds(kClipboardPollingIntervalMs
)) {
29 ClipboardAura::~ClipboardAura() {
32 void ClipboardAura::Start(
33 scoped_ptr
<protocol::ClipboardStub
> client_clipboard
) {
34 DCHECK(thread_checker_
.CalledOnValidThread());
36 client_clipboard_
= client_clipboard
.Pass();
38 // Aura doesn't provide a clipboard-changed notification. The only way to
39 // detect clipboard changes is by polling.
40 clipboard_polling_timer_
.Start(FROM_HERE
, polling_interval_
, this,
41 &ClipboardAura::CheckClipboardForChanges
);
44 void ClipboardAura::InjectClipboardEvent(
45 const protocol::ClipboardEvent
& event
) {
46 DCHECK(thread_checker_
.CalledOnValidThread());
48 // Currently we only handle UTF-8 text.
49 if (event
.mime_type().compare(kMimeTypeTextUtf8
) != 0) {
53 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
54 clipboard_writer
.WriteText(base::UTF8ToUTF16(event
.data()));
56 // Update local change-count to prevent this change from being picked up by
57 // CheckClipboardForChanges.
58 current_change_count_
++;
61 void ClipboardAura::SetPollingIntervalForTesting(
62 base::TimeDelta polling_interval
) {
63 DCHECK(thread_checker_
.CalledOnValidThread());
65 polling_interval_
= polling_interval
;
68 void ClipboardAura::CheckClipboardForChanges() {
69 DCHECK(thread_checker_
.CalledOnValidThread());
71 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
73 clipboard
->GetSequenceNumber(ui::CLIPBOARD_TYPE_COPY_PASTE
);
75 if (change_count
== current_change_count_
) {
79 current_change_count_
= change_count
;
81 protocol::ClipboardEvent event
;
84 clipboard
->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &data
);
85 event
.set_mime_type(kMimeTypeTextUtf8
);
88 client_clipboard_
->InjectClipboardEvent(event
);
91 scoped_ptr
<Clipboard
> Clipboard::Create() {
92 return make_scoped_ptr(new ClipboardAura());
95 } // namespace remoting