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