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 "net/base/net_util.h" |
| 10 #include "webkit/plugins/ppapi/resource_helper.h" |
| 11 |
| 12 namespace webkit { |
| 13 namespace ppapi { |
| 14 |
| 15 PPB_NetworkMonitor_Private_Impl::PPB_NetworkMonitor_Private_Impl( |
| 16 PP_Instance instance, |
| 17 PPB_NetworkMonitor_Callback callback, |
| 18 void* user_data) |
| 19 : Resource(::ppapi::OBJECT_IS_IMPL, instance), |
| 20 callback_(callback), |
| 21 user_data_(user_data), |
| 22 started_(false) { |
| 23 } |
| 24 |
| 25 PPB_NetworkMonitor_Private_Impl::~PPB_NetworkMonitor_Private_Impl() { |
| 26 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
| 27 if (plugin_delegate && started_) { |
| 28 plugin_delegate->RemoveNetworkListObserver(this); |
| 29 } |
| 30 } |
| 31 |
| 32 // static |
| 33 PP_Resource PPB_NetworkMonitor_Private_Impl::Create( |
| 34 PP_Instance instance, |
| 35 PPB_NetworkMonitor_Callback callback, |
| 36 void* user_data) { |
| 37 scoped_refptr<PPB_NetworkMonitor_Private_Impl> result( |
| 38 new PPB_NetworkMonitor_Private_Impl(instance, callback, user_data)); |
| 39 if (!result->Start()) |
| 40 return 0; |
| 41 return result->GetReference(); |
| 42 } |
| 43 |
| 44 ::ppapi::thunk::PPB_NetworkMonitor_Private_API* |
| 45 PPB_NetworkMonitor_Private_Impl::AsPPB_NetworkMonitor_Private_API() { |
| 46 return this; |
| 47 } |
| 48 |
| 49 bool PPB_NetworkMonitor_Private_Impl::Start() { |
| 50 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
| 51 if (!plugin_delegate) |
| 52 return false; |
| 53 started_ = plugin_delegate->AddNetworkListObserver(this); |
| 54 return started_; |
| 55 } |
| 56 |
| 57 void PPB_NetworkMonitor_Private_Impl::OnNetworkListChanged( |
| 58 const net::NetworkInterfaceList& list) { |
| 59 scoped_ptr< ::ppapi::PPB_NetworkList_Private_Shared::NetworkList> list_copy( |
| 60 new ::ppapi::PPB_NetworkList_Private_Shared::NetworkList(list.size())); |
| 61 for (size_t i = 0; i < list.size(); ++i) { |
| 62 ::ppapi::PPB_NetworkList_Private_Shared::NetworkInfo& network = |
| 63 list_copy->at(i); |
| 64 network.name = list[i].name; |
| 65 |
| 66 network.addresses.resize(1); |
| 67 CHECK_LE(list[i].address.size(), sizeof(network.addresses[0].data)); |
| 68 network.addresses[0].size = list[i].address.size(); |
| 69 memcpy(network.addresses[0].data, &(list[i].address.front()), |
| 70 list[i].address.size()); |
| 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 PP_Resource list_resource = |
| 81 ::ppapi::PPB_NetworkList_Private_Shared::Create( |
| 82 ::ppapi::OBJECT_IS_IMPL, pp_instance(), list_copy.Pass()); |
| 83 callback_(user_data_, list_resource); |
| 84 } |
| 85 |
| 86 } // namespace ppapi |
| 87 } // namespace webkit |
OLD | NEW |