blob: dbe75beab8b78caa96f636e006c072444897024f [file] [log] [blame]
// 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 IOS_CHROME_BROWSER_METRICS_PAGELOAD_FOREGROUND_DURATION_TAB_HELPER_H_
#define IOS_CHROME_BROWSER_METRICS_PAGELOAD_FOREGROUND_DURATION_TAB_HELPER_H_
#include <memory>
#include <unordered_map>
#include "base/scoped_observer.h"
#include "base/time/time.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_observer.h"
#import "ios/web/public/web_state_user_data.h"
// Tracks the time spent on pages visible on the screen and logs them as UKMs.
class PageloadForegroundDurationTabHelper
: public web::WebStateUserData<PageloadForegroundDurationTabHelper>,
web::WebStateObserver {
public:
~PageloadForegroundDurationTabHelper() override;
private:
WEB_STATE_USER_DATA_KEY_DECL();
friend class web::WebStateUserData<PageloadForegroundDurationTabHelper>;
explicit PageloadForegroundDurationTabHelper(web::WebState* web_state);
// web::WebStateObserver override.
void WasShown(web::WebState* web_state) override;
void WasHidden(web::WebState* web_state) override;
void DidStartNavigation(web::WebState* web_state,
web::NavigationContext* navigation_context) override;
void DidFinishNavigation(web::WebState* web_state,
web::NavigationContext* navigation_context) override;
void RenderProcessGone(web::WebState* web_state) override;
void WebStateDestroyed(web::WebState* web_state) override;
// Indicates to this tab helper that the app has entered a foreground state.
void UpdateForAppWillForeground();
// Indicates to this tab helper that the app has entered a background state.
void UpdateForAppDidBackground();
// End recording and log a UKM if necessary.
void RecordUkmIfInForeground();
// Whether recording is happening for time spent on the current page.
bool currently_recording_ = false;
// Last time when recording started.
base::TimeTicks last_time_shown_;
// WebState reference.
web::WebState* web_state_ = nullptr;
// Scoped observer that facilitates observing the WebState.
ScopedObserver<web::WebState, WebStateObserver> scoped_observer_;
// Holds references to background NSNotification callback observer.
id foreground_notification_observer_;
// Holds references to foreground NSNotification callback observer.
id background_notification_observer_;
};
#endif // IOS_CHROME_BROWSER_METRICS_PAGELOAD_FOREGROUND_DURATION_TAB_HELPER_H_