1 // Copyright (c) 2011 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 "content/browser/renderer_host/clipboard_message_filter.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "content/public/browser/browser_thread.h"
15 #import "ui/base/cocoa/find_pasteboard.h"
20 // The number of utf16 code units that will be written to the find pasteboard,
21 // longer texts are silently ignored. This is to prevent that a compromised
22 // renderer can write unlimited amounts of data into the find pasteboard.
23 static const size_t kMaxFindPboardStringLength = 4096;
25 class WriteFindPboardWrapper {
27 explicit WriteFindPboardWrapper(NSString* text)
28 : text_([text retain]) {}
31 [[FindPasteboard sharedInstance] setFindText:text_];
35 base::scoped_nsobject<NSString> text_;
37 DISALLOW_COPY_AND_ASSIGN(WriteFindPboardWrapper);
42 // Called on the IO thread.
43 void ClipboardMessageFilter::OnFindPboardWriteString(const string16& text) {
44 if (text.length() <= kMaxFindPboardStringLength) {
45 NSString* nsText = base::SysUTF16ToNSString(text);
47 // FindPasteboard must be used on the UI thread.
48 BrowserThread::PostTask(
49 BrowserThread::UI, FROM_HERE, base::Bind(
50 &WriteFindPboardWrapper::Run,
51 base::Owned(new WriteFindPboardWrapper(nsText))));
56 } // namespace content