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
3 // found in the LICENSE file.
5 #include "remoting/host/clipboard.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/timer/timer.h"
14 #include "remoting/base/constants.h"
15 #include "remoting/base/util.h"
16 #include "remoting/proto/event.pb.h"
17 #include "remoting/protocol/clipboard_stub.h"
21 // Clipboard polling interval in milliseconds.
22 const int64 kClipboardPollingIntervalMs = 500;
28 class ClipboardMac : public Clipboard {
31 ~ClipboardMac() override;
33 // Must be called on the UI thread.
34 void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) override;
35 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override;
39 void CheckClipboardForChanges();
41 scoped_ptr<protocol::ClipboardStub> client_clipboard_;
42 scoped_ptr<base::RepeatingTimer<ClipboardMac> > clipboard_polling_timer_;
43 NSInteger current_change_count_;
45 DISALLOW_COPY_AND_ASSIGN(ClipboardMac);
48 ClipboardMac::ClipboardMac() : current_change_count_(0) {
51 ClipboardMac::~ClipboardMac() {
52 // In it2me the destructor is not called in the same thread that the timer is
53 // created. Thus the timer must have already been destroyed by now.
54 DCHECK(clipboard_polling_timer_.get() == NULL);
57 void ClipboardMac::Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) {
58 client_clipboard_.reset(client_clipboard.release());
60 // Synchronize local change-count with the pasteboard's. The change-count is
61 // used to detect clipboard changes.
62 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
64 // OS X doesn't provide a clipboard-changed notification. The only way to
65 // detect clipboard changes is by polling.
66 clipboard_polling_timer_.reset(new base::RepeatingTimer<ClipboardMac>());
67 clipboard_polling_timer_->Start(FROM_HERE,
68 base::TimeDelta::FromMilliseconds(kClipboardPollingIntervalMs),
69 this, &ClipboardMac::CheckClipboardForChanges);
72 void ClipboardMac::InjectClipboardEvent(const protocol::ClipboardEvent& event) {
73 // Currently we only handle UTF-8 text.
74 if (event.mime_type().compare(kMimeTypeTextUtf8) != 0)
76 if (!StringIsUtf8(event.data().c_str(), event.data().length())) {
77 LOG(ERROR) << "ClipboardEvent data is not UTF-8 encoded.";
81 // Write UTF-8 text to clipboard.
82 NSString* text = base::SysUTF8ToNSString(event.data());
83 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
84 [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType]
86 [pasteboard setString:text forType:NSStringPboardType];
88 // Update local change-count to prevent this change from being picked up by
89 // CheckClipboardForChanges.
90 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
93 void ClipboardMac::Stop() {
94 clipboard_polling_timer_.reset();
95 client_clipboard_.reset();
98 void ClipboardMac::CheckClipboardForChanges() {
99 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
100 NSInteger change_count = [pasteboard changeCount];
101 if (change_count == current_change_count_) {
104 current_change_count_ = change_count;
106 NSString* data = [pasteboard stringForType:NSStringPboardType];
111 protocol::ClipboardEvent event;
112 event.set_mime_type(kMimeTypeTextUtf8);
113 event.set_data(base::SysNSStringToUTF8(data));
114 client_clipboard_->InjectClipboardEvent(event);
117 scoped_ptr<Clipboard> Clipboard::Create() {
118 return make_scoped_ptr(new ClipboardMac());
121 } // namespace remoting