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;
38 void CheckClipboardForChanges();
40 scoped_ptr<protocol::ClipboardStub> client_clipboard_;
41 scoped_ptr<base::RepeatingTimer<ClipboardMac> > clipboard_polling_timer_;
42 NSInteger current_change_count_;
44 DISALLOW_COPY_AND_ASSIGN(ClipboardMac);
47 ClipboardMac::ClipboardMac() : current_change_count_(0) {
50 ClipboardMac::~ClipboardMac() {
51 // In it2me the destructor is not called in the same thread that the timer is
52 // created. Thus the timer must have already been destroyed by now.
53 DCHECK(clipboard_polling_timer_.get() == nullptr);
56 void ClipboardMac::Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) {
57 client_clipboard_.reset(client_clipboard.release());
59 // Synchronize local change-count with the pasteboard's. The change-count is
60 // used to detect clipboard changes.
61 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
63 // OS X doesn't provide a clipboard-changed notification. The only way to
64 // detect clipboard changes is by polling.
65 clipboard_polling_timer_.reset(new base::RepeatingTimer<ClipboardMac>());
66 clipboard_polling_timer_->Start(FROM_HERE,
67 base::TimeDelta::FromMilliseconds(kClipboardPollingIntervalMs),
68 this, &ClipboardMac::CheckClipboardForChanges);
71 void ClipboardMac::InjectClipboardEvent(const protocol::ClipboardEvent& event) {
72 // Currently we only handle UTF-8 text.
73 if (event.mime_type().compare(kMimeTypeTextUtf8) != 0)
75 if (!StringIsUtf8(event.data().c_str(), event.data().length())) {
76 LOG(ERROR) << "ClipboardEvent data is not UTF-8 encoded.";
80 // Write UTF-8 text to clipboard.
81 NSString* text = base::SysUTF8ToNSString(event.data());
82 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
83 [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType]
85 [pasteboard setString:text forType:NSStringPboardType];
87 // Update local change-count to prevent this change from being picked up by
88 // CheckClipboardForChanges.
89 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
92 void ClipboardMac::CheckClipboardForChanges() {
93 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
94 NSInteger change_count = [pasteboard changeCount];
95 if (change_count == current_change_count_) {
98 current_change_count_ = change_count;
100 NSString* data = [pasteboard stringForType:NSStringPboardType];
105 protocol::ClipboardEvent event;
106 event.set_mime_type(kMimeTypeTextUtf8);
107 event.set_data(base::SysNSStringToUTF8(data));
108 client_clipboard_->InjectClipboardEvent(event);
111 scoped_ptr<Clipboard> Clipboard::Create() {
112 return make_scoped_ptr(new ClipboardMac());
115 } // namespace remoting