Commit 813decd2 authored by Tao Bai's avatar Tao Bai Committed by Commit Bot

Add GetAllAwContents method and AwPageLoadMetricsProvider

- Added GetAllAwContent method in AwContentsLifecycleNotifier
- Changed AwContentsLifecycleNotifier to store AwContents pointer.
- Added AwPageLoadMetricsProvider which uses GetAllAwContent.

Bug: 1072770
Change-Id: Ief07426218df2ca28249de66bba2147f0a64a403
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2158914Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarNate Fischer <ntfschr@chromium.org>
Commit-Queue: Tao Bai <michaelbai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761966}
parent e1da98f2
...@@ -140,6 +140,8 @@ source_set("browser") { ...@@ -140,6 +140,8 @@ source_set("browser") {
"network_service/input_stream_reader.h", "network_service/input_stream_reader.h",
"network_service/net_helpers.cc", "network_service/net_helpers.cc",
"network_service/net_helpers.h", "network_service/net_helpers.h",
"page_load_metrics/aw_page_load_metrics_provider.cc",
"page_load_metrics/aw_page_load_metrics_provider.h",
"page_load_metrics/page_load_metrics_initialize.cc", "page_load_metrics/page_load_metrics_initialize.cc",
"page_load_metrics/page_load_metrics_initialize.h", "page_load_metrics/page_load_metrics_initialize.h",
"permission/aw_permission_request.cc", "permission/aw_permission_request.cc",
......
...@@ -48,11 +48,10 @@ void AwContentsLifecycleNotifier::OnWebViewCreated( ...@@ -48,11 +48,10 @@ void AwContentsLifecycleNotifier::OnWebViewCreated(
const AwContents* aw_contents) { const AwContents* aw_contents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
has_aw_contents_ever_created_ = true; has_aw_contents_ever_created_ = true;
uint64_t id = reinterpret_cast<uint64_t>(aw_contents);
bool first_created = !HasAwContentsInstance(); bool first_created = !HasAwContentsInstance();
DCHECK(aw_contents_id_to_data_.find(id) == aw_contents_id_to_data_.end()); DCHECK(aw_contents_to_data_.find(aw_contents) == aw_contents_to_data_.end());
aw_contents_id_to_data_.emplace(id, AwContentsData()); aw_contents_to_data_.emplace(aw_contents, AwContentsData());
state_count_[ToIndex(AwContentsState::kDetached)]++; state_count_[ToIndex(AwContentsState::kDetached)]++;
UpdateAppState(); UpdateAppState();
...@@ -65,13 +64,12 @@ void AwContentsLifecycleNotifier::OnWebViewCreated( ...@@ -65,13 +64,12 @@ void AwContentsLifecycleNotifier::OnWebViewCreated(
void AwContentsLifecycleNotifier::OnWebViewDestroyed( void AwContentsLifecycleNotifier::OnWebViewDestroyed(
const AwContents* aw_contents) { const AwContents* aw_contents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
uint64_t id = reinterpret_cast<uint64_t>(aw_contents); const auto it = aw_contents_to_data_.find(aw_contents);
const auto it = aw_contents_id_to_data_.find(id); DCHECK(it != aw_contents_to_data_.end());
DCHECK(it != aw_contents_id_to_data_.end());
state_count_[ToIndex(it->second.aw_content_state)]--; state_count_[ToIndex(it->second.aw_content_state)]--;
DCHECK(state_count_[ToIndex(it->second.aw_content_state)] >= 0); DCHECK(state_count_[ToIndex(it->second.aw_content_state)] >= 0);
aw_contents_id_to_data_.erase(it); aw_contents_to_data_.erase(it);
UpdateAppState(); UpdateAppState();
if (!HasAwContentsInstance()) { if (!HasAwContentsInstance()) {
...@@ -126,6 +124,15 @@ void AwContentsLifecycleNotifier::RemoveObserver( ...@@ -126,6 +124,15 @@ void AwContentsLifecycleNotifier::RemoveObserver(
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
std::vector<const AwContents*> AwContentsLifecycleNotifier::GetAllAwContents()
const {
std::vector<const AwContents*> result;
result.reserve(aw_contents_to_data_.size());
for (auto& it : aw_contents_to_data_)
result.push_back(it.first);
return result;
}
AwContentsLifecycleNotifier::AwContentsLifecycleNotifier() = default; AwContentsLifecycleNotifier::AwContentsLifecycleNotifier() = default;
AwContentsLifecycleNotifier::~AwContentsLifecycleNotifier() = default; AwContentsLifecycleNotifier::~AwContentsLifecycleNotifier() = default;
...@@ -177,9 +184,8 @@ bool AwContentsLifecycleNotifier::HasAwContentsInstance() const { ...@@ -177,9 +184,8 @@ bool AwContentsLifecycleNotifier::HasAwContentsInstance() const {
AwContentsLifecycleNotifier::AwContentsData* AwContentsLifecycleNotifier::AwContentsData*
AwContentsLifecycleNotifier::GetAwContentsData(const AwContents* aw_contents) { AwContentsLifecycleNotifier::GetAwContentsData(const AwContents* aw_contents) {
uint64_t id = reinterpret_cast<uint64_t>(aw_contents); DCHECK(aw_contents_to_data_.find(aw_contents) != aw_contents_to_data_.end());
DCHECK(aw_contents_id_to_data_.find(id) != aw_contents_id_to_data_.end()); return &aw_contents_to_data_.at(aw_contents);
return &aw_contents_id_to_data_.at(id);
} }
} // namespace android_webview } // namespace android_webview
...@@ -44,6 +44,8 @@ class AwContentsLifecycleNotifier { ...@@ -44,6 +44,8 @@ class AwContentsLifecycleNotifier {
return has_aw_contents_ever_created_; return has_aw_contents_ever_created_;
} }
std::vector<const AwContents*> GetAllAwContents() const;
private: private:
struct AwContentsData { struct AwContentsData {
AwContentsData(); AwContentsData();
...@@ -75,9 +77,9 @@ class AwContentsLifecycleNotifier { ...@@ -75,9 +77,9 @@ class AwContentsLifecycleNotifier {
AwContentsLifecycleNotifier::AwContentsData* GetAwContentsData( AwContentsLifecycleNotifier::AwContentsData* GetAwContentsData(
const AwContents* aw_contents); const AwContents* aw_contents);
// The AwContentId to AwContentsData mapping. // The AwContents to AwContentsData mapping.
std::map<uint64_t, AwContentsLifecycleNotifier::AwContentsData> std::map<const AwContents*, AwContentsLifecycleNotifier::AwContentsData>
aw_contents_id_to_data_; aw_contents_to_data_;
// The number of AwContents instances in each AwContentsState. // The number of AwContents instances in each AwContentsState.
int state_count_[3]{}; int state_count_[3]{};
......
...@@ -258,4 +258,28 @@ TEST_F(AwContentsLifecycleNotifierTest, DetachFromVisibleWindow) { ...@@ -258,4 +258,28 @@ TEST_F(AwContentsLifecycleNotifierTest, DetachFromVisibleWindow) {
ASSERT_TRUE(HasAwContentsEverCreated()); ASSERT_TRUE(HasAwContentsEverCreated());
} }
TEST_F(AwContentsLifecycleNotifierTest, GetAllAwContents) {
std::vector<const AwContents*> all_aw_contents(
notifier()->GetAllAwContents());
ASSERT_TRUE(all_aw_contents.empty());
const AwContents* fake_aw_contents = reinterpret_cast<const AwContents*>(1);
notifier()->OnWebViewCreated(fake_aw_contents);
all_aw_contents = notifier()->GetAllAwContents();
ASSERT_EQ(all_aw_contents.size(), 1u);
ASSERT_EQ(all_aw_contents.back(), fake_aw_contents);
const AwContents* fake_aw_contents2 = reinterpret_cast<const AwContents*>(2);
notifier()->OnWebViewCreated(fake_aw_contents2);
all_aw_contents = notifier()->GetAllAwContents();
ASSERT_EQ(all_aw_contents.size(), 2u);
ASSERT_EQ(all_aw_contents.front(), fake_aw_contents);
ASSERT_EQ(all_aw_contents.back(), fake_aw_contents2);
notifier()->OnWebViewDestroyed(fake_aw_contents);
all_aw_contents = notifier()->GetAllAwContents();
ASSERT_EQ(all_aw_contents.size(), 1u);
ASSERT_EQ(all_aw_contents.back(), fake_aw_contents2);
notifier()->OnWebViewDestroyed(fake_aw_contents2);
all_aw_contents = notifier()->GetAllAwContents();
ASSERT_TRUE(all_aw_contents.empty());
}
} // namespace android_webview } // namespace android_webview
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/page_load_metrics/aw_page_load_metrics_provider.h"
#include "android_webview/browser/aw_contents.h"
#include "android_webview/browser/lifecycle/aw_contents_lifecycle_notifier.h"
#include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
namespace android_webview {
AwPageLoadMetricsProvider::AwPageLoadMetricsProvider() = default;
AwPageLoadMetricsProvider::~AwPageLoadMetricsProvider() = default;
void AwPageLoadMetricsProvider::OnAppEnterBackground() {
std::vector<const AwContents*> all_aw_contents(
AwContentsLifecycleNotifier::GetInstance().GetAllAwContents());
for (auto* aw_contents : all_aw_contents) {
page_load_metrics::MetricsWebContentsObserver* observer =
page_load_metrics::MetricsWebContentsObserver::FromWebContents(
aw_contents->web_contents());
if (observer)
observer->FlushMetricsOnAppEnterBackground();
}
}
} // namespace android_webview
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_WEBVIEW_BROWSER_PAGE_LOAD_METRICS_AW_PAGE_LOAD_METRICS_PROVIDER_H_
#define ANDROID_WEBVIEW_BROWSER_PAGE_LOAD_METRICS_AW_PAGE_LOAD_METRICS_PROVIDER_H_
#include "base/macros.h"
#include "components/metrics/metrics_provider.h"
namespace android_webview {
// MetricsProvider that interfaces with page_load_metrics.
class AwPageLoadMetricsProvider : public metrics::MetricsProvider {
public:
AwPageLoadMetricsProvider();
~AwPageLoadMetricsProvider() override;
// metrics:MetricsProvider:
void OnAppEnterBackground() override;
private:
DISALLOW_COPY_AND_ASSIGN(AwPageLoadMetricsProvider);
};
} // namespace android_webview
#endif // ANDROID_WEBVIEW_BROWSER_PAGE_LOAD_METRICS_AW_PAGE_LOAD_METRICS_PROVIDER_H_
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment