// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_LOGIN_DETECTION_LOGIN_DETECTION_TAB_HELPER_H_
#define CHROME_BROWSER_LOGIN_DETECTION_LOGIN_DETECTION_TAB_HELPER_H_

#include "base/gtest_prod_util.h"
#include "chrome/browser/login_detection/oauth_login_detector.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "services/metrics/public/cpp/ukm_source_id.h"

namespace content {
class NavigationHandle;
class WebContents;
}  // namespace content

namespace login_detection {

// Observes the navigations and records login detection metrics.
class LoginDetectionTabHelper
    : public content::WebContentsObserver,
      public content::WebContentsUserData<LoginDetectionTabHelper> {
 public:
  static void MaybeCreateForWebContents(content::WebContents* web_contents);

  ~LoginDetectionTabHelper() override;
  LoginDetectionTabHelper(const LoginDetectionTabHelper&) = delete;
  LoginDetectionTabHelper& operator=(const LoginDetectionTabHelper&) = delete;

 private:
  friend class content::WebContentsUserData<LoginDetectionTabHelper>;
  FRIEND_TEST_ALL_PREFIXES(LoginDetectionTabHelperTest, PopUpOAuthLogin);

  explicit LoginDetectionTabHelper(content::WebContents* web_contents);

  // Indicates this WebContents was opened as a popup, and the opener
  // WebContents had the |opener_navigation_url|.
  void DidOpenAsPopUp(const GURL& opener_navigation_url);

  // content::WebContentsObserver.
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidOpenRequestedURL(content::WebContents* new_contents,
                           content::RenderFrameHost* source_render_frame_host,
                           const GURL& url,
                           const content::Referrer& referrer,
                           WindowOpenDisposition disposition,
                           ui::PageTransition transition,
                           bool started_from_context_menu,
                           bool renderer_initiated) override;
  void PrimaryPageChanged(content::Page& page) override;
  void WebContentsDestroyed() override;

  // Called when a new signed-in site has just been discovered from one of the
  // supported OAuth login flows.
  void ProcessNewSignedInSite(const GURL& signedin_site);

  // Detects successful OAuth login flows.
  std::unique_ptr<OAuthLoginDetector> oauth_login_detector_;

  // The UKM source id for the primary page. We may need to log something in
  // WebContentsDestroyed and retrieving the UKM source ID then would be too
  // late.
  ukm::SourceId ukm_source_id_;

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

}  // namespace login_detection

#endif  // CHROME_BROWSER_LOGIN_DETECTION_LOGIN_DETECTION_TAB_HELPER_H_
