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