| OLD | NEW |
| 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 #ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ | 5 #ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ |
| 6 #define COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ | 6 #define COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ |
| 7 | 7 |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "components/open_from_clipboard/clipboard_recent_content.h" | 11 #include "components/open_from_clipboard/clipboard_recent_content.h" |
| 12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 13 | 13 |
| 14 @class NSDate; | 14 @class NSDate; |
| 15 @class NSUserDefaults; | 15 @class NSUserDefaults; |
| 16 @class PasteboardNotificationListenerBridge; | 16 @class ApplicationDidBecomeActiveNotificationListenerBridge; |
| 17 | 17 |
| 18 class ClipboardRecentContentIOSTest; | 18 class ClipboardRecentContentIOSTest; |
| 19 | 19 |
| 20 // IOS implementation of ClipboardRecentContent | 20 // IOS implementation of ClipboardRecentContent |
| 21 class ClipboardRecentContentIOS : public ClipboardRecentContent { | 21 class ClipboardRecentContentIOS : public ClipboardRecentContent { |
| 22 public: | 22 public: |
| 23 // |application_scheme| is the URL scheme that can be used to open the | 23 // |application_scheme| is the URL scheme that can be used to open the |
| 24 // current application, may be empty if no such scheme exists. Used to | 24 // current application, may be empty if no such scheme exists. Used to |
| 25 // determine whether or not the clipboard contains a relevant URL. | 25 // determine whether or not the clipboard contains a relevant URL. |
| 26 // |group_user_defaults| is the NSUserDefaults used to store information on | 26 // |group_user_defaults| is the NSUserDefaults used to store information on |
| 27 // pasteboard entry expiration. This information will be shared with other | 27 // pasteboard entry expiration. This information will be shared with other |
| 28 // application in the application group. | 28 // application in the application group. |
| 29 explicit ClipboardRecentContentIOS(const std::string& application_scheme, | 29 explicit ClipboardRecentContentIOS(const std::string& application_scheme, |
| 30 NSUserDefaults* group_user_defaults); | 30 NSUserDefaults* group_user_defaults); |
| 31 ~ClipboardRecentContentIOS() override; | 31 ~ClipboardRecentContentIOS() override; |
| 32 | 32 |
| 33 // Notifies that the content of the pasteboard may have changed. | 33 // If the content of the pasteboard has changed, updates the change count, |
| 34 void PasteboardChanged(); | 34 // change date, and md5 of the latest pasteboard entry if necessary. |
| 35 void UpdateIfNeeded(); |
| 35 | 36 |
| 36 // Checks if pasteboard changed since last time a pasteboard change was | 37 // Returns whether the pasteboard changed since the last time a pasteboard |
| 37 // registered. | 38 // change was detected. |
| 38 bool HasPasteboardChanged(base::TimeDelta uptime); | 39 bool HasPasteboardChanged() const; |
| 39 | |
| 40 // Gets the current URL in the clipboard. If the cache is out of date, updates | |
| 41 // it. | |
| 42 bool GetCurrentURLFromClipboard(GURL* url); | |
| 43 | 40 |
| 44 // Loads information from the user defaults about the latest pasteboard entry. | 41 // Loads information from the user defaults about the latest pasteboard entry. |
| 45 void LoadFromUserDefaults(); | 42 void LoadFromUserDefaults(); |
| 46 | 43 |
| 47 // ClipboardRecentContent implementation. | 44 // ClipboardRecentContent implementation. |
| 48 bool GetRecentURLFromClipboard(GURL* url) const override; | 45 bool GetRecentURLFromClipboard(GURL* url) override; |
| 49 | |
| 50 base::TimeDelta GetClipboardContentAge() const override; | 46 base::TimeDelta GetClipboardContentAge() const override; |
| 51 void SuppressClipboardContent() override; | 47 void SuppressClipboardContent() override; |
| 52 void RecentURLDisplayed() override; | 48 |
| 49 protected: |
| 50 // Returns the uptime. Override in tests to return custom value. |
| 51 virtual base::TimeDelta Uptime() const; |
| 53 | 52 |
| 54 private: | 53 private: |
| 55 friend class ClipboardRecentContentIOSTest; | 54 friend class ClipboardRecentContentIOSTest; |
| 56 | 55 |
| 57 // Helper constructor for testing. |uptime| is how long ago the device has | |
| 58 // started, while |application_scheme| has the same meaning as the public | |
| 59 // constructor. | |
| 60 ClipboardRecentContentIOS(const std::string& application_scheme, | |
| 61 base::TimeDelta uptime); | |
| 62 | |
| 63 // Initializes the object. |uptime| is how long ago the device has started. | |
| 64 void Init(base::TimeDelta uptime); | |
| 65 | |
| 66 // Saves information to the user defaults about the latest pasteboard entry. | 56 // Saves information to the user defaults about the latest pasteboard entry. |
| 67 void SaveToUserDefaults(); | 57 void SaveToUserDefaults(); |
| 68 | 58 |
| 69 // Returns the URL contained in the clipboard (if any). | 59 // Returns the URL contained in the clipboard (if any). |
| 70 GURL URLFromPasteboard(); | 60 GURL URLFromPasteboard(); |
| 71 | 61 |
| 72 // Contains the URL scheme opening the app. May be empty. | 62 // Contains the URL scheme opening the app. May be empty. |
| 73 std::string application_scheme_; | 63 std::string application_scheme_; |
| 74 // The pasteboard's change count. Increases everytime the pasteboard changes. | 64 // The pasteboard's change count. Increases everytime the pasteboard changes. |
| 75 NSInteger last_pasteboard_change_count_; | 65 NSInteger last_pasteboard_change_count_; |
| 76 // Estimation of the date when the pasteboard changed. | 66 // Estimation of the date when the pasteboard changed. |
| 77 base::scoped_nsobject<NSDate> last_pasteboard_change_date_; | 67 base::scoped_nsobject<NSDate> last_pasteboard_change_date_; |
| 78 // Estimation of the copy date of the last displayed URL. | |
| 79 base::scoped_nsobject<NSDate> last_displayed_pasteboard_entry_; | |
| 80 // MD5 hash of the last registered pasteboard entry. | 68 // MD5 hash of the last registered pasteboard entry. |
| 81 base::scoped_nsobject<NSData> last_pasteboard_entry_md5_; | 69 base::scoped_nsobject<NSData> last_pasteboard_entry_md5_; |
| 82 // Cache of the GURL contained in the pasteboard (if any). | 70 // Bridge to receive notifications when the application becomes active. |
| 83 GURL url_from_pasteboard_cache_; | 71 base::scoped_nsobject<ApplicationDidBecomeActiveNotificationListenerBridge> |
| 84 // Bridge to receive notification when the pasteboard changes. | |
| 85 base::scoped_nsobject<PasteboardNotificationListenerBridge> | |
| 86 notification_bridge_; | 72 notification_bridge_; |
| 87 // The user defaults from the app group used to optimize the pasteboard change | 73 // The user defaults from the app group used to optimize the pasteboard change |
| 88 // detection. | 74 // detection. |
| 89 base::scoped_nsobject<NSUserDefaults> shared_user_defaults_; | 75 base::scoped_nsobject<NSUserDefaults> shared_user_defaults_; |
| 90 | 76 |
| 91 DISALLOW_COPY_AND_ASSIGN(ClipboardRecentContentIOS); | 77 DISALLOW_COPY_AND_ASSIGN(ClipboardRecentContentIOS); |
| 92 }; | 78 }; |
| 93 | 79 |
| 94 #endif // COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ | 80 #endif // COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_IOS_H_ |
| OLD | NEW |