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 void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) override;
34 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override;
37 void CheckClipboardForChanges();
39 scoped_ptr<protocol::ClipboardStub> client_clipboard_;
40 scoped_ptr<base::RepeatingTimer<ClipboardMac> > clipboard_polling_timer_;
41 NSInteger current_change_count_;
43 DISALLOW_COPY_AND_ASSIGN(ClipboardMac);
46 ClipboardMac::ClipboardMac() : current_change_count_(0) {}
48 ClipboardMac::~ClipboardMac() {}
50 void ClipboardMac::Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) {
51 client_clipboard_.reset(client_clipboard.release());
53 // Synchronize local change-count with the pasteboard's. The change-count is
54 // used to detect clipboard changes.
55 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
57 // OS X doesn't provide a clipboard-changed notification. The only way to
58 // detect clipboard changes is by polling.
59 clipboard_polling_timer_.reset(new base::RepeatingTimer<ClipboardMac>());
60 clipboard_polling_timer_->Start(FROM_HERE,
61 base::TimeDelta::FromMilliseconds(kClipboardPollingIntervalMs),
62 this, &ClipboardMac::CheckClipboardForChanges);
65 void ClipboardMac::InjectClipboardEvent(const protocol::ClipboardEvent& event) {
66 // Currently we only handle UTF-8 text.
67 if (event.mime_type().compare(kMimeTypeTextUtf8) != 0)
69 if (!StringIsUtf8(event.data().c_str(), event.data().length())) {
70 LOG(ERROR) << "ClipboardEvent data is not UTF-8 encoded.";
74 // Write UTF-8 text to clipboard.
75 NSString* text = base::SysUTF8ToNSString(event.data());
76 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
77 [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType]
79 [pasteboard setString:text forType:NSStringPboardType];
81 // Update local change-count to prevent this change from being picked up by
82 // CheckClipboardForChanges.
83 current_change_count_ = [[NSPasteboard generalPasteboard] changeCount];
86 void ClipboardMac::CheckClipboardForChanges() {
87 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
88 NSInteger change_count = [pasteboard changeCount];
89 if (change_count == current_change_count_) {
92 current_change_count_ = change_count;
94 NSString* data = [pasteboard stringForType:NSStringPboardType];
99 protocol::ClipboardEvent event;
100 event.set_mime_type(kMimeTypeTextUtf8);
101 event.set_data(base::SysNSStringToUTF8(data));
102 client_clipboard_->InjectClipboardEvent(event);
105 scoped_ptr<Clipboard> Clipboard::Create() {
106 return make_scoped_ptr(new ClipboardMac());
109 } // namespace remoting