summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/signin/reauth_tab_helper.cc
blob: e8bd1d8b1c87b3d9e761a45b414081bf8470cec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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 "chrome/browser/signin/reauth_tab_helper.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/signin/reauth_result.h"
#include "content/public/browser/navigation_handle.h"
#include "net/http/http_status_code.h"
#include "url/origin.h"

namespace signin {

namespace {

bool IsExpectedResponseCode(int response_code) {
  return response_code == net::HTTP_OK || response_code == net::HTTP_NO_CONTENT;
}

}  // namespace

// static
void ReauthTabHelper::CreateForWebContents(content::WebContents* web_contents,
                                           const GURL& reauth_url,
                                           ReauthCallback callback) {
  DCHECK(web_contents);
  if (!FromWebContents(web_contents)) {
    web_contents->SetUserData(
        UserDataKey(), base::WrapUnique(new ReauthTabHelper(
                           web_contents, reauth_url, std::move(callback))));
  } else {
    std::move(callback).Run(signin::ReauthResult::kCancelled);
  }
}

ReauthTabHelper::~ReauthTabHelper() = default;

void ReauthTabHelper::CompleteReauth(signin::ReauthResult result) {
  if (callback_)
    std::move(callback_).Run(result);
}

void ReauthTabHelper::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  if (!navigation_handle->IsInPrimaryMainFrame())
    return;

  is_within_reauth_origin_ &=
      url::IsSameOriginWith(reauth_url_, navigation_handle->GetURL());

  if (navigation_handle->IsErrorPage()) {
    has_last_committed_error_page_ = true;
    return;
  }

  has_last_committed_error_page_ = false;

  GURL::Replacements replacements;
  replacements.ClearQuery();
  GURL url_without_query =
      navigation_handle->GetURL().ReplaceComponents(replacements);
  if (url_without_query != reauth_url_)
    return;

  if (!navigation_handle->GetResponseHeaders() ||
      !IsExpectedResponseCode(
          navigation_handle->GetResponseHeaders()->response_code())) {
    CompleteReauth(signin::ReauthResult::kUnexpectedResponse);
  }

  CompleteReauth(signin::ReauthResult::kSuccess);
}

void ReauthTabHelper::WebContentsDestroyed() {
  CompleteReauth(signin::ReauthResult::kDismissedByUser);
}

bool ReauthTabHelper::is_within_reauth_origin() {
  return is_within_reauth_origin_;
}

bool ReauthTabHelper::has_last_committed_error_page() {
  return has_last_committed_error_page_;
}

ReauthTabHelper::ReauthTabHelper(content::WebContents* web_contents,
                                 const GURL& reauth_url,
                                 ReauthCallback callback)
    : content::WebContentsUserData<ReauthTabHelper>(*web_contents),
      content::WebContentsObserver(web_contents),
      reauth_url_(reauth_url),
      callback_(std::move(callback)) {}

WEB_CONTENTS_USER_DATA_KEY_IMPL(ReauthTabHelper);

}  // namespace signin