Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: ppapi/proxy/ppb_network_monitor_private_proxy.cc

Issue 23453025: Refactor PPB_NetworkMonitor_Private interface to use CompletionCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_private_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_Private_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_Private_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::EnterResourceCreation enter(pp_instance());
yzshen1 2013/09/04 19:52:01 Please use EnterResourceCreationNoLock. At this po
Sergey Ulanov 2013/09/04 22:22:32 Done. Thanks for catching this.
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_Private_API*
38 AsPPB_NetworkMonitor_Private_API() OVERRIDE { 59 AsPPB_NetworkMonitor_Private_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). It acquires the ProxyLock inside because
44 // ObserverListThreadSafe does not support Bind/Closure, otherwise we would 65 // ObserverListThreadSafe does not support Bind/Closure, otherwise we would
45 // wrap the call with a lock using RunWhileLocked. 66 // wrap the call with a lock using RunWhileLocked.
46 void OnNetworkListReceivedLocks( 67 void OnNetworkListReceivedLocks(
47 const scoped_refptr<NetworkListStorage>& list) { 68 const scoped_refptr<NetworkListStorage>& list) {
48 ProxyAutoLock lock; 69 ProxyAutoLock lock;
49 PP_Resource list_resource = 70
50 PPB_NetworkList_Private_Shared::Create( 71 current_list_ = list;
72
73 if (TrackedCallback::IsPending(update_callback_)) {
74 initial_list_sent_ = true;
75 {
76 thunk::EnterResourceCreationNoLock enter(pp_instance());
77 *network_list_ = PPB_NetworkList_Private_Shared::Create(
51 OBJECT_IS_PROXY, pp_instance(), list); 78 OBJECT_IS_PROXY, pp_instance(), list);
yzshen1 2013/09/04 19:52:01 nit: please consider setting network_list_ to NULL
Sergey Ulanov 2013/09/04 22:22:32 Done.
52 CallWhileUnlocked(callback_, user_data_, list_resource); 79 }
80 update_callback_->Run(PP_OK);
81 }
53 } 82 }
54 83
55 private: 84 private:
56 PPB_NetworkMonitor_Private_Proxy* proxy_; 85 PPB_NetworkMonitor_Private_Proxy* proxy_;
57 PPB_NetworkMonitor_Callback callback_; 86 scoped_refptr<NetworkListStorage> current_list_;
58 void* user_data_; 87 bool initial_list_sent_;
88
89 // Parameters passed to UpdateNetworkList();
90 PP_Resource* network_list_;
91 scoped_refptr<TrackedCallback> update_callback_;
59 92
60 DISALLOW_COPY_AND_ASSIGN(NetworkMonitor); 93 DISALLOW_COPY_AND_ASSIGN(NetworkMonitor);
61 }; 94 };
62 95
63 PPB_NetworkMonitor_Private_Proxy::PPB_NetworkMonitor_Private_Proxy( 96 PPB_NetworkMonitor_Private_Proxy::PPB_NetworkMonitor_Private_Proxy(
64 Dispatcher* dispatcher) 97 Dispatcher* dispatcher)
65 : InterfaceProxy(dispatcher), 98 : InterfaceProxy(dispatcher),
66 monitors_(new ObserverListThreadSafe<NetworkMonitor>()), 99 monitors_(new ObserverListThreadSafe<NetworkMonitor>()),
67 monitors_count_(0) { 100 monitors_count_(0) {
68 } 101 }
69 102
70 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() { 103 PPB_NetworkMonitor_Private_Proxy::~PPB_NetworkMonitor_Private_Proxy() {
71 monitors_->AssertEmpty(); 104 monitors_->AssertEmpty();
72 } 105 }
73 106
74 // static 107 // static
75 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource( 108 PP_Resource PPB_NetworkMonitor_Private_Proxy::CreateProxyResource(
76 PP_Instance instance, 109 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 110 // TODO(dmichael): Check that this thread has a valid message loop associated
80 // with it. 111 // with it.
81 if (!callback)
82 return 0;
83
84 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); 112 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
85 if (!dispatcher) 113 if (!dispatcher)
86 return 0; 114 return 0;
87 PPB_NetworkMonitor_Private_Proxy* proxy = 115 PPB_NetworkMonitor_Private_Proxy* proxy =
88 static_cast<PPB_NetworkMonitor_Private_Proxy*>( 116 static_cast<PPB_NetworkMonitor_Private_Proxy*>(
89 dispatcher->GetInterfaceProxy(kApiID)); 117 dispatcher->GetInterfaceProxy(kApiID));
90 if (!proxy) 118 if (!proxy)
91 return 0; 119 return 0;
92 120
93 scoped_refptr<NetworkMonitor> result( 121 scoped_refptr<NetworkMonitor> result(new NetworkMonitor(instance, proxy));
94 new NetworkMonitor(instance, proxy, callback, user_data));
95 proxy->monitors_->AddObserver(result.get()); 122 proxy->monitors_->AddObserver(result.get());
96 123
97 proxy->monitors_count_++; 124 proxy->monitors_count_++;
98 if (proxy->monitors_count_ == 1) { 125 if (proxy->monitors_count_ == 1) {
99 // If that is the first network monitor then send Start message. 126 // If that is the first network monitor then send Start message.
100 PluginGlobals::Get()->GetBrowserSender()->Send( 127 PluginGlobals::Get()->GetBrowserSender()->Send(
101 new PpapiHostMsg_PPBNetworkMonitor_Start( 128 new PpapiHostMsg_PPBNetworkMonitor_Start(
102 dispatcher->plugin_dispatcher_id())); 129 dispatcher->plugin_dispatcher_id()));
103 130
104 // We could have received network list message after sending the 131 // We could have received network list message after sending the
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 PluginGlobals::Get()->GetBrowserSender()->Send( 174 PluginGlobals::Get()->GetBrowserSender()->Send(
148 new PpapiHostMsg_PPBNetworkMonitor_Stop( 175 new PpapiHostMsg_PPBNetworkMonitor_Stop(
149 dispatcher->plugin_dispatcher_id())); 176 dispatcher->plugin_dispatcher_id()));
150 } 177 }
151 current_list_ = NULL; 178 current_list_ = NULL;
152 } 179 }
153 } 180 }
154 181
155 } // namespace proxy 182 } // namespace proxy
156 } // namespace ppapi 183 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698