1 // Copyright (c) 2009 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 #import "ui/base/cocoa/find_pasteboard.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
10 NSString* kFindPasteboardChangedNotification =
11 @"kFindPasteboardChangedNotification_Chrome";
13 @implementation FindPasteboard
15 + (FindPasteboard*)sharedInstance {
16 static FindPasteboard* instance = nil;
18 instance = [[FindPasteboard alloc] init];
24 if ((self = [super init])) {
25 findText_.reset([[NSString alloc] init]);
27 // Check if the text in the findboard has changed on app activate.
28 [[NSNotificationCenter defaultCenter]
30 selector:@selector(loadTextFromPasteboard:)
31 name:NSApplicationDidBecomeActiveNotification
33 [self loadTextFromPasteboard:nil];
39 // Since this is a singleton, this should only be executed in test code.
40 [[NSNotificationCenter defaultCenter] removeObserver:self];
44 - (NSPasteboard*)findPboard {
45 return [NSPasteboard pasteboardWithName:NSFindPboard];
48 - (void)loadTextFromPasteboard:(NSNotification*)notification {
49 NSPasteboard* findPboard = [self findPboard];
50 if ([[findPboard types] containsObject:NSStringPboardType])
51 [self setFindText:[findPboard stringForType:NSStringPboardType]];
54 - (NSString*)findText {
58 - (void)setFindText:(NSString*)newText {
63 DCHECK([NSThread isMainThread]);
65 BOOL needToSendNotification = ![findText_.get() isEqualToString:newText];
66 if (needToSendNotification) {
67 findText_.reset([newText copy]);
68 NSPasteboard* findPboard = [self findPboard];
69 [findPboard declareTypes:[NSArray arrayWithObject:NSStringPboardType]
71 [findPboard setString:findText_.get() forType:NSStringPboardType];
72 [[NSNotificationCenter defaultCenter]
73 postNotificationName:kFindPasteboardChangedNotification
80 base::string16 GetFindPboardText() {
81 return base::SysNSStringToUTF16([[FindPasteboard sharedInstance] findText]);