OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_network_monitor_private_proxy.h" | 5 #include "ppapi/proxy/ppb_network_monitor_private_proxy.h" |
6 | 6 |
7 #include "ppapi/proxy/enter_proxy.h" | 7 #include "ppapi/proxy/enter_proxy.h" |
8 #include "ppapi/proxy/plugin_proxy_delegate.h" | 8 #include "ppapi/proxy/plugin_proxy_delegate.h" |
9 #include "ppapi/proxy/ppapi_messages.h" | 9 #include "ppapi/proxy/ppapi_messages.h" |
| 10 #include "ppapi/shared_impl/proxy_lock.h" |
10 #include "ppapi/thunk/ppb_network_monitor_private_api.h" | 11 #include "ppapi/thunk/ppb_network_monitor_private_api.h" |
11 | 12 |
12 namespace ppapi { | 13 namespace ppapi { |
13 namespace proxy { | 14 namespace proxy { |
14 | 15 |
15 class PPB_NetworkMonitor_Private_Proxy::NetworkMonitor | 16 class PPB_NetworkMonitor_Private_Proxy::NetworkMonitor |
16 : public Resource, | 17 : public Resource, |
17 public thunk::PPB_NetworkMonitor_Private_API, | 18 public thunk::PPB_NetworkMonitor_Private_API, |
18 public base::SupportsWeakPtr< | 19 public base::SupportsWeakPtr< |
19 PPB_NetworkMonitor_Private_Proxy::NetworkMonitor> { | 20 PPB_NetworkMonitor_Private_Proxy::NetworkMonitor> { |
(...skipping 12 matching lines...) Expand all Loading... |
32 proxy_->OnNetworkMonitorDeleted(this, pp_instance()); | 33 proxy_->OnNetworkMonitorDeleted(this, pp_instance()); |
33 } | 34 } |
34 | 35 |
35 | 36 |
36 // Resource overrides. | 37 // Resource overrides. |
37 virtual ppapi::thunk::PPB_NetworkMonitor_Private_API* | 38 virtual ppapi::thunk::PPB_NetworkMonitor_Private_API* |
38 AsPPB_NetworkMonitor_Private_API() OVERRIDE { | 39 AsPPB_NetworkMonitor_Private_API() OVERRIDE { |
39 return this; | 40 return this; |
40 } | 41 } |
41 | 42 |
42 void OnNetworkListReceived(const scoped_refptr<NetworkListStorage>& list) { | 43 // This is invoked when a network list is received for this monitor (either |
| 44 // initially or on a change). It acquires the ProxyLock inside because |
| 45 // ObserverListThreadSafe does not support Bind/Closure, otherwise we would |
| 46 // wrap the call with a lock using RunWhileLocked. |
| 47 void OnNetworkListReceivedLocks( |
| 48 const scoped_refptr<NetworkListStorage>& list) { |
| 49 ProxyAutoLock lock; |
43 PP_Resource list_resource = | 50 PP_Resource list_resource = |
44 PPB_NetworkList_Private_Shared::Create( | 51 PPB_NetworkList_Private_Shared::Create( |
45 OBJECT_IS_PROXY, pp_instance(), list); | 52 OBJECT_IS_PROXY, pp_instance(), list); |
46 CallWhileUnlocked(callback_, user_data_, list_resource); | 53 CallWhileUnlocked(callback_, user_data_, list_resource); |
47 } | 54 } |
48 | 55 |
49 private: | 56 private: |
50 PPB_NetworkMonitor_Private_Proxy* proxy_; | 57 PPB_NetworkMonitor_Private_Proxy* proxy_; |
51 PPB_NetworkMonitor_Callback callback_; | 58 PPB_NetworkMonitor_Callback callback_; |
52 void* user_data_; | 59 void* user_data_; |
(...skipping 10 matching lines...) Expand all Loading... |
63 | 70 |
64 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() { | 71 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() { |
65 monitors_->AssertEmpty(); | 72 monitors_->AssertEmpty(); |
66 } | 73 } |
67 | 74 |
68 // static | 75 // static |
69 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource( | 76 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource( |
70 PP_Instance instance, | 77 PP_Instance instance, |
71 PPB_NetworkMonitor_Callback callback, | 78 PPB_NetworkMonitor_Callback callback, |
72 void* user_data) { | 79 void* user_data) { |
| 80 // TODO(dmichael): Check that this thread has a valid message loop associated |
| 81 // with it. |
73 if (!callback) | 82 if (!callback) |
74 return 0; | 83 return 0; |
75 | 84 |
76 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 85 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
77 if (!dispatcher) | 86 if (!dispatcher) |
78 return 0; | 87 return 0; |
79 PPB_NetworkMonitor_Private_Proxy* proxy = | 88 PPB_NetworkMonitor_Private_Proxy* proxy = |
80 static_cast<PPB_NetworkMonitor_Private_Proxy*>( | 89 static_cast<PPB_NetworkMonitor_Private_Proxy*>( |
81 dispatcher->GetInterfaceProxy(kApiID)); | 90 dispatcher->GetInterfaceProxy(kApiID)); |
82 if (!proxy) | 91 if (!proxy) |
83 return 0; | 92 return 0; |
84 | 93 |
85 scoped_refptr<NetworkMonitor> result( | 94 scoped_refptr<NetworkMonitor> result( |
86 new NetworkMonitor(instance, proxy, callback, user_data)); | 95 new NetworkMonitor(instance, proxy, callback, user_data)); |
87 proxy->monitors_->AddObserver(result.get()); | 96 proxy->monitors_->AddObserver(result.get()); |
88 | 97 |
89 proxy->monitors_count_++; | 98 proxy->monitors_count_++; |
90 if (proxy->monitors_count_ == 1) { | 99 if (proxy->monitors_count_ == 1) { |
91 // If that is the first network monitor then send Start message. | 100 // If that is the first network monitor then send Start message. |
92 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( | 101 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( |
93 new PpapiHostMsg_PPBNetworkMonitor_Start( | 102 new PpapiHostMsg_PPBNetworkMonitor_Start( |
94 dispatcher->plugin_dispatcher_id())); | 103 dispatcher->plugin_dispatcher_id())); |
95 | 104 |
96 // We could have received network list message after sending the | 105 // We could have received network list message after sending the |
97 // previous Stop message. This list is stale now, so reset it | 106 // previous Stop message. This list is stale now, so reset it |
98 // here. | 107 // here. |
99 proxy->current_list_ = NULL; | 108 proxy->current_list_ = NULL; |
100 } else if (proxy->current_list_.get()) { | 109 } else if (proxy->current_list_.get()) { |
101 MessageLoop::current()->PostTask(FROM_HERE, RunWhileLocked(base::Bind( | 110 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
102 &NetworkMonitor::OnNetworkListReceived, | 111 &NetworkMonitor::OnNetworkListReceivedLocks, |
103 result->AsWeakPtr(), proxy->current_list_))); | 112 result->AsWeakPtr(), proxy->current_list_)); |
104 } | 113 } |
105 | 114 |
106 return result->GetReference(); | 115 return result->GetReference(); |
107 } | 116 } |
108 | 117 |
109 bool PPB_NetworkMonitor_Private_Proxy::OnMessageReceived( | 118 bool PPB_NetworkMonitor_Private_Proxy::OnMessageReceived( |
110 const IPC::Message& msg) { | 119 const IPC::Message& msg) { |
111 bool handled = true; | 120 bool handled = true; |
112 IPC_BEGIN_MESSAGE_MAP(PPB_NetworkMonitor_Private_Proxy, msg) | 121 IPC_BEGIN_MESSAGE_MAP(PPB_NetworkMonitor_Private_Proxy, msg) |
113 IPC_MESSAGE_HANDLER(PpapiMsg_PPBNetworkMonitor_NetworkList, | 122 IPC_MESSAGE_HANDLER(PpapiMsg_PPBNetworkMonitor_NetworkList, |
114 OnPluginMsgNetworkList) | 123 OnPluginMsgNetworkList) |
115 IPC_MESSAGE_UNHANDLED(handled = false) | 124 IPC_MESSAGE_UNHANDLED(handled = false) |
116 IPC_END_MESSAGE_MAP() | 125 IPC_END_MESSAGE_MAP() |
117 return handled; | 126 return handled; |
118 } | 127 } |
119 | 128 |
120 void PPB_NetworkMonitor_Private_Proxy::OnPluginMsgNetworkList( | 129 void PPB_NetworkMonitor_Private_Proxy::OnPluginMsgNetworkList( |
121 uint32 plugin_dispatcher_id, | 130 uint32 plugin_dispatcher_id, |
122 const ppapi::NetworkList& list) { | 131 const ppapi::NetworkList& list) { |
123 scoped_refptr<NetworkListStorage> list_storage(new NetworkListStorage(list)); | 132 scoped_refptr<NetworkListStorage> list_storage(new NetworkListStorage(list)); |
124 current_list_ = list_storage; | 133 current_list_ = list_storage; |
125 monitors_->Notify(&NetworkMonitor::OnNetworkListReceived, list_storage); | 134 monitors_->Notify(&NetworkMonitor::OnNetworkListReceivedLocks, list_storage); |
126 } | 135 } |
127 | 136 |
128 void PPB_NetworkMonitor_Private_Proxy::OnNetworkMonitorDeleted( | 137 void PPB_NetworkMonitor_Private_Proxy::OnNetworkMonitorDeleted( |
129 NetworkMonitor* monitor, | 138 NetworkMonitor* monitor, |
130 PP_Instance instance) { | 139 PP_Instance instance) { |
131 monitors_->RemoveObserver(monitor); | 140 monitors_->RemoveObserver(monitor); |
132 monitors_count_--; | 141 monitors_count_--; |
133 if (monitors_count_ == 0) { | 142 if (monitors_count_ == 0) { |
134 // Send Stop message if that was the last NetworkMonitor. | 143 // Send Stop message if that was the last NetworkMonitor. |
135 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 144 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
136 if (dispatcher) { | 145 if (dispatcher) { |
137 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( | 146 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( |
138 new PpapiHostMsg_PPBNetworkMonitor_Stop( | 147 new PpapiHostMsg_PPBNetworkMonitor_Stop( |
139 dispatcher->plugin_dispatcher_id())); | 148 dispatcher->plugin_dispatcher_id())); |
140 } | 149 } |
141 current_list_ = NULL; | 150 current_list_ = NULL; |
142 } | 151 } |
143 } | 152 } |
144 | 153 |
145 } // namespace proxy | 154 } // namespace proxy |
146 } // namespace ppapi | 155 } // namespace ppapi |
OLD | NEW |