1// Copyright 2014 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
5#include "chrome/browser/chromeos/login/ui/captive_portal_view.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/chromeos/login/ui/captive_portal_window_proxy.h"
9#include "chrome/grit/generated_resources.h"
10#include "chromeos/network/network_handler.h"
11#include "chromeos/network/network_state.h"
12#include "chromeos/network/network_state_handler.h"
13#include "components/captive_portal/captive_portal_detector.h"
14#include "content/public/browser/web_contents.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/views/window/dialog_delegate.h"
17#include "url/gurl.h"
18
19namespace {
20
21const char* CaptivePortalStartURL() {
22  return captive_portal::CaptivePortalDetector::kDefaultURL;
23}
24
25}  // namespace
26
27namespace chromeos {
28
29CaptivePortalView::CaptivePortalView(Profile* profile,
30                                     CaptivePortalWindowProxy* proxy)
31    : SimpleWebViewDialog(profile),
32      proxy_(proxy),
33      redirected_(false) {
34}
35
36CaptivePortalView::~CaptivePortalView() {
37}
38
39void CaptivePortalView::StartLoad() {
40  SimpleWebViewDialog::StartLoad(GURL(CaptivePortalStartURL()));
41}
42
43bool CaptivePortalView::CanResize() const {
44  return false;
45}
46
47ui::ModalType CaptivePortalView::GetModalType() const {
48  return ui::MODAL_TYPE_SYSTEM;
49}
50
51base::string16 CaptivePortalView::GetWindowTitle() const {
52  base::string16 network_name;
53  const NetworkState* default_network =
54      NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
55  std::string default_network_name =
56      default_network ? default_network->name() : std::string();
57  if (!default_network_name.empty()) {
58    network_name = base::ASCIIToUTF16(default_network_name);
59  } else {
60    DLOG(ERROR)
61        << "No active/default network, but captive portal window is shown.";
62  }
63
64  return l10n_util::GetStringFUTF16(IDS_LOGIN_CAPTIVE_PORTAL_WINDOW_TITLE,
65                                    network_name);
66}
67
68bool CaptivePortalView::ShouldShowWindowTitle() const {
69  return true;
70}
71
72views::NonClientFrameView* CaptivePortalView::CreateNonClientFrameView(
73    views::Widget* widget) {
74  return views::DialogDelegate::CreateDialogFrameView(widget);
75}
76
77void CaptivePortalView::NavigationStateChanged(
78    const content::WebContents* source,
79    content::InvalidateTypes changed_flags) {
80  SimpleWebViewDialog::NavigationStateChanged(source, changed_flags);
81
82  // Naive way to determine the redirection. This won't be needed after portal
83  // detection will be done on the Chrome side.
84  GURL url = source->GetLastCommittedURL();
85  // Note, |url| will be empty for "client3.google.com/generate_204" page.
86  if (!redirected_  && url != GURL::EmptyGURL() &&
87      url != GURL(CaptivePortalStartURL())) {
88    redirected_ = true;
89    proxy_->OnRedirected();
90  }
91}
92
93void CaptivePortalView::LoadingStateChanged(content::WebContents* source,
94    bool to_different_document) {
95  SimpleWebViewDialog::LoadingStateChanged(source, to_different_document);
96  // TODO(nkostylev): Fix case of no connectivity, check HTTP code returned.
97  // Disable this heuristic as it has false positives.
98  // Relying on just shill portal check to close dialog is fine.
99  // if (!is_loading && !redirected_)
100  //   proxy_->OnOriginalURLLoaded();
101}
102
103}  // namespace chromeos
104