network_login_observer.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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/network_login_observer.h"
6
7#include "chrome/browser/browser_list.h"
8#include "chrome/browser/browser_window.h"
9#include "chrome/browser/chromeos/cros/cros_library.h"
10#include "chrome/browser/chromeos/cros/network_library.h"
11#include "chrome/browser/chromeos/login/background_view.h"
12#include "chrome/browser/chromeos/login/login_utils.h"
13#include "chrome/browser/chromeos/options/network_config_view.h"
14#include "chrome/browser/ui/browser.h"
15#include "chrome/browser/ui/views/window.h"
16#include "views/window/dialog_delegate.h"
17#include "views/window/window.h"
18
19namespace chromeos {
20
21NetworkLoginObserver::NetworkLoginObserver(NetworkLibrary* netlib) {
22  RefreshStoredNetworks(netlib->wifi_networks());
23  netlib->AddNetworkManagerObserver(this);
24}
25
26NetworkLoginObserver::~NetworkLoginObserver() {
27  CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
28}
29
30void NetworkLoginObserver::CreateModalPopup(views::WindowDelegate* view) {
31  Browser* browser = BrowserList::GetLastActive();
32  if (browser && browser->type() != Browser::TYPE_NORMAL) {
33    browser = BrowserList::FindBrowserWithType(browser->profile(),
34                                               Browser::TYPE_NORMAL,
35                                               true);
36  }
37  if (browser) {
38    views::Window* window = browser::CreateViewsWindow(
39        browser->window()->GetNativeHandle(), gfx::Rect(), view);
40    window->SetIsAlwaysOnTop(true);
41    window->Show();
42  } else {
43    // Browser not found, so we should be in login/oobe screen.
44    BackgroundView* background_view = LoginUtils::Get()->GetBackgroundView();
45    if (background_view) {
46      background_view->CreateModalPopup(view);
47    }
48  }
49}
50
51void NetworkLoginObserver::RefreshStoredNetworks(
52    const WifiNetworkVector& wifi_networks) {
53  wifi_network_failures_.clear();
54  for (WifiNetworkVector::const_iterator it = wifi_networks.begin();
55       it < wifi_networks.end(); it++) {
56    const WifiNetwork* wifi = *it;
57    wifi_network_failures_[wifi->service_path()] = wifi->failed();
58  }
59}
60
61void NetworkLoginObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
62  const WifiNetworkVector& wifi_networks = obj->wifi_networks();
63
64  NetworkConfigView* view = NULL;
65  // Check to see if we have any newly failed wifi network.
66  for (WifiNetworkVector::const_iterator it = wifi_networks.begin();
67       it < wifi_networks.end(); it++) {
68    const WifiNetwork* wifi = *it;
69    if (wifi->failed()) {
70      WifiFailureMap::iterator iter =
71          wifi_network_failures_.find(wifi->service_path());
72      // If the network did not previously exist, then don't do anything.
73      // For example, if the user travels to a location and finds a service
74      // that has previously failed, we don't want to show an error.
75      if (iter == wifi_network_failures_.end())
76        continue;
77
78      // If this network was in a failed state previously, then it's not new.
79      if (iter->second)
80        continue;
81
82      // Display login box again for bad_passphrase and bad_wepkey errors.
83      if (wifi->error() == ERROR_BAD_PASSPHRASE ||
84          wifi->error() == ERROR_BAD_WEPKEY) {
85        // The NetworkConfigView will show the appropriate error message.
86        view = new NetworkConfigView(wifi);
87        // There should only be one wifi network that failed to connect.
88        // If for some reason, we have more than one failure,
89        // we only display the first one. So we break here.
90        break;
91      }
92    }
93  }
94
95  RefreshStoredNetworks(wifi_networks);
96
97  // Show login box if necessary.
98  if (view)
99    CreateModalPopup(view);
100}
101
102}  // namespace chromeos
103