OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #import "chrome/browser/ui/cocoa/find_pasteboard.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/sys_string_conversions.h" | |
9 | |
10 NSString* kFindPasteboardChangedNotification = | |
11 @"kFindPasteboardChangedNotification_Chrome"; | |
12 | |
13 @implementation FindPasteboard | |
14 | |
15 + (FindPasteboard*)sharedInstance { | |
16 static FindPasteboard* instance = nil; | |
17 if (!instance) { | |
18 instance = [[FindPasteboard alloc] init]; | |
19 } | |
20 return instance; | |
21 } | |
22 | |
23 - (id)init { | |
24 if ((self = [super init])) { | |
25 findText_.reset([[NSString alloc] init]); | |
26 | |
27 // Check if the text in the findboard has changed on app activate. | |
28 [[NSNotificationCenter defaultCenter] | |
29 addObserver:self | |
30 selector:@selector(loadTextFromPasteboard:) | |
31 name:NSApplicationDidBecomeActiveNotification | |
32 object:nil]; | |
33 [self loadTextFromPasteboard:nil]; | |
34 } | |
35 return self; | |
36 } | |
37 | |
38 - (void)dealloc { | |
39 // Since this is a singleton, this should only be executed in test code. | |
40 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
41 [super dealloc]; | |
42 } | |
43 | |
44 - (NSPasteboard*)findPboard { | |
45 return [NSPasteboard pasteboardWithName:NSFindPboard]; | |
46 } | |
47 | |
48 - (void)loadTextFromPasteboard:(NSNotification*)notification { | |
49 NSPasteboard* findPboard = [self findPboard]; | |
50 if ([[findPboard types] containsObject:NSStringPboardType]) | |
51 [self setFindText:[findPboard stringForType:NSStringPboardType]]; | |
52 } | |
53 | |
54 - (NSString*)findText { | |
55 return findText_; | |
56 } | |
57 | |
58 - (void)setFindText:(NSString*)newText { | |
59 DCHECK(newText); | |
60 if (!newText) | |
61 return; | |
62 | |
63 DCHECK([NSThread isMainThread]); | |
64 | |
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] | |
70 owner:nil]; | |
71 [findPboard setString:findText_.get() forType:NSStringPboardType]; | |
72 [[NSNotificationCenter defaultCenter] | |
73 postNotificationName:kFindPasteboardChangedNotification | |
74 object:self]; | |
75 } | |
76 } | |
77 | |
78 @end | |
79 | |
80 string16 GetFindPboardText() { | |
81 return base::SysNSStringToUTF16([[FindPasteboard sharedInstance] findText]); | |
82 } | |
OLD | NEW |