Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: components/open_from_clipboard/clipboard_recent_content_ios.mm

Issue 1274713003: Refactor ClipboardRecentContent singleton creation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comment by jif Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h" 5 #import "components/open_from_clipboard/clipboard_recent_content_ios.h"
6 6
7 #import <UIKit/UIKit.h> 7 #import <UIKit/UIKit.h>
8 8
9 #include "base/ios/ios_util.h" 9 #import "base/ios/ios_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/singleton.h"
13 #include "base/metrics/user_metrics.h" 12 #include "base/metrics/user_metrics.h"
14 #include "base/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
15 #include "base/sys_info.h" 14 #include "base/sys_info.h"
16 #include "url/gurl.h" 15 #include "url/gurl.h"
17 #include "url/url_constants.h" 16 #include "url/url_constants.h"
18 17
19 ClipboardRecentContent* ClipboardRecentContent::GetInstance() { 18 // Bridge that forwards pasteboard change notifications to its delegate.
20 return ClipboardRecentContentIOS::GetInstance(); 19 @interface PasteboardNotificationListenerBridge : NSObject
20
21 // Initialize the PasteboardNotificationListenerBridge with |delegate| which
22 // must not be null.
23 - (instancetype)initWithDelegate:(ClipboardRecentContentIOS*)delegate
24 NS_DESIGNATED_INITIALIZER;
25
26 - (instancetype)init NS_UNAVAILABLE;
27
28 @end
29
30 @implementation PasteboardNotificationListenerBridge {
31 ClipboardRecentContentIOS* _delegate;
21 } 32 }
22 33
23 // Bridge that forwards pasteboard change notifications to its delegate. 34 - (instancetype)init {
24 @interface PasteboardNotificationListenerBridge : NSObject { 35 NOTREACHED();
25 ClipboardRecentContentIOS* _delegate; 36 return nil;
26 } 37 }
27 @end
28 38
29 @implementation PasteboardNotificationListenerBridge 39 - (instancetype)initWithDelegate:(ClipboardRecentContentIOS*)delegate {
30
31 - (id)initWithDelegate:(ClipboardRecentContentIOS*)delegate {
32 DCHECK(delegate); 40 DCHECK(delegate);
33 self = [super init]; 41 self = [super init];
34 if (self) { 42 if (self) {
35 _delegate = delegate; 43 _delegate = delegate;
36 [[NSNotificationCenter defaultCenter] 44 [[NSNotificationCenter defaultCenter]
37 addObserver:self 45 addObserver:self
38 selector:@selector(pasteboardChangedNotification:) 46 selector:@selector(pasteboardChangedNotification:)
39 name:UIPasteboardChangedNotification 47 name:UIPasteboardChangedNotification
40 object:[UIPasteboard generalPasteboard]]; 48 object:[UIPasteboard generalPasteboard]];
41 [[NSNotificationCenter defaultCenter] 49 [[NSNotificationCenter defaultCenter]
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 base::TimeDelta kMaximumAgeOfClipboard = base::TimeDelta::FromHours(3); 85 base::TimeDelta kMaximumAgeOfClipboard = base::TimeDelta::FromHours(3);
78 // Schemes accepted by the ClipboardRecentContentIOS. 86 // Schemes accepted by the ClipboardRecentContentIOS.
79 const char* kAuthorizedSchemes[] = { 87 const char* kAuthorizedSchemes[] = {
80 url::kHttpScheme, 88 url::kHttpScheme,
81 url::kHttpsScheme, 89 url::kHttpsScheme,
82 url::kDataScheme, 90 url::kDataScheme,
83 url::kAboutScheme, 91 url::kAboutScheme,
84 }; 92 };
85 } // namespace 93 } // namespace
86 94
87 ClipboardRecentContentIOS* ClipboardRecentContentIOS::GetInstance() {
88 return Singleton<ClipboardRecentContentIOS>::get();
89 }
90
91 bool ClipboardRecentContentIOS::GetRecentURLFromClipboard(GURL* url) const { 95 bool ClipboardRecentContentIOS::GetRecentURLFromClipboard(GURL* url) const {
92 DCHECK(url); 96 DCHECK(url);
93 if (GetClipboardContentAge() > kMaximumAgeOfClipboard || 97 if (GetClipboardContentAge() > kMaximumAgeOfClipboard ||
94 [UIPasteboard generalPasteboard].changeCount == 98 [UIPasteboard generalPasteboard].changeCount ==
95 suppressedPasteboardEntryCount_) { 99 suppressedPasteboardEntryCount_) {
96 return false; 100 return false;
97 } 101 }
98 102
99 if (urlFromPasteboardCache_.is_valid()) { 103 if (urlFromPasteboardCache_.is_valid()) {
100 *url = urlFromPasteboardCache_; 104 *url = urlFromPasteboardCache_;
(...skipping 19 matching lines...) Expand all
120 base::RecordAction( 124 base::RecordAction(
121 base::UserMetricsAction("MobileOmniboxClipboardChanged")); 125 base::UserMetricsAction("MobileOmniboxClipboardChanged"));
122 } 126 }
123 lastPasteboardChangeDate_.reset([[NSDate date] retain]); 127 lastPasteboardChangeDate_.reset([[NSDate date] retain]);
124 lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount; 128 lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
125 if (lastPasteboardChangeCount_ != suppressedPasteboardEntryCount_) { 129 if (lastPasteboardChangeCount_ != suppressedPasteboardEntryCount_) {
126 suppressedPasteboardEntryCount_ = NSIntegerMax; 130 suppressedPasteboardEntryCount_ = NSIntegerMax;
127 } 131 }
128 } 132 }
129 133
130 ClipboardRecentContentIOS::ClipboardRecentContentIOS() { 134 ClipboardRecentContentIOS::ClipboardRecentContentIOS(
135 const std::string& application_scheme)
136 : application_scheme_(application_scheme) {
131 Init(base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime())); 137 Init(base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime()));
132 } 138 }
133 139
134 ClipboardRecentContentIOS::ClipboardRecentContentIOS(base::TimeDelta uptime) { 140 ClipboardRecentContentIOS::ClipboardRecentContentIOS(
141 const std::string& application_scheme,
142 base::TimeDelta uptime)
143 : application_scheme_(application_scheme) {
135 Init(uptime); 144 Init(uptime);
136 } 145 }
137 146
138 void ClipboardRecentContentIOS::Init(base::TimeDelta uptime) { 147 void ClipboardRecentContentIOS::Init(base::TimeDelta uptime) {
139 lastPasteboardChangeCount_ = NSIntegerMax; 148 lastPasteboardChangeCount_ = NSIntegerMax;
140 suppressedPasteboardEntryCount_ = NSIntegerMax; 149 suppressedPasteboardEntryCount_ = NSIntegerMax;
141 urlFromPasteboardCache_ = URLFromPasteboard(); 150 urlFromPasteboardCache_ = URLFromPasteboard();
142 LoadFromUserDefaults(); 151 LoadFromUserDefaults();
143 152
144 // On iOS 7 (unlike on iOS 8, despite what the documentation says), the change 153 // On iOS 7 (unlike on iOS 8, despite what the documentation says), the change
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 226
218 void ClipboardRecentContentIOS::SaveToUserDefaults() { 227 void ClipboardRecentContentIOS::SaveToUserDefaults() {
219 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 228 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
220 [defaults setInteger:lastPasteboardChangeCount_ 229 [defaults setInteger:lastPasteboardChangeCount_
221 forKey:kPasteboardChangeCountKey]; 230 forKey:kPasteboardChangeCountKey];
222 [defaults setObject:lastPasteboardChangeDate_ 231 [defaults setObject:lastPasteboardChangeDate_
223 forKey:kPasteboardChangeDateKey]; 232 forKey:kPasteboardChangeDateKey];
224 [defaults setInteger:suppressedPasteboardEntryCount_ 233 [defaults setInteger:suppressedPasteboardEntryCount_
225 forKey:kSuppressedPasteboardEntryCountKey]; 234 forKey:kSuppressedPasteboardEntryCountKey];
226 } 235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698