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
|
// 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 "ppapi/proxy/ppb_network_monitor_private_proxy.h"
#include "ppapi/proxy/enter_proxy.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/thunk/ppb_network_monitor_private_api.h"
namespace ppapi {
namespace proxy {
class PPB_NetworkMonitor_Private_Proxy::NetworkMonitor
: public Resource,
public thunk::PPB_NetworkMonitor_Private_API,
public base::SupportsWeakPtr<
PPB_NetworkMonitor_Private_Proxy::NetworkMonitor> {
public:
NetworkMonitor(PP_Instance instance,
PPB_NetworkMonitor_Private_Proxy* proxy,
PPB_NetworkMonitor_Callback callback,
void* user_data)
: Resource(OBJECT_IS_PROXY, instance),
proxy_(proxy),
callback_(callback),
user_data_(user_data) {
}
virtual ~NetworkMonitor() {
proxy_->OnNetworkMonitorDeleted(this, pp_instance());
}
// Resource overrides.
virtual ppapi::thunk::PPB_NetworkMonitor_Private_API*
AsPPB_NetworkMonitor_Private_API() OVERRIDE {
return this;
}
// This is invoked when a network list is received for this monitor (either
// initially or on a change). It acquires the ProxyLock inside because
// ObserverListThreadSafe does not support Bind/Closure, otherwise we would
// wrap the call with a lock using RunWhileLocked.
void OnNetworkListReceivedLocks(
const scoped_refptr<NetworkListStorage>& list) {
ProxyAutoLock lock;
PP_Resource list_resource =
PPB_NetworkList_Private_Shared::Create(
OBJECT_IS_PROXY, pp_instance(), list);
CallWhileUnlocked(callback_, user_data_, list_resource);
}
private:
PPB_NetworkMonitor_Private_Proxy* proxy_;
PPB_NetworkMonitor_Callback callback_;
void* user_data_;
DISALLOW_COPY_AND_ASSIGN(NetworkMonitor);
};
PPB_NetworkMonitor_Private_Proxy::PPB_NetworkMonitor_Private_Proxy(
Dispatcher* dispatcher)
: InterfaceProxy(dispatcher),
monitors_(new ObserverListThreadSafe<NetworkMonitor>()),
monitors_count_(0) {
}
PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() {
monitors_->AssertEmpty();
}
// static
PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource(
PP_Instance instance,
PPB_NetworkMonitor_Callback callback,
void* user_data) {
// TODO(dmichael): Check that this thread has a valid message loop associated
// with it.
if (!callback)
return 0;
PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
if (!dispatcher)
return 0;
PPB_NetworkMonitor_Private_Proxy* proxy =
static_cast<PPB_NetworkMonitor_Private_Proxy*>(
dispatcher->GetInterfaceProxy(kApiID));
if (!proxy)
return 0;
scoped_refptr<NetworkMonitor> result(
new NetworkMonitor(instance, proxy, callback, user_data));
proxy->monitors_->AddObserver(result.get());
proxy->monitors_count_++;
if (proxy->monitors_count_ == 1) {
// If that is the first network monitor then send Start message.
PluginGlobals::Get()->GetBrowserSender()->Send(
new PpapiHostMsg_PPBNetworkMonitor_Start(
dispatcher->plugin_dispatcher_id()));
// We could have received network list message after sending the
// previous Stop message. This list is stale now, so reset it
// here.
proxy->current_list_ = NULL;
} else if (proxy->current_list_.get()) {
MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
&NetworkMonitor::OnNetworkListReceivedLocks,
result->AsWeakPtr(), proxy->current_list_));
}
return result->GetReference();
}
bool PPB_NetworkMonitor_Private_Proxy::OnMessageReceived(
const IPC::Message& msg) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PPB_NetworkMonitor_Private_Proxy, msg)
IPC_MESSAGE_HANDLER(PpapiMsg_PPBNetworkMonitor_NetworkList,
OnPluginMsgNetworkList)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PPB_NetworkMonitor_Private_Proxy::OnPluginMsgNetworkList(
uint32 plugin_dispatcher_id,
const ppapi::NetworkList& list) {
scoped_refptr<NetworkListStorage> list_storage(new NetworkListStorage(list));
current_list_ = list_storage;
monitors_->Notify(&NetworkMonitor::OnNetworkListReceivedLocks, list_storage);
}
void PPB_NetworkMonitor_Private_Proxy::OnNetworkMonitorDeleted(
NetworkMonitor* monitor,
PP_Instance instance) {
monitors_->RemoveObserver(monitor);
monitors_count_--;
if (monitors_count_ == 0) {
// Send Stop message if that was the last NetworkMonitor.
PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
if (dispatcher) {
PluginGlobals::Get()->GetBrowserSender()->Send(
new PpapiHostMsg_PPBNetworkMonitor_Stop(
dispatcher->plugin_dispatcher_id()));
}
current_list_ = NULL;
}
}
} // namespace proxy
} // namespace ppapi
|