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/ppapi_messages.h" | 8 #include "ppapi/proxy/ppapi_messages.h" |
9 #include "ppapi/shared_impl/proxy_lock.h" | 9 #include "ppapi/shared_impl/proxy_lock.h" |
10 #include "ppapi/thunk/ppb_network_monitor_private_api.h" | 10 #include "ppapi/thunk/ppb_network_monitor_api.h" |
11 | 11 |
12 namespace ppapi { | 12 namespace ppapi { |
13 namespace proxy { | 13 namespace proxy { |
14 | 14 |
15 class PPB_NetworkMonitor_Private_Proxy::NetworkMonitor | 15 class PPB_NetworkMonitor_Private_Proxy::NetworkMonitor |
16 : public Resource, | 16 : public Resource, |
17 public thunk::PPB_NetworkMonitor_Private_API, | 17 public thunk::PPB_NetworkMonitor_API, |
18 public base::SupportsWeakPtr< | 18 public base::SupportsWeakPtr< |
19 PPB_NetworkMonitor_Private_Proxy::NetworkMonitor> { | 19 PPB_NetworkMonitor_Private_Proxy::NetworkMonitor> { |
20 public: | 20 public: |
21 NetworkMonitor(PP_Instance instance, | 21 NetworkMonitor(PP_Instance instance, |
22 PPB_NetworkMonitor_Private_Proxy* proxy, | 22 PPB_NetworkMonitor_Private_Proxy* proxy) |
23 PPB_NetworkMonitor_Callback callback, | |
24 void* user_data) | |
25 : Resource(OBJECT_IS_PROXY, instance), | 23 : Resource(OBJECT_IS_PROXY, instance), |
26 proxy_(proxy), | 24 proxy_(proxy), |
27 callback_(callback), | 25 initial_list_sent_(false), |
28 user_data_(user_data) { | 26 network_list_(NULL) { |
29 } | 27 } |
30 | 28 |
31 virtual ~NetworkMonitor() { | 29 virtual ~NetworkMonitor() { |
| 30 if (TrackedCallback::IsPending(update_callback_)) |
| 31 update_callback_->PostAbort(); |
32 proxy_->OnNetworkMonitorDeleted(this, pp_instance()); | 32 proxy_->OnNetworkMonitorDeleted(this, pp_instance()); |
33 } | 33 } |
34 | 34 |
| 35 // thunk::PPB_NetworkMonitor_API interface. |
| 36 virtual int32_t UpdateNetworkList( |
| 37 PP_Resource* network_list, |
| 38 scoped_refptr<TrackedCallback> callback) OVERRIDE { |
| 39 if (!network_list) |
| 40 return PP_ERROR_BADARGUMENT; |
| 41 if (TrackedCallback::IsPending(update_callback_)) |
| 42 return PP_ERROR_INPROGRESS; |
| 43 |
| 44 if (current_list_ && !initial_list_sent_) { |
| 45 initial_list_sent_ = true; |
| 46 thunk::EnterResourceCreationNoLock enter(pp_instance()); |
| 47 *network_list = PPB_NetworkList_Private_Shared::Create( |
| 48 OBJECT_IS_PROXY, pp_instance(), current_list_); |
| 49 return PP_OK; |
| 50 } |
| 51 |
| 52 network_list_ = network_list; |
| 53 update_callback_ = callback; |
| 54 return PP_OK_COMPLETIONPENDING; |
| 55 } |
35 | 56 |
36 // Resource overrides. | 57 // Resource overrides. |
37 virtual ppapi::thunk::PPB_NetworkMonitor_Private_API* | 58 virtual ppapi::thunk::PPB_NetworkMonitor_API* |
38 AsPPB_NetworkMonitor_Private_API() OVERRIDE { | 59 AsPPB_NetworkMonitor_API() OVERRIDE { |
39 return this; | 60 return this; |
40 } | 61 } |
41 | 62 |
42 // This is invoked when a network list is received for this monitor (either | 63 // This is invoked when a network list is received for this monitor (either |
43 // initially or on a change). It acquires the ProxyLock inside because | 64 // initially or on a change). |
44 // ObserverListThreadSafe does not support Bind/Closure, otherwise we would | 65 void OnNetworkListReceived(const scoped_refptr<NetworkListStorage>& list) { |
45 // wrap the call with a lock using RunWhileLocked. | 66 current_list_ = list; |
46 void OnNetworkListReceivedLocks( | 67 |
47 const scoped_refptr<NetworkListStorage>& list) { | 68 if (TrackedCallback::IsPending(update_callback_)) { |
48 ProxyAutoLock lock; | 69 initial_list_sent_ = true; |
49 PP_Resource list_resource = | 70 { |
50 PPB_NetworkList_Private_Shared::Create( | 71 thunk::EnterResourceCreationNoLock enter(pp_instance()); |
| 72 *network_list_ = PPB_NetworkList_Private_Shared::Create( |
51 OBJECT_IS_PROXY, pp_instance(), list); | 73 OBJECT_IS_PROXY, pp_instance(), list); |
52 CallWhileUnlocked(callback_, user_data_, list_resource); | 74 network_list_ = NULL; |
| 75 } |
| 76 update_callback_->Run(PP_OK); |
| 77 } |
53 } | 78 } |
54 | 79 |
55 private: | 80 private: |
56 PPB_NetworkMonitor_Private_Proxy* proxy_; | 81 PPB_NetworkMonitor_Private_Proxy* proxy_; |
57 PPB_NetworkMonitor_Callback callback_; | 82 scoped_refptr<NetworkListStorage> current_list_; |
58 void* user_data_; | 83 bool initial_list_sent_; |
| 84 |
| 85 // Parameters passed to UpdateNetworkList(); |
| 86 PP_Resource* network_list_; |
| 87 scoped_refptr<TrackedCallback> update_callback_; |
59 | 88 |
60 DISALLOW_COPY_AND_ASSIGN(NetworkMonitor); | 89 DISALLOW_COPY_AND_ASSIGN(NetworkMonitor); |
61 }; | 90 }; |
62 | 91 |
63 PPB_NetworkMonitor_Private_Proxy::PPB_NetworkMonitor_Private_Proxy( | 92 PPB_NetworkMonitor_Private_Proxy::PPB_NetworkMonitor_Private_Proxy( |
64 Dispatcher* dispatcher) | 93 Dispatcher* dispatcher) |
65 : InterfaceProxy(dispatcher), | 94 : InterfaceProxy(dispatcher), |
66 monitors_(new ObserverListThreadSafe<NetworkMonitor>()), | 95 monitors_(ObserverList<NetworkMonitor>::NOTIFY_EXISTING_ONLY) { |
67 monitors_count_(0) { | |
68 } | 96 } |
69 | 97 |
70 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() { | 98 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() { |
71 monitors_->AssertEmpty(); | 99 DCHECK(!monitors_.might_have_observers()); |
72 } | 100 } |
73 | 101 |
74 // static | 102 // static |
75 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource( | 103 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource( |
76 PP_Instance instance, | 104 PP_Instance instance) { |
77 PPB_NetworkMonitor_Callback callback, | |
78 void* user_data) { | |
79 // TODO(dmichael): Check that this thread has a valid message loop associated | 105 // TODO(dmichael): Check that this thread has a valid message loop associated |
80 // with it. | 106 // with it. |
81 if (!callback) | |
82 return 0; | |
83 | |
84 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 107 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
85 if (!dispatcher) | 108 if (!dispatcher) |
86 return 0; | 109 return 0; |
87 PPB_NetworkMonitor_Private_Proxy* proxy = | 110 PPB_NetworkMonitor_Private_Proxy* proxy = |
88 static_cast<PPB_NetworkMonitor_Private_Proxy*>( | 111 static_cast<PPB_NetworkMonitor_Private_Proxy*>( |
89 dispatcher->GetInterfaceProxy(kApiID)); | 112 dispatcher->GetInterfaceProxy(kApiID)); |
90 if (!proxy) | 113 if (!proxy) |
91 return 0; | 114 return 0; |
92 | 115 |
93 scoped_refptr<NetworkMonitor> result( | 116 scoped_refptr<NetworkMonitor> result(new NetworkMonitor(instance, proxy)); |
94 new NetworkMonitor(instance, proxy, callback, user_data)); | |
95 proxy->monitors_->AddObserver(result.get()); | |
96 | 117 |
97 proxy->monitors_count_++; | 118 bool first_network_monitor = !proxy->monitors_.might_have_observers(); |
98 if (proxy->monitors_count_ == 1) { | 119 proxy->monitors_.AddObserver(result.get()); |
| 120 if (first_network_monitor) { |
99 // If that is the first network monitor then send Start message. | 121 // If that is the first network monitor then send Start message. |
100 PluginGlobals::Get()->GetBrowserSender()->Send( | 122 PluginGlobals::Get()->GetBrowserSender()->Send( |
101 new PpapiHostMsg_PPBNetworkMonitor_Start( | 123 new PpapiHostMsg_PPBNetworkMonitor_Start( |
102 dispatcher->plugin_dispatcher_id())); | 124 dispatcher->plugin_dispatcher_id())); |
103 | 125 |
104 // We could have received network list message after sending the | 126 // We could have received network list message after sending the |
105 // previous Stop message. This list is stale now, so reset it | 127 // previous Stop message. This list is stale now, so reset it |
106 // here. | 128 // here. |
107 proxy->current_list_ = NULL; | 129 proxy->current_list_ = NULL; |
108 } else if (proxy->current_list_.get()) { | 130 } else if (proxy->current_list_.get()) { |
109 base::MessageLoop::current()->PostTask( | 131 result->OnNetworkListReceived(proxy->current_list_); |
110 FROM_HERE, | |
111 base::Bind(&NetworkMonitor::OnNetworkListReceivedLocks, | |
112 result->AsWeakPtr(), | |
113 proxy->current_list_)); | |
114 } | 132 } |
115 | 133 |
116 return result->GetReference(); | 134 return result->GetReference(); |
117 } | 135 } |
118 | 136 |
119 bool PPB_NetworkMonitor_Private_Proxy::OnMessageReceived( | 137 bool PPB_NetworkMonitor_Private_Proxy::OnMessageReceived( |
120 const IPC::Message& msg) { | 138 const IPC::Message& msg) { |
121 bool handled = true; | 139 bool handled = true; |
122 IPC_BEGIN_MESSAGE_MAP(PPB_NetworkMonitor_Private_Proxy, msg) | 140 IPC_BEGIN_MESSAGE_MAP(PPB_NetworkMonitor_Private_Proxy, msg) |
123 IPC_MESSAGE_HANDLER(PpapiMsg_PPBNetworkMonitor_NetworkList, | 141 IPC_MESSAGE_HANDLER(PpapiMsg_PPBNetworkMonitor_NetworkList, |
124 OnPluginMsgNetworkList) | 142 OnPluginMsgNetworkList) |
125 IPC_MESSAGE_UNHANDLED(handled = false) | 143 IPC_MESSAGE_UNHANDLED(handled = false) |
126 IPC_END_MESSAGE_MAP() | 144 IPC_END_MESSAGE_MAP() |
127 return handled; | 145 return handled; |
128 } | 146 } |
129 | 147 |
130 void PPB_NetworkMonitor_Private_Proxy::OnPluginMsgNetworkList( | 148 void PPB_NetworkMonitor_Private_Proxy::OnPluginMsgNetworkList( |
131 uint32 plugin_dispatcher_id, | 149 uint32 plugin_dispatcher_id, |
132 const ppapi::NetworkList& list) { | 150 const ppapi::NetworkList& list) { |
133 scoped_refptr<NetworkListStorage> list_storage(new NetworkListStorage(list)); | 151 scoped_refptr<NetworkListStorage> list_storage(new NetworkListStorage(list)); |
134 current_list_ = list_storage; | 152 current_list_ = list_storage; |
135 monitors_->Notify(&NetworkMonitor::OnNetworkListReceivedLocks, list_storage); | 153 FOR_EACH_OBSERVER(NetworkMonitor, monitors_, |
| 154 OnNetworkListReceived(list_storage)); |
136 } | 155 } |
137 | 156 |
138 void PPB_NetworkMonitor_Private_Proxy::OnNetworkMonitorDeleted( | 157 void PPB_NetworkMonitor_Private_Proxy::OnNetworkMonitorDeleted( |
139 NetworkMonitor* monitor, | 158 NetworkMonitor* monitor, |
140 PP_Instance instance) { | 159 PP_Instance instance) { |
141 monitors_->RemoveObserver(monitor); | 160 monitors_.RemoveObserver(monitor); |
142 monitors_count_--; | 161 if (!monitors_.might_have_observers()) { |
143 if (monitors_count_ == 0) { | |
144 // Send Stop message if that was the last NetworkMonitor. | 162 // Send Stop message if that was the last NetworkMonitor. |
145 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 163 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
146 if (dispatcher) { | 164 if (dispatcher) { |
147 PluginGlobals::Get()->GetBrowserSender()->Send( | 165 PluginGlobals::Get()->GetBrowserSender()->Send( |
148 new PpapiHostMsg_PPBNetworkMonitor_Stop( | 166 new PpapiHostMsg_PPBNetworkMonitor_Stop( |
149 dispatcher->plugin_dispatcher_id())); | 167 dispatcher->plugin_dispatcher_id())); |
150 } | 168 } |
151 current_list_ = NULL; | 169 current_list_ = NULL; |
152 } | 170 } |
153 } | 171 } |
154 | 172 |
155 } // namespace proxy | 173 } // namespace proxy |
156 } // namespace ppapi | 174 } // namespace ppapi |
OLD | NEW |