web_state_impl_serialized_data.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2021 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. #import "ios/web/web_state/web_state_impl_serialized_data.h"
  5. #if !defined(__has_feature) || !__has_feature(objc_arc)
  6. #error "This file requires ARC support."
  7. #endif
  8. #import "ios/web/public/navigation/web_state_policy_decider.h"
  9. #import "ios/web/public/session/crw_navigation_item_storage.h"
  10. #import "ios/web/public/session/crw_session_storage.h"
  11. #import "ios/web/public/session/serializable_user_data_manager.h"
  12. #import "ios/web/public/web_state_observer.h"
  13. namespace web {
  14. namespace {
  15. // The key under which the WebState's stable identifier was saved in the user
  16. // serializable data before the M98 release.
  17. NSString* const kTabIdKey = @"TabId";
  18. }
  19. WebStateImpl::SerializedData::SerializedData(WebStateImpl* owner,
  20. const CreateParams& create_params,
  21. CRWSessionStorage* session_storage)
  22. : owner_(owner),
  23. create_params_(create_params),
  24. session_storage_(session_storage) {
  25. DCHECK(owner_);
  26. DCHECK(session_storage_);
  27. // Restore the serializable user data as user code may depend on accessing
  28. // on those values even for an unrealized WebState.
  29. if (session_storage_.userData) {
  30. SerializableUserDataManager::FromWebState(owner_)->SetUserDataFromSession(
  31. session_storage_.userData);
  32. }
  33. }
  34. WebStateImpl::SerializedData::~SerializedData() = default;
  35. void WebStateImpl::SerializedData::TearDown() {
  36. for (auto& observer : observers())
  37. observer.WebStateDestroyed(owner_);
  38. for (auto& observer : policy_deciders())
  39. observer.WebStateDestroyed();
  40. for (auto& observer : policy_deciders())
  41. observer.ResetWebState();
  42. }
  43. WebState::CreateParams WebStateImpl::SerializedData::GetCreateParams() const {
  44. return create_params_;
  45. }
  46. CRWSessionStorage* WebStateImpl::SerializedData::GetSessionStorage() const {
  47. // If a SerializableUserDataManager is attached to the WebState, the user
  48. // may have changed its content. Thus, update the serializable user data
  49. // if needed. Use a const pointer to the WebState to avoid creating the
  50. // manager if it does not exists yet.
  51. const SerializableUserDataManager* user_data_manager =
  52. SerializableUserDataManager::FromWebState(
  53. const_cast<const WebStateImpl*>(owner_));
  54. if (user_data_manager) {
  55. session_storage_.userData = user_data_manager->GetUserDataForSession();
  56. }
  57. return session_storage_;
  58. }
  59. base::Time WebStateImpl::SerializedData::GetLastActiveTime() const {
  60. if (!create_params_.last_active_time.is_null())
  61. return create_params_.last_active_time;
  62. return session_storage_.lastActiveTime;
  63. }
  64. BrowserState* WebStateImpl::SerializedData::GetBrowserState() const {
  65. return create_params_.browser_state;
  66. }
  67. NSString* WebStateImpl::SerializedData::GetStableIdentifier() const {
  68. DCHECK(session_storage_.stableIdentifier.length);
  69. return [session_storage_.stableIdentifier copy];
  70. }
  71. const std::u16string& WebStateImpl::SerializedData::GetTitle() const {
  72. static const std::u16string kEmptyString16;
  73. CRWNavigationItemStorage* item = GetLastCommittedItem();
  74. return item ? item.title : kEmptyString16;
  75. }
  76. const FaviconStatus& WebStateImpl::SerializedData::GetFaviconStatus() const {
  77. return favicon_status_;
  78. }
  79. void WebStateImpl::SerializedData::SetFaviconStatus(
  80. const FaviconStatus& favicon_status) {
  81. favicon_status_ = favicon_status;
  82. }
  83. int WebStateImpl::SerializedData::GetNavigationItemCount() const {
  84. return session_storage_.itemStorages.count;
  85. }
  86. const GURL& WebStateImpl::SerializedData::GetVisibleURL() const {
  87. // A restored WebState has no pending item. Thus the visible item is the
  88. // last committed item. This means that GetVisibleURL() must return the
  89. // same URL as GetLastCommittedURL().
  90. return GetLastCommittedURL();
  91. }
  92. const GURL& WebStateImpl::SerializedData::GetLastCommittedURL() const {
  93. CRWNavigationItemStorage* item = GetLastCommittedItem();
  94. return item ? item.virtualURL : GURL::EmptyGURL();
  95. }
  96. // TODO(crbug.com/1264451): this private method allow to implement `GetTitle()`
  97. // and `GetLastCommittedURL()` without duplicating code. As of today, the title
  98. // and URL for the WebState are not saved directly, so this method access them
  99. // via the serialized NavigationManager state. This will be removed once the
  100. // format of the WebState serialization is changed to directly saved the title
  101. // and URL. This implementation allow to test unrealized WebState before the
  102. // new format is used. This slightly break encapsulation, but this is a private
  103. // method of a private class and the file format is quite stable, so this seem
  104. // reasonable as a temporary solution.
  105. CRWNavigationItemStorage* WebStateImpl::SerializedData::GetLastCommittedItem()
  106. const {
  107. const NSInteger index = session_storage_.lastCommittedItemIndex;
  108. if (index < 0)
  109. return nil;
  110. const NSUInteger uindex = static_cast<NSUInteger>(index);
  111. if (session_storage_.itemStorages.count <= uindex) {
  112. return nil;
  113. }
  114. return session_storage_.itemStorages[uindex];
  115. }
  116. } // namespace web