OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/singleton.h" |
| 12 #include "base/metrics/user_metrics.h" |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "url/gurl.h" |
| 15 #include "url/url_constants.h" |
| 16 |
| 17 ClipboardRecentContent* ClipboardRecentContent::GetInstance() { |
| 18 return ClipboardRecentContentIOS::GetInstance(); |
| 19 } |
| 20 |
| 21 // Bridge that forwards pasteboard change notifications to its delegate. |
| 22 @interface PasteboardNotificationListenerBridge : NSObject { |
| 23 ClipboardRecentContentIOS* _delegate; |
| 24 } |
| 25 @end |
| 26 |
| 27 @implementation PasteboardNotificationListenerBridge |
| 28 |
| 29 - (id)initWithDelegate:(ClipboardRecentContentIOS*)delegate { |
| 30 DCHECK(delegate); |
| 31 self = [super init]; |
| 32 if (self) { |
| 33 _delegate = delegate; |
| 34 [[NSNotificationCenter defaultCenter] |
| 35 addObserver:self |
| 36 selector:@selector(pasteboardChangedNotification:) |
| 37 name:UIPasteboardChangedNotification |
| 38 object:[UIPasteboard generalPasteboard]]; |
| 39 [[NSNotificationCenter defaultCenter] |
| 40 addObserver:self |
| 41 selector:@selector(pasteboardChangedNotification:) |
| 42 name:UIApplicationDidBecomeActiveNotification |
| 43 object:nil]; |
| 44 } |
| 45 return self; |
| 46 } |
| 47 |
| 48 - (void)dealloc { |
| 49 [[NSNotificationCenter defaultCenter] |
| 50 removeObserver:self |
| 51 name:UIPasteboardChangedNotification |
| 52 object:[UIPasteboard generalPasteboard]]; |
| 53 [[NSNotificationCenter defaultCenter] |
| 54 removeObserver:self |
| 55 name:UIApplicationDidBecomeActiveNotification |
| 56 object:[UIPasteboard generalPasteboard]]; |
| 57 [super dealloc]; |
| 58 } |
| 59 |
| 60 - (void)pasteboardChangedNotification:(NSNotification*)notification { |
| 61 _delegate->PasteboardChanged(); |
| 62 } |
| 63 |
| 64 @end |
| 65 |
| 66 namespace { |
| 67 // Key used to store the pasteboard's current change count. If when resuming |
| 68 // chrome the pasteboard's change count is different from the stored one, then |
| 69 // it means that the pasteboard's content has changed. |
| 70 NSString* kPasteboardChangeCountKey = @"PasteboardChangeCount"; |
| 71 // Key used to store the last date at which it was detected that the pasteboard |
| 72 // changed. It is used to evaluate the age of the pasteboard's content. |
| 73 NSString* kPasteboardChangeDateKey = @"PasteboardChangeDate"; |
| 74 NSTimeInterval kMaximumAgeOfClipboardInSeconds = 6 * 60 * 60; |
| 75 // Schemes accepted by the ClipboardRecentContentIOS. |
| 76 const char* kAuthorizedSchemes[] = { |
| 77 url::kHttpScheme, |
| 78 url::kHttpsScheme, |
| 79 url::kDataScheme, |
| 80 url::kAboutScheme, |
| 81 }; |
| 82 } // namespace |
| 83 |
| 84 ClipboardRecentContentIOS* ClipboardRecentContentIOS::GetInstance() { |
| 85 return Singleton<ClipboardRecentContentIOS>::get(); |
| 86 } |
| 87 |
| 88 bool ClipboardRecentContentIOS::GetRecentURLFromClipboard(GURL* url) const { |
| 89 DCHECK(url); |
| 90 if (-[lastPasteboardChangeDate_ timeIntervalSinceNow] > |
| 91 kMaximumAgeOfClipboardInSeconds) { |
| 92 return false; |
| 93 } |
| 94 if (urlFromPasteboardCache_.is_valid()) { |
| 95 *url = urlFromPasteboardCache_; |
| 96 return true; |
| 97 } |
| 98 return false; |
| 99 } |
| 100 |
| 101 void ClipboardRecentContentIOS::PasteboardChanged() { |
| 102 if ([UIPasteboard generalPasteboard].changeCount != |
| 103 lastPasteboardChangeCount_) { |
| 104 urlFromPasteboardCache_ = URLFromPasteboard(); |
| 105 if (!urlFromPasteboardCache_.is_empty()) { |
| 106 base::RecordAction( |
| 107 base::UserMetricsAction("MobileOmniboxClipboardChanged")); |
| 108 } |
| 109 lastPasteboardChangeDate_.reset([[NSDate date] retain]); |
| 110 lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount; |
| 111 SaveToUserDefaults(); |
| 112 } |
| 113 } |
| 114 |
| 115 ClipboardRecentContentIOS::ClipboardRecentContentIOS() |
| 116 : ClipboardRecentContent() { |
| 117 urlFromPasteboardCache_ = URLFromPasteboard(); |
| 118 LoadFromUserDefaults(); |
| 119 if ([UIPasteboard generalPasteboard].changeCount != |
| 120 lastPasteboardChangeCount_) { |
| 121 PasteboardChanged(); |
| 122 } |
| 123 notificationBridge_.reset( |
| 124 [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); |
| 125 } |
| 126 |
| 127 ClipboardRecentContentIOS::~ClipboardRecentContentIOS() { |
| 128 } |
| 129 |
| 130 GURL ClipboardRecentContentIOS::URLFromPasteboard() { |
| 131 const std::string clipboard = |
| 132 base::SysNSStringToUTF8([[UIPasteboard generalPasteboard] string]); |
| 133 GURL gurl = GURL(clipboard); |
| 134 if (gurl.is_valid()) { |
| 135 for (size_t i = 0; i < arraysize(kAuthorizedSchemes); ++i) { |
| 136 if (gurl.SchemeIs(kAuthorizedSchemes[i])) { |
| 137 return gurl; |
| 138 } |
| 139 } |
| 140 if (!application_scheme_.empty() && |
| 141 gurl.SchemeIs(application_scheme_.c_str())) { |
| 142 return gurl; |
| 143 } |
| 144 } |
| 145 return GURL::EmptyGURL(); |
| 146 } |
| 147 |
| 148 void ClipboardRecentContentIOS::LoadFromUserDefaults() { |
| 149 lastPasteboardChangeCount_ = [[NSUserDefaults standardUserDefaults] |
| 150 integerForKey:kPasteboardChangeCountKey]; |
| 151 lastPasteboardChangeDate_.reset([[[NSUserDefaults standardUserDefaults] |
| 152 objectForKey:kPasteboardChangeDateKey] retain]); |
| 153 DCHECK(!lastPasteboardChangeDate_ || |
| 154 [lastPasteboardChangeDate_ isKindOfClass:[NSDate class]]); |
| 155 } |
| 156 |
| 157 void ClipboardRecentContentIOS::SaveToUserDefaults() { |
| 158 [[NSUserDefaults standardUserDefaults] setInteger:lastPasteboardChangeCount_ |
| 159 forKey:kPasteboardChangeCountKey]; |
| 160 [[NSUserDefaults standardUserDefaults] setObject:lastPasteboardChangeDate_ |
| 161 forKey:kPasteboardChangeDateKey]; |
| 162 } |
OLD | NEW |