summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_message_observer.cc
blob: 7892ecc008d77c8f1e77c046b32865bb3ff5bfd0 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2012 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/chromeos/network_message_observer.h"

#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/system/chromeos/network/network_observer.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/network_library.h"
#include "chrome/browser/chromeos/notifications/balloon_view_host_chromeos.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/time_format.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace {
ash::NetworkObserver::NetworkType GetAshNetworkType(
    chromeos::ConnectionType connection_type) {
  switch (connection_type) {
   case chromeos::TYPE_CELLULAR:
     return ash::NetworkObserver::NETWORK_CELLULAR;
   case chromeos::TYPE_ETHERNET:
     return ash::NetworkObserver::NETWORK_ETHERNET;
   case chromeos::TYPE_WIFI:
     return ash::NetworkObserver::NETWORK_WIFI;
   case chromeos::TYPE_BLUETOOTH:
     return ash::NetworkObserver::NETWORK_BLUETOOTH;
   default:
     return ash::NetworkObserver::NETWORK_UNKNOWN;
  }
}

}  // namespace

namespace chromeos {

class NetworkMessageNotification : public ash::NetworkTrayDelegate {
 public:
  NetworkMessageNotification(Profile* profile,
                             ash::NetworkObserver::MessageType error_type)
      : error_type_(error_type) {
    switch (error_type) {
      case ash::NetworkObserver::ERROR_CONNECT_FAILED:
        title_ = l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE);
        return;
      case ash::NetworkObserver::MESSAGE_DATA_PROMO:
        NOTREACHED();
        return;
    }
    NOTREACHED();
  }

  // Overridden from ash::NetworkTrayDelegate:
  virtual void NotificationLinkClicked(size_t index) OVERRIDE {
    base::ListValue empty_value;
    if (!callback_.is_null())
      callback_.Run(&empty_value);
  }

  void Hide() {
    ash::Shell::GetInstance()->system_tray_notifier()->
        NotifyClearNetworkMessage(error_type_);
  }

  void SetTitle(const string16& title) {
    title_ = title;
  }

  void Show(chromeos::ConnectionType connection_type,
            const string16& message,
            const string16& link_text,
            const BalloonViewHost::MessageCallback& callback,
            bool urgent, bool sticky) {
    callback_ = callback;
    std::vector<string16> links;
    links.push_back(link_text);
    ash::NetworkObserver::NetworkType network_type = GetAshNetworkType(
        connection_type);
    ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage(
        this, error_type_, network_type, title_, message, links);
  }

  void ShowAlways(chromeos::ConnectionType connection_type,
                  const string16& message,
                  const string16& link_text,
                  const BalloonViewHost::MessageCallback& callback,
                  bool urgent, bool sticky) {
    Show(connection_type, message, link_text, callback, urgent, sticky);
  }

 private:
  string16 title_;
  ash::NetworkObserver::MessageType error_type_;
  BalloonViewHost::MessageCallback callback_;
};

NetworkMessageObserver::NetworkMessageObserver(Profile* profile) {
  notification_connection_error_.reset(
      new NetworkMessageNotification(
          profile, ash::NetworkObserver::ERROR_CONNECT_FAILED));
  NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
  OnNetworkManagerChanged(netlib);
  // Note that this gets added as a NetworkManagerObserver
  // and UserActionObserver in startup_browser_creator.cc
}

NetworkMessageObserver::~NetworkMessageObserver() {
  NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
  netlib->RemoveNetworkManagerObserver(this);
  netlib->RemoveUserActionObserver(this);
  notification_connection_error_->Hide();
}

void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* cros) {
  const Network* new_failed_network = NULL;
  // Check to see if we have any newly failed networks.
  for (WifiNetworkVector::const_iterator it = cros->wifi_networks().begin();
       it != cros->wifi_networks().end(); it++) {
    const WifiNetwork* net = *it;
    if (net->notify_failure()) {
      new_failed_network = net;
      break;  // There should only be one failed network.
    }
  }

  if (!new_failed_network) {
    for (WimaxNetworkVector::const_iterator it = cros->wimax_networks().begin();
         it != cros->wimax_networks().end(); ++it) {
      const WimaxNetwork* net = *it;
      if (net->notify_failure()) {
        new_failed_network = net;
        break;  // There should only be one failed network.
      }
    }
  }

  if (!new_failed_network) {
    for (CellularNetworkVector::const_iterator it =
             cros->cellular_networks().begin();
         it != cros->cellular_networks().end(); it++) {
      const CellularNetwork* net = *it;
      if (net->notify_failure()) {
        new_failed_network = net;
        break;  // There should only be one failed network.
      }
    }
  }

  if (!new_failed_network) {
    for (VirtualNetworkVector::const_iterator it =
             cros->virtual_networks().begin();
         it != cros->virtual_networks().end(); it++) {
      const VirtualNetwork* net = *it;
      if (net->notify_failure()) {
        new_failed_network = net;
        break;  // There should only be one failed network.
      }
    }
  }

  // Show connection error notification if necessary.
  if (new_failed_network) {
    VLOG(1) << "Failed Network: " << new_failed_network->service_path();
    notification_connection_error_->ShowAlways(
        new_failed_network->type(),
        l10n_util::GetStringFUTF16(
            IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS,
            UTF8ToUTF16(new_failed_network->name()),
            UTF8ToUTF16(new_failed_network->GetErrorString())),
        string16(), BalloonViewHost::MessageCallback(), false, false);
  }
}

void NetworkMessageObserver::OnConnectionInitiated(NetworkLibrary* cros,
                                                   const Network* network) {
  // If user initiated any network connection, we hide the error notification.
  notification_connection_error_->Hide();
}

}  // namespace chromeos