aw_contents_lifecycle_notifier.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "android_webview/browser/lifecycle/aw_contents_lifecycle_notifier.h"
  5. #include <utility>
  6. #include "android_webview/browser_jni_headers/AwContentsLifecycleNotifier_jni.h"
  7. #include "content/public/browser/browser_thread.h"
  8. using base::android::AttachCurrentThread;
  9. using content::BrowserThread;
  10. namespace android_webview {
  11. namespace {
  12. AwContentsLifecycleNotifier::AwContentsState CalculateState(
  13. bool is_attached_to_window,
  14. bool is_window_visible) {
  15. // Can't assume the sequence of Attached, Detached, Visible, Invisible event
  16. // because the app could changed it; Calculate the state here.
  17. if (is_attached_to_window) {
  18. return is_window_visible
  19. ? AwContentsLifecycleNotifier::AwContentsState::kForeground
  20. : AwContentsLifecycleNotifier::AwContentsState::kBackground;
  21. }
  22. return AwContentsLifecycleNotifier::AwContentsState::kDetached;
  23. }
  24. AwContentsLifecycleNotifier* g_instance = nullptr;
  25. } // namespace
  26. AwContentsLifecycleNotifier::AwContentsData::AwContentsData() = default;
  27. AwContentsLifecycleNotifier::AwContentsData::AwContentsData(
  28. AwContentsData&& data) = default;
  29. AwContentsLifecycleNotifier::AwContentsData::~AwContentsData() = default;
  30. // static
  31. AwContentsLifecycleNotifier& AwContentsLifecycleNotifier::GetInstance() {
  32. DCHECK(g_instance);
  33. g_instance->EnsureOnValidSequence();
  34. return *g_instance;
  35. }
  36. AwContentsLifecycleNotifier::AwContentsLifecycleNotifier(
  37. OnLoseForegroundCallback on_lose_foreground_callback)
  38. : on_lose_foreground_callback_(std::move(on_lose_foreground_callback)) {
  39. EnsureOnValidSequence();
  40. DCHECK(!g_instance);
  41. g_instance = this;
  42. }
  43. AwContentsLifecycleNotifier::~AwContentsLifecycleNotifier() {
  44. EnsureOnValidSequence();
  45. DCHECK(g_instance);
  46. g_instance = nullptr;
  47. }
  48. void AwContentsLifecycleNotifier::OnWebViewCreated(
  49. const AwContents* aw_contents) {
  50. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  51. has_aw_contents_ever_created_ = true;
  52. bool first_created = !HasAwContentsInstance();
  53. DCHECK(aw_contents_to_data_.find(aw_contents) == aw_contents_to_data_.end());
  54. aw_contents_to_data_.emplace(aw_contents, AwContentsData());
  55. state_count_[ToIndex(AwContentsState::kDetached)]++;
  56. UpdateAppState();
  57. if (first_created) {
  58. Java_AwContentsLifecycleNotifier_onFirstWebViewCreated(
  59. AttachCurrentThread());
  60. }
  61. }
  62. void AwContentsLifecycleNotifier::OnWebViewDestroyed(
  63. const AwContents* aw_contents) {
  64. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  65. const auto it = aw_contents_to_data_.find(aw_contents);
  66. DCHECK(it != aw_contents_to_data_.end());
  67. state_count_[ToIndex(it->second.aw_content_state)]--;
  68. DCHECK(state_count_[ToIndex(it->second.aw_content_state)] >= 0);
  69. aw_contents_to_data_.erase(it);
  70. UpdateAppState();
  71. if (!HasAwContentsInstance()) {
  72. Java_AwContentsLifecycleNotifier_onLastWebViewDestroyed(
  73. AttachCurrentThread());
  74. }
  75. }
  76. void AwContentsLifecycleNotifier::OnWebViewAttachedToWindow(
  77. const AwContents* aw_contents) {
  78. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  79. auto* data = GetAwContentsData(aw_contents);
  80. data->attached_to_window = true;
  81. OnAwContentsStateChanged(data);
  82. }
  83. void AwContentsLifecycleNotifier::OnWebViewDetachedFromWindow(
  84. const AwContents* aw_contents) {
  85. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  86. auto* data = GetAwContentsData(aw_contents);
  87. data->attached_to_window = false;
  88. DCHECK(data->aw_content_state != AwContentsState::kDetached);
  89. OnAwContentsStateChanged(data);
  90. }
  91. void AwContentsLifecycleNotifier::OnWebViewWindowBeVisible(
  92. const AwContents* aw_contents) {
  93. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  94. auto* data = GetAwContentsData(aw_contents);
  95. data->window_visible = true;
  96. OnAwContentsStateChanged(data);
  97. }
  98. void AwContentsLifecycleNotifier::OnWebViewWindowBeInvisible(
  99. const AwContents* aw_contents) {
  100. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  101. auto* data = GetAwContentsData(aw_contents);
  102. data->window_visible = false;
  103. OnAwContentsStateChanged(data);
  104. }
  105. void AwContentsLifecycleNotifier::AddObserver(
  106. WebViewAppStateObserver* observer) {
  107. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  108. observers_.AddObserver(observer);
  109. observer->OnAppStateChanged(app_state_);
  110. }
  111. void AwContentsLifecycleNotifier::RemoveObserver(
  112. WebViewAppStateObserver* observer) {
  113. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  114. observers_.RemoveObserver(observer);
  115. }
  116. std::vector<const AwContents*> AwContentsLifecycleNotifier::GetAllAwContents()
  117. const {
  118. std::vector<const AwContents*> result;
  119. result.reserve(aw_contents_to_data_.size());
  120. for (auto& it : aw_contents_to_data_)
  121. result.push_back(it.first);
  122. return result;
  123. }
  124. size_t AwContentsLifecycleNotifier::ToIndex(AwContentsState state) const {
  125. size_t index = static_cast<size_t>(state);
  126. DCHECK(index < std::size(state_count_));
  127. return index;
  128. }
  129. void AwContentsLifecycleNotifier::OnAwContentsStateChanged(
  130. AwContentsLifecycleNotifier::AwContentsData* data) {
  131. AwContentsLifecycleNotifier::AwContentsState state =
  132. CalculateState(data->attached_to_window, data->window_visible);
  133. if (data->aw_content_state == state)
  134. return;
  135. state_count_[ToIndex(data->aw_content_state)]--;
  136. DCHECK(state_count_[ToIndex(data->aw_content_state)] >= 0);
  137. state_count_[ToIndex(state)]++;
  138. data->aw_content_state = state;
  139. UpdateAppState();
  140. }
  141. void AwContentsLifecycleNotifier::UpdateAppState() {
  142. WebViewAppStateObserver::State state;
  143. if (state_count_[ToIndex(AwContentsState::kForeground)] > 0)
  144. state = WebViewAppStateObserver::State::kForeground;
  145. else if (state_count_[ToIndex(AwContentsState::kBackground)] > 0)
  146. state = WebViewAppStateObserver::State::kBackground;
  147. else if (state_count_[ToIndex(AwContentsState::kDetached)] > 0)
  148. state = WebViewAppStateObserver::State::kUnknown;
  149. else
  150. state = WebViewAppStateObserver::State::kDestroyed;
  151. if (state != app_state_) {
  152. bool previous_in_foreground =
  153. app_state_ == WebViewAppStateObserver::State::kForeground;
  154. app_state_ = state;
  155. for (auto& observer : observers_) {
  156. observer.OnAppStateChanged(app_state_);
  157. }
  158. if (previous_in_foreground && on_lose_foreground_callback_) {
  159. on_lose_foreground_callback_.Run();
  160. }
  161. }
  162. }
  163. bool AwContentsLifecycleNotifier::HasAwContentsInstance() const {
  164. for (size_t i = 0; i < std::size(state_count_); i++) {
  165. if (state_count_[i] > 0)
  166. return true;
  167. }
  168. return false;
  169. }
  170. AwContentsLifecycleNotifier::AwContentsData*
  171. AwContentsLifecycleNotifier::GetAwContentsData(const AwContents* aw_contents) {
  172. DCHECK(aw_contents_to_data_.find(aw_contents) != aw_contents_to_data_.end());
  173. return &aw_contents_to_data_.at(aw_contents);
  174. }
  175. } // namespace android_webview