Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / cocoa / find_pasteboard.mm
blob536d8ccd7670d76020a3d9fa647a831723c53f9b
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;
17   if (!instance) {
18     instance = [[FindPasteboard alloc] init];
19   }
20   return instance;
23 - (id)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]
29         addObserver:self
30            selector:@selector(loadTextFromPasteboard:)
31                name:NSApplicationDidBecomeActiveNotification
32              object:nil];
33     [self loadTextFromPasteboard:nil];
34   }
35   return self;
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];
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 {
55   return findText_;
58 - (void)setFindText:(NSString*)newText {
59   DCHECK(newText);
60   if (!newText)
61     return;
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]
70                        owner:nil];
71     [findPboard setString:findText_.get() forType:NSStringPboardType];
72     [[NSNotificationCenter defaultCenter]
73         postNotificationName:kFindPasteboardChangedNotification
74                       object:self];
75   }
78 @end
80 base::string16 GetFindPboardText() {
81   return base::SysNSStringToUTF16([[FindPasteboard sharedInstance] findText]);