summaryrefslogtreecommitdiffstats
path: root/content/renderer/browser_plugin/browser_plugin.cc
blob: 6139b839c0b5fb59bee0e1fcb2196686c49dbba4 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// 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 "content/renderer/browser_plugin/browser_plugin.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#if defined (OS_WIN)
#include "base/sys_info.h"
#endif
#include "base/utf_string_conversions.h"
#include "content/common/browser_plugin_messages.h"
#include "content/common/view_messages.h"
#include "content/public/common/content_client.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/renderer/browser_plugin/browser_plugin_bindings.h"
#include "content/renderer/browser_plugin/browser_plugin_manager.h"
#include "content/renderer/render_process_impl.h"
#include "content/renderer/render_thread_impl.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginParams.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
#include "webkit/plugins/sad_plugin.h"

using WebKit::WebCanvas;
using WebKit::WebPlugin;
using WebKit::WebPluginContainer;
using WebKit::WebPluginParams;
using WebKit::WebPoint;
using WebKit::WebString;
using WebKit::WebRect;
using WebKit::WebURL;
using WebKit::WebVector;

namespace content {

namespace {
const char kCrashEventName[] = "crash";
const char kIsTopLevel[] = "isTopLevel";
const char kLoadAbortEventName[] = "loadAbort";
const char kLoadRedirectEventName[] = "loadRedirect";
const char kLoadStartEventName[] = "loadStart";
const char kNavigationEventName[] = "navigation";
const char kNewURL[] = "newUrl";
const char kOldURL[] = "oldUrl";
const char kPartitionAttribute[] = "partition";
const char kPersistPrefix[] = "persist:";
const char kSrcAttribute[] = "src";
const char kType[] = "type";
const char kURL[] = "url";
}

BrowserPlugin::BrowserPlugin(
    int instance_id,
    RenderViewImpl* render_view,
    WebKit::WebFrame* frame,
    const WebPluginParams& params)
    : instance_id_(instance_id),
      render_view_(render_view),
      container_(NULL),
      damage_buffer_(NULL),
      sad_guest_(NULL),
      guest_crashed_(false),
      resize_pending_(false),
      navigate_src_sent_(false),
      process_id_(-1),
      persist_storage_(false),
      visible_(true),
      current_nav_entry_index_(0),
      nav_entry_count_(0) {
  BrowserPluginManager::Get()->AddBrowserPlugin(instance_id, this);
  bindings_.reset(new BrowserPluginBindings(this));

  ParseAttributes(params);
}

BrowserPlugin::~BrowserPlugin() {
  if (damage_buffer_)
    FreeDamageBuffer();
  RemoveEventListeners();
  BrowserPluginManager::Get()->RemoveBrowserPlugin(instance_id_);
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_PluginDestroyed(
          render_view_->GetRoutingID(),
          instance_id_));
}

void BrowserPlugin::Cleanup() {
  if (damage_buffer_)
    FreeDamageBuffer();
}

std::string BrowserPlugin::GetSrcAttribute() const {
  return src_;
}

void BrowserPlugin::SetSrcAttribute(const std::string& src) {
  if (src.empty() || (src == src_ && !guest_crashed_))
    return;

  // If we haven't created the guest yet, do so now. We will navigate it right
  // after creation. If |src| is empty, we can delay the creation until we
  // acutally need it.
  if (!navigate_src_sent_) {
    BrowserPluginManager::Get()->Send(
        new BrowserPluginHostMsg_CreateGuest(
            render_view_->GetRoutingID(),
            instance_id_,
            storage_partition_id_,
            persist_storage_));
  }

  scoped_ptr<BrowserPluginHostMsg_ResizeGuest_Params> params(
      GetPendingResizeParams());
  DCHECK(!params->resize_pending);

  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_NavigateGuest(
          render_view_->GetRoutingID(),
          instance_id_,
          src,
          *params));
  // Record that we sent a NavigateGuest message to embedder.
  // Once this instance has navigated, the storage partition cannot be changed,
  // so this value is used for enforcing this.
  navigate_src_sent_ = true;
  src_ = src;
}

std::string BrowserPlugin::GetPartitionAttribute() const {
  std::string value;
  if (persist_storage_)
    value.append(kPersistPrefix);

  value.append(storage_partition_id_);
  return value;
}

bool BrowserPlugin::CanGoBack() const {
  return nav_entry_count_ > 1 && current_nav_entry_index_ > 0;
}

bool BrowserPlugin::CanGoForward() const {
  return current_nav_entry_index_ >= 0 &&
      current_nav_entry_index_ < (nav_entry_count_ - 1);
}

bool BrowserPlugin::SetPartitionAttribute(const std::string& partition_id,
                                          std::string& error_message) {
  if (navigate_src_sent_) {
    error_message =
      "The object has already navigated, so its partition cannot be changed.";
    return false;
  }

  std::string input = partition_id;

  // Since the "persist:" prefix is in ASCII, StartsWith will work fine on
  // UTF-8 encoded |partition_id|. If the prefix is a match, we can safely
  // remove the prefix without splicing in the middle of a multi-byte codepoint.
  // We can use the rest of the string as UTF-8 encoded one.
  if (StartsWithASCII(input, kPersistPrefix, true)) {
    size_t index = input.find(":");
    CHECK(index != std::string::npos);
    // It is safe to do index + 1, since we tested for the full prefix above.
    input = input.substr(index + 1);
    if (input.empty()) {
      error_message = "Invalid empty partition attribute.";
      return false;
    }
    persist_storage_ = true;
  } else {
    persist_storage_ = false;
  }

  storage_partition_id_ = input;
  return true;
}

void BrowserPlugin::ParseAttributes(const WebKit::WebPluginParams& params) {
  std::string src;

  // Get the src attribute from the attributes vector
  for (unsigned i = 0; i < params.attributeNames.size(); ++i) {
    std::string attributeName = params.attributeNames[i].utf8();
    if (LowerCaseEqualsASCII(attributeName, kSrcAttribute)) {
      src = params.attributeValues[i].utf8();
    } else if (LowerCaseEqualsASCII(attributeName, kPartitionAttribute)) {
      std::string error;
      SetPartitionAttribute(params.attributeValues[i].utf8(), error);
    }
  }

  // Set the 'src' attribute last, as it will set the has_navigated_ flag to
  // true, which prevents changing the 'partition' attribute.
  SetSrcAttribute(src);
}

float BrowserPlugin::GetDeviceScaleFactor() const {
  if (!render_view_)
    return 1.0f;
  return render_view_->GetWebView()->deviceScaleFactor();
}

void BrowserPlugin::RemoveEventListeners() {
  EventListenerMap::iterator event_listener_map_iter =
      event_listener_map_.begin();
  for (; event_listener_map_iter != event_listener_map_.end();
       ++event_listener_map_iter) {
    EventListeners& listeners =
        event_listener_map_[event_listener_map_iter->first];
    EventListeners::iterator it = listeners.begin();
    for (; it != listeners.end(); ++it) {
      it->Dispose();
    }
  }
  event_listener_map_.clear();
}

void BrowserPlugin::Back() {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_Go(render_view_->GetRoutingID(),
                                  instance_id_, -1));
}

void BrowserPlugin::Forward() {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_Go(render_view_->GetRoutingID(),
                                  instance_id_, 1));
}

void BrowserPlugin::Go(int relative_index) {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_Go(render_view_->GetRoutingID(),
                                  instance_id_,
                                  relative_index));
}

void BrowserPlugin::TerminateGuest() {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_TerminateGuest(render_view_->GetRoutingID(),
                                              instance_id_));
}

void BrowserPlugin::Stop() {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_Stop(render_view_->GetRoutingID(),
                                    instance_id_));
}

void BrowserPlugin::Reload() {
  if (!navigate_src_sent_)
    return;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_Reload(render_view_->GetRoutingID(),
                                      instance_id_));
}

void BrowserPlugin::UpdateRect(
    int message_id,
    const BrowserPluginMsg_UpdateRect_Params& params) {
  if (width() != params.view_size.width() ||
      height() != params.view_size.height()) {
    BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK(
        render_view_->GetRoutingID(),
        instance_id_,
        message_id,
        gfx::Size(width(), height())));
    return;
  }

  float backing_store_scale_factor =
      backing_store_.get() ? backing_store_->GetScaleFactor() : 1.0f;

  if (params.is_resize_ack ||
      backing_store_scale_factor != params.scale_factor) {
    resize_pending_ = !params.is_resize_ack;
    backing_store_.reset(
        new BrowserPluginBackingStore(gfx::Size(width(), height()),
                                      params.scale_factor));
  }

  // Update the backing store.
  if (!params.scroll_rect.IsEmpty()) {
    backing_store_->ScrollBackingStore(params.dx,
                                       params.dy,
                                       params.scroll_rect,
                                       params.view_size);
  }
  for (unsigned i = 0; i < params.copy_rects.size(); i++) {
    backing_store_->PaintToBackingStore(params.bitmap_rect,
                                        params.copy_rects,
                                        damage_buffer_);
  }
  // Invalidate the container.
  container_->invalidate();
  BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_UpdateRect_ACK(
      render_view_->GetRoutingID(),
      instance_id_,
      message_id,
      gfx::Size()));
}

void BrowserPlugin::GuestCrashed() {
  guest_crashed_ = true;
  container_->invalidate();
  // We won't paint the contents of the current backing store again so we might
  // as well toss it out and save memory.
  backing_store_.reset();

  if (!HasListeners(kCrashEventName))
    return;

  WebKit::WebElement plugin = container()->element();
  v8::HandleScope handle_scope;
  v8::Context::Scope context_scope(
      plugin.document().frame()->mainWorldScriptContext());

  // TODO(fsamuel): Copying the event listeners is insufficent because
  // new persistent handles are not created when the copy constructor is
  // called. See http://crbug.com/155044.
  EventListeners listeners(event_listener_map_[kCrashEventName]);
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    WebKit::WebFrame* frame = plugin.document().frame();
    if (frame)
      frame->callFunctionEvenIfScriptDisabled(*it, v8::Object::New(), 0, NULL);
  }
}

void BrowserPlugin::DidNavigate(
    const BrowserPluginMsg_DidNavigate_Params& params) {
  // If the guest has just committed a new navigation then it is no longer
  // crashed.
  guest_crashed_ = false;
  src_ = params.url.spec();
  process_id_ = params.process_id;
  current_nav_entry_index_ = params.current_entry_index;
  nav_entry_count_ = params.entry_count;

  if (!HasListeners(kNavigationEventName))
    return;

  WebKit::WebElement plugin = container()->element();
  v8::HandleScope handle_scope;
  v8::Context::Scope context_scope(
      plugin.document().frame()->mainWorldScriptContext());

  // Construct the navigation event object.
  v8::Local<v8::Object> event = v8::Object::New();
  event->Set(v8::String::New(kURL, sizeof(kURL) - 1),
             v8::String::New(src_.data(), src_.size()));
  event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1),
             v8::Boolean::New(params.is_top_level));
  v8::Local<v8::Value> val = event;

  // TODO(fsamuel): Copying the event listeners is insufficent because
  // new persistent handles are not created when the copy constructor is
  // called. See http://crbug.com/155044.
  EventListeners listeners(event_listener_map_[kNavigationEventName]);
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    WebKit::WebFrame* frame = plugin.document().frame();
    if (frame) {
      frame->callFunctionEvenIfScriptDisabled(
          *it, v8::Object::New(), 1, &val);
    }
  }
}

void BrowserPlugin::LoadStart(const GURL& url, bool is_top_level) {
  if (!HasListeners(kLoadStartEventName))
    return;

  WebKit::WebElement plugin = container()->element();
  v8::HandleScope handle_scope;
  v8::Context::Scope context_scope(
      plugin.document().frame()->mainWorldScriptContext());

  // Construct the loadStart event object.
  v8::Local<v8::Object> event = v8::Object::New();
  event->Set(v8::String::New(kURL, sizeof(kURL) - 1),
             v8::String::New(url.spec().data(), url.spec().size()));
  event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1),
             v8::Boolean::New(is_top_level));
  v8::Local<v8::Value> val = event;

  // TODO(fsamuel): Copying the event listeners is insufficent because
  // new persistent handles are not created when the copy constructor is
  // called. See http://crbug.com/155044.
  EventListeners listeners(event_listener_map_[kLoadStartEventName]);
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    WebKit::WebFrame* frame = plugin.document().frame();
    if (frame)
      frame->callFunctionEvenIfScriptDisabled(*it, v8::Object::New(), 1, &val);
  }
}

void BrowserPlugin::LoadAbort(const GURL& url,
                              bool is_top_level,
                              const std::string& type) {
  if (!HasListeners(kLoadAbortEventName))
    return;

  WebKit::WebElement plugin = container()->element();
  v8::HandleScope handle_scope;
  v8::Context::Scope context_scope(
      plugin.document().frame()->mainWorldScriptContext());

  // Construct the loadAbort event object.
  v8::Local<v8::Object> event = v8::Object::New();
  event->Set(v8::String::New(kURL, sizeof(kURL) - 1),
             v8::String::New(url.spec().data(), url.spec().size()));
  event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1),
             v8::Boolean::New(is_top_level));
  event->Set(v8::String::New(kType, sizeof(kType) - 1),
             v8::String::New(type.data(), type.size()));
  v8::Local<v8::Value> val = event;

  // TODO(fsamuel): Copying the event listeners is insufficent because
  // new persistent handles are not created when the copy constructor is
  // called. See http://crbug.com/155044.
  EventListeners listeners(event_listener_map_[kLoadAbortEventName]);
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    WebKit::WebFrame* frame = plugin.document().frame();
    if (frame)
      frame->callFunctionEvenIfScriptDisabled(*it, v8::Object::New(), 1, &val);
  }
}

void BrowserPlugin::LoadRedirect(const GURL& old_url,
                                 const GURL& new_url,
                                 bool is_top_level) {
  if (!HasListeners(kLoadRedirectEventName))
    return;

  WebKit::WebElement plugin = container()->element();
  v8::HandleScope handle_scope;
  v8::Context::Scope context_scope(
      plugin.document().frame()->mainWorldScriptContext());

  // Construct the loadRedirect event object.
  v8::Local<v8::Object> event = v8::Object::New();
  event->Set(v8::String::New(kOldURL, sizeof(kOldURL) - 1),
             v8::String::New(old_url.spec().data(), old_url.spec().size()));
  event->Set(v8::String::New(kNewURL, sizeof(kNewURL) - 1),
             v8::String::New(new_url.spec().data(), new_url.spec().size()));
  event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1),
             v8::Boolean::New(is_top_level));
  v8::Local<v8::Value> val = event;

  // TODO(fsamuel): Copying the event listeners is insufficent because
  // new persistent handles are not created when the copy constructor is
  // called. See http://crbug.com/155044.
  EventListeners listeners(event_listener_map_[kLoadRedirectEventName]);
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    WebKit::WebFrame* frame = plugin.document().frame();
    if (frame)
      frame->callFunctionEvenIfScriptDisabled(*it, v8::Object::New(), 1, &val);
  }
}

void BrowserPlugin::AdvanceFocus(bool reverse) {
  // We do not have a RenderView when we are testing.
  if (render_view_)
    render_view_->GetWebView()->advanceFocus(reverse);
}

void BrowserPlugin::SetAcceptTouchEvents(bool accept) {
  if (container())
    container()->setIsAcceptingTouchEvents(accept);
}

bool BrowserPlugin::HasListeners(const std::string& event_name) {
  return event_listener_map_.find(event_name) != event_listener_map_.end();
}

bool BrowserPlugin::AddEventListener(const std::string& event_name,
                                     v8::Local<v8::Function> function) {
  EventListeners& listeners = event_listener_map_[event_name];
  for (unsigned int i = 0; i < listeners.size(); ++i) {
    if (listeners[i] == function)
      return false;
  }
  v8::Persistent<v8::Function> persistent_function =
      v8::Persistent<v8::Function>::New(function);
  listeners.push_back(persistent_function);
  return true;
}

bool BrowserPlugin::RemoveEventListener(const std::string& event_name,
                                        v8::Local<v8::Function> function) {
  if (event_listener_map_.find(event_name) == event_listener_map_.end())
    return false;

  EventListeners& listeners = event_listener_map_[event_name];
  EventListeners::iterator it = listeners.begin();
  for (; it != listeners.end(); ++it) {
    if (*it == function) {
      it->Dispose();
      listeners.erase(it);
      return true;
    }
  }
  return false;
}

WebKit::WebPluginContainer* BrowserPlugin::container() const {
  return container_;
}

bool BrowserPlugin::initialize(WebPluginContainer* container) {
  container_ = container;
  return true;
}

void BrowserPlugin::destroy() {
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

NPObject* BrowserPlugin::scriptableObject() {
  NPObject* browser_plugin_np_object(bindings_->np_object());
  // The object is expected to be retained before it is returned.
  WebKit::WebBindings::retainObject(browser_plugin_np_object);
  return browser_plugin_np_object;
}

bool BrowserPlugin::supportsKeyboardFocus() const {
  return true;
}

void BrowserPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
  if (guest_crashed_) {
    if (!sad_guest_)  // Lazily initialize bitmap.
      sad_guest_ = content::GetContentClient()->renderer()->
          GetSadPluginBitmap();
    // TODO(fsamuel): Do we want to paint something other than a sad plugin
    // on crash? See http://www.crbug.com/140266.
    // content_shell does not have the sad plugin bitmap, so we'll paint black
    // instead to make it clear that something went wrong.
    if (sad_guest_) {
      webkit::PaintSadPlugin(canvas, plugin_rect_, *sad_guest_);
      return;
    }
  }
  SkAutoCanvasRestore auto_restore(canvas, true);
  canvas->translate(plugin_rect_.x(), plugin_rect_.y());
  SkRect image_data_rect = SkRect::MakeXYWH(
      SkIntToScalar(0),
      SkIntToScalar(0),
      SkIntToScalar(plugin_rect_.width()),
      SkIntToScalar(plugin_rect_.height()));
  canvas->clipRect(image_data_rect);
  // Paint black or white in case we have nothing in our backing store or we
  // need to show a gutter.
  SkPaint paint;
  paint.setStyle(SkPaint::kFill_Style);
  paint.setColor(guest_crashed_ ? SK_ColorBLACK : SK_ColorWHITE);
  canvas->drawRect(image_data_rect, paint);
  // Stay a solid color if we have never set a non-empty src, or we don't have a
  // backing store.
  if (!backing_store_.get() || !navigate_src_sent_)
    return;
  float inverse_scale_factor =  1.0f / backing_store_->GetScaleFactor();
  canvas->scale(inverse_scale_factor, inverse_scale_factor);
  canvas->drawBitmap(backing_store_->GetBitmap(), 0, 0);
}

void BrowserPlugin::updateGeometry(
    const WebRect& window_rect,
    const WebRect& clip_rect,
    const WebVector<WebRect>& cut_outs_rects,
    bool is_visible) {
  int old_width = width();
  int old_height = height();
  plugin_rect_ = window_rect;
  if (old_width == window_rect.width &&
      old_height == window_rect.height) {
    return;
  }

  const size_t stride = skia::PlatformCanvas::StrideForWidth(window_rect.width);
  // Make sure the size of the damage buffer is at least four bytes so that we
  // can fit in a magic word to verify that the memory is shared correctly.
  size_t size =
      std::max(sizeof(unsigned int),
               static_cast<size_t>(window_rect.height *
                                   stride *
                                   GetDeviceScaleFactor() *
                                   GetDeviceScaleFactor()));

  // Don't drop the old damage buffer until after we've made sure that the
  // browser process has dropped it.
  TransportDIB* new_damage_buffer = CreateTransportDIB(size);
  pending_resize_params_.reset();

  scoped_ptr<BrowserPluginHostMsg_ResizeGuest_Params> params(
      new BrowserPluginHostMsg_ResizeGuest_Params);
  params->damage_buffer_id = new_damage_buffer->id();
#if defined(OS_MACOSX)
  // |damage_buffer_id| is not enough to retrieve the damage buffer (on browser
  // side) since we don't let the browser cache the damage buffer. We need a
  // handle to the damage buffer for this.
  params->damage_buffer_handle = new_damage_buffer->handle();
#endif
#if defined(OS_WIN)
  params->damage_buffer_size = size;
#endif
  params->width = window_rect.width;
  params->height = window_rect.height;
  params->resize_pending = resize_pending_;
  params->scale_factor = GetDeviceScaleFactor();

  if (navigate_src_sent_) {
    BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_ResizeGuest(
        render_view_->GetRoutingID(),
        instance_id_,
        *params));
    resize_pending_ = true;
  } else {
    // Until an actual navigation occurs, there is no browser-side embedder
    // present to notify about geometry updates. In this case, after we've
    // updated the BrowserPlugin's state we are done and we do not send a resize
    // message to the browser.
    pending_resize_params_.reset(params.release());
  }
  if (damage_buffer_)
    FreeDamageBuffer();
  damage_buffer_ = new_damage_buffer;
}

void BrowserPlugin::FreeDamageBuffer() {
  DCHECK(damage_buffer_);
#if defined(OS_MACOSX)
  // We don't need to (nor should we) send ViewHostMsg_FreeTransportDIB
  // message to the browser to free the damage buffer since we manage the
  // damage buffer ourselves.
  delete damage_buffer_;
#else
  RenderProcess::current()->FreeTransportDIB(damage_buffer_);
  damage_buffer_ = NULL;
#endif
}

BrowserPluginHostMsg_ResizeGuest_Params*
    BrowserPlugin::GetPendingResizeParams() {
  if (pending_resize_params_.get()) {
    resize_pending_ = true;
    return pending_resize_params_.release();
  } else {
    BrowserPluginHostMsg_ResizeGuest_Params* params =
        new BrowserPluginHostMsg_ResizeGuest_Params;

    // We don't have a pending resize to send, so we send an invalid transport
    // dib Id.
    params->damage_buffer_id = TransportDIB::Id();
    params->width = width();
    params->height = height();
    params->resize_pending = false;
    return params;
  }
}

TransportDIB* BrowserPlugin::CreateTransportDIB(const size_t size) {
#if defined(OS_MACOSX)
  TransportDIB::Handle handle;
  // On OSX we don't let the browser manage the transport dib. We manage the
  // deletion of the dib in FreeDamageBuffer().
  IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(
      size,
      false,  // cache in browser.
      &handle);
  TransportDIB* new_damage_buffer = NULL;
  if (BrowserPluginManager::Get()->Send(msg) && handle.fd >= 0)
    new_damage_buffer = TransportDIB::Map(handle);
#else
  TransportDIB* new_damage_buffer =
      RenderProcess::current()->CreateTransportDIB(size);
#endif
  if (!new_damage_buffer)
    NOTREACHED() << "Unable to create damage buffer";
#if defined(OS_WIN)
  // Windows does not map the buffer by default.
  CHECK(new_damage_buffer->Map());
#endif
  DCHECK(new_damage_buffer->memory());
  // Insert the magic word.
  *static_cast<unsigned int*>(new_damage_buffer->memory()) = 0xdeadbeef;
  return new_damage_buffer;
}

void BrowserPlugin::updateFocus(bool focused) {
  BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_SetFocus(
      render_view_->GetRoutingID(),
      instance_id_,
      focused));
}

void BrowserPlugin::updateVisibility(bool visible) {
  if (visible_ == visible)
    return;

  visible_ = visible;
  if (!navigate_src_sent_)
    return;

  BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_SetVisibility(
      render_view_->GetRoutingID(),
      instance_id_,
      visible));
}

bool BrowserPlugin::acceptsInputEvents() {
  return true;
}

bool BrowserPlugin::handleInputEvent(const WebKit::WebInputEvent& event,
                                     WebKit::WebCursorInfo& cursor_info) {
  if (guest_crashed_ || !navigate_src_sent_)
    return false;
  bool handled = false;
  WebCursor cursor;
  IPC::Message* message =
      new BrowserPluginHostMsg_HandleInputEvent(
          render_view_->GetRoutingID(),
          &handled,
          &cursor);
  message->WriteInt(instance_id_);
  message->WriteData(reinterpret_cast<const char*>(&plugin_rect_),
                     sizeof(gfx::Rect));
  message->WriteData(reinterpret_cast<const char*>(&event), event.size);
  BrowserPluginManager::Get()->Send(message);
  cursor.GetCursorInfo(&cursor_info);
  return handled;
}

bool BrowserPlugin::handleDragStatusUpdate(WebKit::WebDragStatus drag_status,
                                           const WebKit::WebDragData& drag_data,
                                           WebKit::WebDragOperationsMask mask,
                                           const WebKit::WebPoint& position,
                                           const WebKit::WebPoint& screen) {
  if (guest_crashed_ || !navigate_src_sent_)
    return false;
  BrowserPluginManager::Get()->Send(
      new BrowserPluginHostMsg_DragStatusUpdate(
        render_view_->GetRoutingID(),
        instance_id_,
        drag_status,
        WebDropData(drag_data),
        mask,
        position));
  return false;
}

void BrowserPlugin::didReceiveResponse(
    const WebKit::WebURLResponse& response) {
}

void BrowserPlugin::didReceiveData(const char* data, int data_length) {
}

void BrowserPlugin::didFinishLoading() {
}

void BrowserPlugin::didFailLoading(const WebKit::WebURLError& error) {
}

void BrowserPlugin::didFinishLoadingFrameRequest(const WebKit::WebURL& url,
                                                 void* notify_data) {
}

void BrowserPlugin::didFailLoadingFrameRequest(
    const WebKit::WebURL& url,
    void* notify_data,
    const WebKit::WebURLError& error) {
}

}  // namespace content