| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/child/npapi/plugin_instance.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "content/child/npapi/plugin_host.h" | |
| 19 #include "content/child/npapi/plugin_lib.h" | |
| 20 #include "content/child/npapi/webplugin.h" | |
| 21 #include "content/child/npapi/webplugin_delegate.h" | |
| 22 #include "content/child/npapi/webplugin_resource_client.h" | |
| 23 #include "content/public/common/content_constants.h" | |
| 24 #include "content/public/common/content_switches.h" | |
| 25 #include "net/base/escape.h" | |
| 26 | |
| 27 #if defined(OS_MACOSX) | |
| 28 #include <ApplicationServices/ApplicationServices.h> | |
| 29 #endif | |
| 30 | |
| 31 namespace content { | |
| 32 | |
| 33 PluginInstance::PluginInstance(PluginLib* plugin, const std::string& mime_type) | |
| 34 : plugin_(plugin), | |
| 35 npp_(0), | |
| 36 host_(PluginHost::Singleton()), | |
| 37 npp_functions_(plugin->functions()), | |
| 38 transparent_(true), | |
| 39 webplugin_(0), | |
| 40 mime_type_(mime_type), | |
| 41 use_mozilla_user_agent_(false), | |
| 42 #if defined (OS_MACOSX) | |
| 43 #ifdef NP_NO_QUICKDRAW | |
| 44 drawing_model_(NPDrawingModelCoreGraphics), | |
| 45 #else | |
| 46 drawing_model_(NPDrawingModelQuickDraw), | |
| 47 #endif | |
| 48 #ifdef NP_NO_CARBON | |
| 49 event_model_(NPEventModelCocoa), | |
| 50 #else | |
| 51 event_model_(NPEventModelCarbon), | |
| 52 #endif | |
| 53 currently_handled_event_(NULL), | |
| 54 #endif | |
| 55 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 56 load_manually_(false), | |
| 57 next_timer_id_(1) { | |
| 58 npp_ = new NPP_t(); | |
| 59 npp_->ndata = 0; | |
| 60 npp_->pdata = 0; | |
| 61 | |
| 62 if (mime_type_ == kFlashPluginSwfMimeType) | |
| 63 transparent_ = false; | |
| 64 | |
| 65 memset(&zero_padding_, 0, sizeof(zero_padding_)); | |
| 66 } | |
| 67 | |
| 68 PluginInstance::~PluginInstance() { | |
| 69 if (npp_ != 0) { | |
| 70 delete npp_; | |
| 71 npp_ = 0; | |
| 72 } | |
| 73 | |
| 74 if (plugin_.get()) | |
| 75 plugin_->CloseInstance(); | |
| 76 } | |
| 77 | |
| 78 bool PluginInstance::Start(const GURL& url, | |
| 79 char** const param_names, | |
| 80 char** const param_values, | |
| 81 int param_count, | |
| 82 bool load_manually) { | |
| 83 load_manually_ = load_manually; | |
| 84 unsigned short mode = load_manually_ ? NP_FULL : NP_EMBED; | |
| 85 npp_->ndata = this; | |
| 86 | |
| 87 NPError err = NPP_New(mode, param_count, | |
| 88 const_cast<char **>(param_names), const_cast<char **>(param_values)); | |
| 89 return err == NPERR_NO_ERROR; | |
| 90 } | |
| 91 | |
| 92 unsigned PluginInstance::GetBackingTextureId() { | |
| 93 // By default the plugin instance is not backed by an OpenGL texture. | |
| 94 return 0; | |
| 95 } | |
| 96 | |
| 97 // NPAPI methods | |
| 98 NPError PluginInstance::NPP_New(unsigned short mode, | |
| 99 short argc, | |
| 100 char* argn[], | |
| 101 char* argv[]) { | |
| 102 DCHECK(npp_functions_ != 0); | |
| 103 DCHECK(npp_functions_->newp != 0); | |
| 104 DCHECK(argc >= 0); | |
| 105 | |
| 106 if (npp_functions_->newp != 0) { | |
| 107 return npp_functions_->newp( | |
| 108 (NPMIMEType)mime_type_.c_str(), npp_, mode, argc, argn, argv, NULL); | |
| 109 } | |
| 110 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 111 } | |
| 112 | |
| 113 void PluginInstance::NPP_Destroy() { | |
| 114 DCHECK(npp_functions_ != 0); | |
| 115 DCHECK(npp_functions_->destroy != 0); | |
| 116 | |
| 117 if (npp_functions_->destroy != 0) { | |
| 118 NPSavedData *savedData = 0; | |
| 119 npp_functions_->destroy(npp_, &savedData); | |
| 120 | |
| 121 // TODO: Support savedData. Technically, these need to be | |
| 122 // saved on a per-URL basis, and then only passed | |
| 123 // to new instances of the plugin at the same URL. | |
| 124 // Sounds like a huge security risk. When we do support | |
| 125 // these, we should pass them back to the PluginLib | |
| 126 // to be stored there. | |
| 127 DCHECK(savedData == 0); | |
| 128 } | |
| 129 | |
| 130 for (unsigned int file_index = 0; file_index < files_created_.size(); | |
| 131 file_index++) { | |
| 132 base::DeleteFile(files_created_[file_index], false); | |
| 133 } | |
| 134 | |
| 135 // Ensure that no timer callbacks are invoked after NPP_Destroy. | |
| 136 timers_.clear(); | |
| 137 } | |
| 138 | |
| 139 NPError PluginInstance::NPP_SetWindow(NPWindow* window) { | |
| 140 DCHECK(npp_functions_ != 0); | |
| 141 DCHECK(npp_functions_->setwindow != 0); | |
| 142 | |
| 143 if (npp_functions_->setwindow != 0) { | |
| 144 return npp_functions_->setwindow(npp_, window); | |
| 145 } | |
| 146 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 147 } | |
| 148 | |
| 149 NPError PluginInstance::NPP_NewStream(NPMIMEType type, | |
| 150 NPStream* stream, | |
| 151 NPBool seekable, | |
| 152 unsigned short* stype) { | |
| 153 DCHECK(npp_functions_ != 0); | |
| 154 DCHECK(npp_functions_->newstream != 0); | |
| 155 if (npp_functions_->newstream != 0) { | |
| 156 return npp_functions_->newstream(npp_, type, stream, seekable, stype); | |
| 157 } | |
| 158 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 159 } | |
| 160 | |
| 161 NPError PluginInstance::NPP_DestroyStream(NPStream* stream, NPReason reason) { | |
| 162 DCHECK(npp_functions_ != 0); | |
| 163 DCHECK(npp_functions_->destroystream != 0); | |
| 164 | |
| 165 if (stream == NULL || (stream->ndata == NULL)) | |
| 166 return NPERR_INVALID_INSTANCE_ERROR; | |
| 167 | |
| 168 if (npp_functions_->destroystream != 0) { | |
| 169 NPError result = npp_functions_->destroystream(npp_, stream, reason); | |
| 170 stream->ndata = NULL; | |
| 171 return result; | |
| 172 } | |
| 173 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 174 } | |
| 175 | |
| 176 int PluginInstance::NPP_WriteReady(NPStream* stream) { | |
| 177 DCHECK(npp_functions_ != 0); | |
| 178 DCHECK(npp_functions_->writeready != 0); | |
| 179 if (npp_functions_->writeready != 0) { | |
| 180 return npp_functions_->writeready(npp_, stream); | |
| 181 } | |
| 182 return 0; | |
| 183 } | |
| 184 | |
| 185 int PluginInstance::NPP_Write(NPStream* stream, | |
| 186 int offset, | |
| 187 int len, | |
| 188 void* buffer) { | |
| 189 DCHECK(npp_functions_ != 0); | |
| 190 DCHECK(npp_functions_->write != 0); | |
| 191 if (npp_functions_->write != 0) { | |
| 192 return npp_functions_->write(npp_, stream, offset, len, buffer); | |
| 193 } | |
| 194 return 0; | |
| 195 } | |
| 196 | |
| 197 void PluginInstance::NPP_StreamAsFile(NPStream* stream, const char* fname) { | |
| 198 DCHECK(npp_functions_ != 0); | |
| 199 DCHECK(npp_functions_->asfile != 0); | |
| 200 if (npp_functions_->asfile != 0) { | |
| 201 npp_functions_->asfile(npp_, stream, fname); | |
| 202 } | |
| 203 | |
| 204 // Creating a temporary FilePath instance on the stack as the explicit | |
| 205 // FilePath constructor with StringType as an argument causes a compiler | |
| 206 // error when invoked via vector push back. | |
| 207 base::FilePath file_name = base::FilePath::FromUTF8Unsafe(fname); | |
| 208 files_created_.push_back(file_name); | |
| 209 } | |
| 210 | |
| 211 NPError PluginInstance::NPP_GetValue(NPPVariable variable, void* value) { | |
| 212 DCHECK(npp_functions_ != 0); | |
| 213 // getvalue is NULL for Shockwave | |
| 214 if (npp_functions_->getvalue != 0) { | |
| 215 return npp_functions_->getvalue(npp_, variable, value); | |
| 216 } | |
| 217 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 218 } | |
| 219 | |
| 220 NPError PluginInstance::NPP_SetValue(NPNVariable variable, void* value) { | |
| 221 DCHECK(npp_functions_ != 0); | |
| 222 if (npp_functions_->setvalue != 0) { | |
| 223 return npp_functions_->setvalue(npp_, variable, value); | |
| 224 } | |
| 225 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 226 } | |
| 227 | |
| 228 short PluginInstance::NPP_HandleEvent(void* event) { | |
| 229 DCHECK(npp_functions_ != 0); | |
| 230 DCHECK(npp_functions_->event != 0); | |
| 231 if (npp_functions_->event != 0) { | |
| 232 return npp_functions_->event(npp_, (void*)event); | |
| 233 } | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 237 bool PluginInstance::NPP_Print(NPPrint* platform_print) { | |
| 238 DCHECK(npp_functions_ != 0); | |
| 239 if (npp_functions_->print != 0) { | |
| 240 npp_functions_->print(npp_, platform_print); | |
| 241 return true; | |
| 242 } | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 | |
| 247 void PluginInstance::PluginThreadAsyncCall(void (*func)(void*), | |
| 248 void* user_data) { | |
| 249 task_runner_->PostTask( | |
| 250 FROM_HERE, base::Bind(&PluginInstance::OnPluginThreadAsyncCall, this, | |
| 251 func, user_data)); | |
| 252 } | |
| 253 | |
| 254 void PluginInstance::OnPluginThreadAsyncCall(void (*func)(void*), | |
| 255 void* user_data) { | |
| 256 // Do not invoke the callback if NPP_Destroy has already been invoked. | |
| 257 if (webplugin_) | |
| 258 func(user_data); | |
| 259 } | |
| 260 | |
| 261 uint32_t PluginInstance::ScheduleTimer(uint32_t interval, | |
| 262 NPBool repeat, | |
| 263 void (*func)(NPP id, | |
| 264 uint32_t timer_id)) { | |
| 265 // Use next timer id. | |
| 266 uint32_t timer_id; | |
| 267 timer_id = next_timer_id_; | |
| 268 ++next_timer_id_; | |
| 269 DCHECK(next_timer_id_ != 0); | |
| 270 | |
| 271 // Record timer interval and repeat. | |
| 272 TimerInfo info; | |
| 273 info.interval = interval; | |
| 274 info.repeat = repeat ? true : false; | |
| 275 timers_[timer_id] = info; | |
| 276 | |
| 277 // Schedule the callback. | |
| 278 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 279 FROM_HERE, | |
| 280 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id), | |
| 281 base::TimeDelta::FromMilliseconds(interval)); | |
| 282 return timer_id; | |
| 283 } | |
| 284 | |
| 285 void PluginInstance::UnscheduleTimer(uint32_t timer_id) { | |
| 286 // Remove info about the timer. | |
| 287 TimerMap::iterator it = timers_.find(timer_id); | |
| 288 if (it != timers_.end()) | |
| 289 timers_.erase(it); | |
| 290 } | |
| 291 | |
| 292 #if !defined(OS_MACOSX) | |
| 293 NPError PluginInstance::PopUpContextMenu(NPMenu* menu) { | |
| 294 NOTIMPLEMENTED(); | |
| 295 return NPERR_GENERIC_ERROR; | |
| 296 } | |
| 297 #endif | |
| 298 | |
| 299 void PluginInstance::OnTimerCall(void (*func)(NPP id, uint32_t timer_id), | |
| 300 NPP id, | |
| 301 uint32_t timer_id) { | |
| 302 // Do not invoke callback if the timer has been unscheduled. | |
| 303 TimerMap::iterator it = timers_.find(timer_id); | |
| 304 if (it == timers_.end()) | |
| 305 return; | |
| 306 | |
| 307 // Get all information about the timer before invoking the callback. The | |
| 308 // callback might unschedule the timer. | |
| 309 TimerInfo info = it->second; | |
| 310 | |
| 311 func(id, timer_id); | |
| 312 | |
| 313 // If the timer was unscheduled by the callback, just free up the timer id. | |
| 314 if (timers_.find(timer_id) == timers_.end()) | |
| 315 return; | |
| 316 | |
| 317 // Reschedule repeating timers after invoking the callback so callback is not | |
| 318 // re-entered if it pumps the message loop. | |
| 319 if (info.repeat) { | |
| 320 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 321 FROM_HERE, | |
| 322 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id), | |
| 323 base::TimeDelta::FromMilliseconds(info.interval)); | |
| 324 } else { | |
| 325 timers_.erase(it); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 void PluginInstance::PushPopupsEnabledState(bool enabled) { | |
| 330 popups_enabled_stack_.push(enabled); | |
| 331 } | |
| 332 | |
| 333 void PluginInstance::PopPopupsEnabledState() { | |
| 334 popups_enabled_stack_.pop(); | |
| 335 } | |
| 336 | |
| 337 bool PluginInstance::ConvertPoint(double source_x, double source_y, | |
| 338 NPCoordinateSpace source_space, | |
| 339 double* dest_x, double* dest_y, | |
| 340 NPCoordinateSpace dest_space) { | |
| 341 #if defined(OS_MACOSX) | |
| 342 CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID()); | |
| 343 | |
| 344 double flipped_screen_x = source_x; | |
| 345 double flipped_screen_y = source_y; | |
| 346 switch(source_space) { | |
| 347 case NPCoordinateSpacePlugin: | |
| 348 flipped_screen_x += plugin_origin_.x(); | |
| 349 flipped_screen_y += plugin_origin_.y(); | |
| 350 break; | |
| 351 case NPCoordinateSpaceWindow: | |
| 352 flipped_screen_x += containing_window_frame_.x(); | |
| 353 flipped_screen_y = containing_window_frame_.height() - source_y + | |
| 354 containing_window_frame_.y(); | |
| 355 break; | |
| 356 case NPCoordinateSpaceFlippedWindow: | |
| 357 flipped_screen_x += containing_window_frame_.x(); | |
| 358 flipped_screen_y += containing_window_frame_.y(); | |
| 359 break; | |
| 360 case NPCoordinateSpaceScreen: | |
| 361 flipped_screen_y = main_display_bounds.size.height - flipped_screen_y; | |
| 362 break; | |
| 363 case NPCoordinateSpaceFlippedScreen: | |
| 364 break; | |
| 365 default: | |
| 366 NOTREACHED(); | |
| 367 return false; | |
| 368 } | |
| 369 | |
| 370 double target_x = flipped_screen_x; | |
| 371 double target_y = flipped_screen_y; | |
| 372 switch(dest_space) { | |
| 373 case NPCoordinateSpacePlugin: | |
| 374 target_x -= plugin_origin_.x(); | |
| 375 target_y -= plugin_origin_.y(); | |
| 376 break; | |
| 377 case NPCoordinateSpaceWindow: | |
| 378 target_x -= containing_window_frame_.x(); | |
| 379 target_y -= containing_window_frame_.y(); | |
| 380 target_y = containing_window_frame_.height() - target_y; | |
| 381 break; | |
| 382 case NPCoordinateSpaceFlippedWindow: | |
| 383 target_x -= containing_window_frame_.x(); | |
| 384 target_y -= containing_window_frame_.y(); | |
| 385 break; | |
| 386 case NPCoordinateSpaceScreen: | |
| 387 target_y = main_display_bounds.size.height - flipped_screen_y; | |
| 388 break; | |
| 389 case NPCoordinateSpaceFlippedScreen: | |
| 390 break; | |
| 391 default: | |
| 392 NOTREACHED(); | |
| 393 return false; | |
| 394 } | |
| 395 | |
| 396 if (dest_x) | |
| 397 *dest_x = target_x; | |
| 398 if (dest_y) | |
| 399 *dest_y = target_y; | |
| 400 return true; | |
| 401 #else | |
| 402 NOTIMPLEMENTED(); | |
| 403 return false; | |
| 404 #endif | |
| 405 } | |
| 406 | |
| 407 } // namespace content | |
| OLD | NEW |