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

Side by Side Diff: remoting/host/plugin/host_plugin.cc

Issue 7635030: Add PluginMessageLoopProxy and use it for Host plugin UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <stdio.h> 5 #include <stdio.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/at_exit.h" 11 #include "base/at_exit.h"
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/stringize_macros.h" 14 #include "base/stringize_macros.h"
15 #include "remoting/base/plugin_message_loop_proxy.h"
15 #include "remoting/host/plugin/host_plugin_utils.h" 16 #include "remoting/host/plugin/host_plugin_utils.h"
16 #include "remoting/host/plugin/host_script_object.h" 17 #include "remoting/host/plugin/host_script_object.h"
17 #include "third_party/npapi/bindings/npapi.h" 18 #include "third_party/npapi/bindings/npapi.h"
18 #include "third_party/npapi/bindings/npfunctions.h" 19 #include "third_party/npapi/bindings/npfunctions.h"
19 #include "third_party/npapi/bindings/npruntime.h" 20 #include "third_party/npapi/bindings/npruntime.h"
20 21
21 // Symbol export is handled with a separate def file on Windows. 22 // Symbol export is handled with a separate def file on Windows.
22 #if defined (__GNUC__) && __GNUC__ >= 4 23 #if defined (__GNUC__) && __GNUC__ >= 4
23 #define EXPORT __attribute__((visibility("default"))) 24 #define EXPORT __attribute__((visibility("default")))
24 #else 25 #else
(...skipping 26 matching lines...) Expand all
51 52
52 // The name and description are returned by GetValue, but are also 53 // The name and description are returned by GetValue, but are also
53 // combined with the MIME type to satisfy GetMIMEDescription, so we 54 // combined with the MIME type to satisfy GetMIMEDescription, so we
54 // use macros here to allow that to happen at compile-time. 55 // use macros here to allow that to happen at compile-time.
55 #define HOST_PLUGIN_NAME "Remoting Host Plugin" 56 #define HOST_PLUGIN_NAME "Remoting Host Plugin"
56 #define HOST_PLUGIN_DESCRIPTION "Remoting Host Plugin" 57 #define HOST_PLUGIN_DESCRIPTION "Remoting Host Plugin"
57 58
58 // NPAPI plugin implementation for remoting host. 59 // NPAPI plugin implementation for remoting host.
59 // Documentation for most of the calls in this class can be found here: 60 // Documentation for most of the calls in this class can be found here:
60 // https://developer.mozilla.org/en/Gecko_Plugin_API_Reference/Scripting_plugins 61 // https://developer.mozilla.org/en/Gecko_Plugin_API_Reference/Scripting_plugins
61 class HostNPPlugin { 62 class HostNPPlugin : public remoting::PluginMessageLoopProxy::Delegate {
62 public: 63 public:
63 // |mode| is the display mode of plug-in. Values: 64 // |mode| is the display mode of plug-in. Values:
64 // NP_EMBED: (1) Instance was created by an EMBED tag and shares the browser 65 // NP_EMBED: (1) Instance was created by an EMBED tag and shares the browser
65 // window with other content. 66 // window with other content.
66 // NP_FULL: (2) Instance was created by a separate file and is the primary 67 // NP_FULL: (2) Instance was created by a separate file and is the primary
67 // content in the window. 68 // content in the window.
68 HostNPPlugin(NPP instance, uint16 mode) 69 HostNPPlugin(NPP instance, uint16 mode)
69 : instance_(instance), scriptable_object_(NULL) {} 70 : instance_(instance),
71 scriptable_object_(NULL),
72 np_thread_id_(base::PlatformThread::CurrentId()) {
73 }
70 74
71 ~HostNPPlugin() { 75 ~HostNPPlugin() {
72 if (scriptable_object_) { 76 if (scriptable_object_) {
73 g_npnetscape_funcs->releaseobject(scriptable_object_); 77 g_npnetscape_funcs->releaseobject(scriptable_object_);
74 scriptable_object_ = NULL; 78 scriptable_object_ = NULL;
75 } 79 }
76 } 80 }
77 81
78 bool Init(int16 argc, char** argn, char** argv, NPSavedData* saved) { 82 bool Init(int16 argc, char** argn, char** argv, NPSavedData* saved) {
79 #if defined(OS_MACOSX) 83 #if defined(OS_MACOSX)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 &RemoveProperty, 143 &RemoveProperty,
140 &Enumerate, 144 &Enumerate,
141 NULL 145 NULL
142 }; 146 };
143 scriptable_object_ = g_npnetscape_funcs->createobject(instance_, 147 scriptable_object_ = g_npnetscape_funcs->createobject(instance_,
144 &npc_ref_object); 148 &npc_ref_object);
145 } 149 }
146 return scriptable_object_; 150 return scriptable_object_;
147 } 151 }
148 152
153 // PluginMessageLoopProxy::Delegate implementation.
154 virtual bool RunOnPluginThread(
155 int delay_ms, void(function)(void*), void* data) OVERRIDE {
156 if (delay_ms == 0) {
157 g_npnetscape_funcs->pluginthreadasynccall(instance_, function, data);
158 } else {
159 base::AutoLock auto_lock(timers_lock_);
160 uint32_t timer_id = g_npnetscape_funcs->scheduletimer(
161 instance_, delay_ms, false, &NPDelayedTaskSpringboard);
162 DelayedTask task = {function, data};
163 timers_[timer_id] = task;
164 }
165 return true;
166 }
167
168 virtual bool IsPluginThread() OVERRIDE {
169 return np_thread_id_ == base::PlatformThread::CurrentId();
170 }
171
172 static void NPDelayedTaskSpringboard(NPP npp, uint32_t timer_id) {
173 HostNPPlugin* self = reinterpret_cast<HostNPPlugin*>(npp->pdata);
174 DelayedTask task;
175 {
176 base::AutoLock auto_lock(self->timers_lock_);
177 std::map<uint32_t, DelayedTask>::iterator it =
178 self->timers_.find(timer_id);
179 CHECK(it != self->timers_.end());
180 DelayedTask task = it->second;
181 self->timers_.erase(it);
182 }
183 task.function(task.data);
184 }
185
149 private: 186 private:
150 struct ScriptableNPObject : public NPObject { 187 struct ScriptableNPObject : public NPObject {
151 HostNPScriptObject* scriptable_object; 188 HostNPScriptObject* scriptable_object;
152 }; 189 };
153 190
191 struct DelayedTask {
192 void (*function)(void*);
193 void* data;
194 };
195
154 static HostNPScriptObject* ScriptableFromObject(NPObject* obj) { 196 static HostNPScriptObject* ScriptableFromObject(NPObject* obj) {
155 return reinterpret_cast<ScriptableNPObject*>(obj)->scriptable_object; 197 return reinterpret_cast<ScriptableNPObject*>(obj)->scriptable_object;
156 } 198 }
157 199
158 static NPObject* Allocate(NPP npp, NPClass* aClass) { 200 static NPObject* Allocate(NPP npp, NPClass* aClass) {
159 VLOG(2) << "static Allocate"; 201 VLOG(2) << "static Allocate";
160 ScriptableNPObject* object = 202 ScriptableNPObject* object =
161 reinterpret_cast<ScriptableNPObject*>( 203 reinterpret_cast<ScriptableNPObject*>(
162 g_npnetscape_funcs->memalloc(sizeof(ScriptableNPObject))); 204 g_npnetscape_funcs->memalloc(sizeof(ScriptableNPObject)));
205 HostNPPlugin* plugin = reinterpret_cast<HostNPPlugin*>(npp->pdata);
163 206
164 object->_class = aClass; 207 object->_class = aClass;
165 object->referenceCount = 1; 208 object->referenceCount = 1;
166 object->scriptable_object = new HostNPScriptObject(npp, object); 209 object->scriptable_object = new HostNPScriptObject(npp, object, plugin);
167 if (!object->scriptable_object->Init()) { 210 if (!object->scriptable_object->Init()) {
168 Deallocate(object); 211 Deallocate(object);
169 object = NULL; 212 object = NULL;
170 } 213 }
171 return object; 214 return object;
172 } 215 }
173 216
174 static void Deallocate(NPObject* npobj) { 217 static void Deallocate(NPObject* npobj) {
175 VLOG(2) << "static Deallocate"; 218 VLOG(2) << "static Deallocate";
176 if (npobj) { 219 if (npobj) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 for (uint32_t i = 0; i < *count; ++i) { 326 for (uint32_t i = 0; i < *count; ++i) {
284 (*value)[i] = 327 (*value)[i] =
285 g_npnetscape_funcs->getstringidentifier(values[i].c_str()); 328 g_npnetscape_funcs->getstringidentifier(values[i].c_str());
286 } 329 }
287 } 330 }
288 return is_good; 331 return is_good;
289 } 332 }
290 333
291 NPP instance_; 334 NPP instance_;
292 NPObject* scriptable_object_; 335 NPObject* scriptable_object_;
336
337 base::PlatformThreadId np_thread_id_;
338 std::map<uint32_t, DelayedTask> timers_;
339 base::Lock timers_lock_;
293 }; 340 };
294 341
295 // Utility functions to map NPAPI Entry Points to C++ Objects. 342 // Utility functions to map NPAPI Entry Points to C++ Objects.
296 HostNPPlugin* PluginFromInstance(NPP instance) { 343 HostNPPlugin* PluginFromInstance(NPP instance) {
297 return reinterpret_cast<HostNPPlugin*>(instance->pdata); 344 return reinterpret_cast<HostNPPlugin*>(instance->pdata);
298 } 345 }
299 346
300 NPError CreatePlugin(NPMIMEType pluginType, 347 NPError CreatePlugin(NPMIMEType pluginType,
301 NPP instance, 348 NPP instance,
302 uint16 mode, 349 uint16 mode,
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 } 492 }
446 493
447 EXPORT NPError API_CALL NP_GetValue(void* npp, 494 EXPORT NPError API_CALL NP_GetValue(void* npp,
448 NPPVariable variable, 495 NPPVariable variable,
449 void* value) { 496 void* value) {
450 return GetValue((NPP)npp, variable, value); 497 return GetValue((NPP)npp, variable, value);
451 } 498 }
452 #endif 499 #endif
453 500
454 } // extern "C" 501 } // extern "C"
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698