| OLD | NEW |
| (Empty) |
| 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 "webkit/plugins/ppapi/ppb_network_monitor_private_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "ppapi/shared_impl/ppb_network_list_private_shared.h" | |
| 9 #include "ppapi/shared_impl/private/net_address_private_impl.h" | |
| 10 #include "net/base/ip_endpoint.h" | |
| 11 #include "net/base/net_util.h" | |
| 12 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 13 | |
| 14 namespace webkit { | |
| 15 namespace ppapi { | |
| 16 | |
| 17 PPB_NetworkMonitor_Private_Impl::PPB_NetworkMonitor_Private_Impl( | |
| 18 PP_Instance instance, | |
| 19 PPB_NetworkMonitor_Callback callback, | |
| 20 void* user_data) | |
| 21 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | |
| 22 callback_(callback), | |
| 23 user_data_(user_data), | |
| 24 started_(false) { | |
| 25 } | |
| 26 | |
| 27 PPB_NetworkMonitor_Private_Impl::~PPB_NetworkMonitor_Private_Impl() { | |
| 28 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 29 if (plugin_delegate && started_) { | |
| 30 plugin_delegate->RemoveNetworkListObserver(this); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 // static | |
| 35 PP_Resource PPB_NetworkMonitor_Private_Impl::Create( | |
| 36 PP_Instance instance, | |
| 37 PPB_NetworkMonitor_Callback callback, | |
| 38 void* user_data) { | |
| 39 scoped_refptr<PPB_NetworkMonitor_Private_Impl> result( | |
| 40 new PPB_NetworkMonitor_Private_Impl(instance, callback, user_data)); | |
| 41 if (!result->Start()) | |
| 42 return 0; | |
| 43 return result->GetReference(); | |
| 44 } | |
| 45 | |
| 46 ::ppapi::thunk::PPB_NetworkMonitor_Private_API* | |
| 47 PPB_NetworkMonitor_Private_Impl::AsPPB_NetworkMonitor_Private_API() { | |
| 48 return this; | |
| 49 } | |
| 50 | |
| 51 bool PPB_NetworkMonitor_Private_Impl::Start() { | |
| 52 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 53 if (!plugin_delegate) | |
| 54 return false; | |
| 55 started_ = plugin_delegate->AddNetworkListObserver(this); | |
| 56 return started_; | |
| 57 } | |
| 58 | |
| 59 void PPB_NetworkMonitor_Private_Impl::OnNetworkListChanged( | |
| 60 const net::NetworkInterfaceList& list) { | |
| 61 ::ppapi::NetworkList list_copy(list.size()); | |
| 62 for (size_t i = 0; i < list.size(); ++i) { | |
| 63 ::ppapi::NetworkInfo& network = list_copy.at(i); | |
| 64 network.name = list[i].name; | |
| 65 | |
| 66 network.addresses.resize( | |
| 67 1, ::ppapi::NetAddressPrivateImpl::kInvalidNetAddress); | |
| 68 bool result = ::ppapi::NetAddressPrivateImpl::IPEndPointToNetAddress( | |
| 69 list[i].address, 0, &(network.addresses[0])); | |
| 70 DCHECK(result); | |
| 71 | |
| 72 // TODO(sergeyu): Currently net::NetworkInterfaceList provides | |
| 73 // only name and one IP address. Add all other fields and copy | |
| 74 // them here. | |
| 75 network.type = PP_NETWORKLIST_UNKNOWN; | |
| 76 network.state = PP_NETWORKLIST_UP; | |
| 77 network.display_name = list[i].name; | |
| 78 network.mtu = 0; | |
| 79 } | |
| 80 scoped_refptr< ::ppapi::NetworkListStorage> list_storage( | |
| 81 new ::ppapi::NetworkListStorage(list_copy)); | |
| 82 PP_Resource list_resource = | |
| 83 ::ppapi::PPB_NetworkList_Private_Shared::Create( | |
| 84 ::ppapi::OBJECT_IS_IMPL, pp_instance(), list_storage); | |
| 85 callback_(user_data_, list_resource); | |
| 86 } | |
| 87 | |
| 88 } // namespace ppapi | |
| 89 } // namespace webkit | |
| OLD | NEW |