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 "chromeos/network/network_handler.h" 10 #include "chromeos/network/network_state.h" 11 #include "chromeos/network/network_state_handler.h" 12 #include "components/captive_portal/captive_portal_detector.h" 13 #include "content/public/browser/web_contents.h" 14 #include "grit/generated_resources.h" 15 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/views/window/dialog_delegate.h" 17 #include "url/gurl.h" 18 19 namespace { 20 21 const char* CaptivePortalStartURL() { 22 return captive_portal::CaptivePortalDetector::kDefaultURL; 23 } 24 25 } // namespace 26 27 namespace chromeos { 28 29 CaptivePortalView::CaptivePortalView(Profile* profile, 30 CaptivePortalWindowProxy* proxy) 31 : SimpleWebViewDialog(profile), 32 proxy_(proxy), 33 redirected_(false) { 34 } 35 36 CaptivePortalView::~CaptivePortalView() { 37 } 38 39 void CaptivePortalView::StartLoad() { 40 SimpleWebViewDialog::StartLoad(GURL(CaptivePortalStartURL())); 41 } 42 43 bool CaptivePortalView::CanResize() const { 44 return false; 45 } 46 47 ui::ModalType CaptivePortalView::GetModalType() const { 48 return ui::MODAL_TYPE_SYSTEM; 49 } 50 51 base::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 68 bool CaptivePortalView::ShouldShowWindowTitle() const { 69 return true; 70 } 71 72 views::NonClientFrameView* CaptivePortalView::CreateNonClientFrameView( 73 views::Widget* widget) { 74 return views::DialogDelegate::CreateDialogFrameView(widget); 75 } 76 77 void CaptivePortalView::NavigationStateChanged( 78 const content::WebContents* source, unsigned changed_flags) { 79 SimpleWebViewDialog::NavigationStateChanged(source, changed_flags); 80 81 // Naive way to determine the redirection. This won't be needed after portal 82 // detection will be done on the Chrome side. 83 GURL url = source->GetLastCommittedURL(); 84 // Note, |url| will be empty for "client3.google.com/generate_204" page. 85 if (!redirected_ && url != GURL::EmptyGURL() && 86 url != GURL(CaptivePortalStartURL())) { 87 redirected_ = true; 88 proxy_->OnRedirected(); 89 } 90 } 91 92 void CaptivePortalView::LoadingStateChanged(content::WebContents* source, 93 bool to_different_document) { 94 SimpleWebViewDialog::LoadingStateChanged(source, to_different_document); 95 // TODO(nkostylev): Fix case of no connectivity, check HTTP code returned. 96 // Disable this heuristic as it has false positives. 97 // Relying on just shill portal check to close dialog is fine. 98 // if (!is_loading && !redirected_) 99 // proxy_->OnOriginalURLLoaded(); 100 } 101 102 } // namespace chromeos 103