summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/pepper_widget.cc
blob: 9f05f2bf8224bacf1151fd6a12f9661b6c535a68 (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
// Copyright (c) 2010 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 "chrome/renderer/pepper_widget.h"

#include "base/hash_tables.h"
#include "base/lazy_instance.h"
#include "chrome/renderer/pepper_scrollbar_widget.h"
#include "chrome/renderer/webplugin_delegate_pepper.h"
#include "skia/ext/platform_canvas.h"
#include "webkit/glue/plugins/plugin_instance.h"
#include "webkit/glue/plugins/webplugin.h"
#include "webkit/glue/plugins/webplugin_delegate.h"

#if defined(OS_WIN)
#include "base/win_util.h"
#endif

static int g_next_id;
typedef base::hash_map<int, PepperWidget*> WidgetMap;
static base::LazyInstance<WidgetMap> g_widgets(base::LINKER_INITIALIZED);

NPError NPCreateWidget(NPP instance,
                       NPWidgetType type,
                       void* params,
                       NPWidgetID* id) {
  PepperWidget* widget;
  switch (type) {
    case NPWidgetTypeScrollbar:
      widget = new PepperScrollbarWidget(
          *static_cast<NPScrollbarCreateParams*>(params));
      break;
    default:
      return NPERR_INVALID_PARAM;
  }

  *id = ++g_next_id;
  widget->Init(instance, *id);
  return NPERR_NO_ERROR;
}

NPError NPDestroyWidget(NPP instance, NPWidgetID id) {
  WidgetMap::iterator iter = g_widgets.Get().find(id);
  if (iter == g_widgets.Get().end())
    return NPERR_INVALID_PARAM;

  iter->second->Destroy();
  return NPERR_NO_ERROR;
}

NPError NPPaintWidget(NPP instance,
                      NPWidgetID id,
                      NPDeviceContext2D* context,
                      NPRect* dirty) {
  WidgetMap::iterator iter = g_widgets.Get().find(id);
  if (iter == g_widgets.Get().end())
    return NPERR_INVALID_PARAM;

  NPAPI::PluginInstance* plugin =
      static_cast<NPAPI::PluginInstance*>(instance->ndata);
  WebPluginDelegatePepper* delegate =
      static_cast<WebPluginDelegatePepper*>(plugin->webplugin()->delegate());
  Graphics2DDeviceContext* gdc = delegate->GetGraphicsContext(context);
  iter->second->Paint(gdc, *dirty);

#if defined(OS_WIN)
  if (win_util::GetWinVersion() == win_util::WINVERSION_XP) {
    gdc->canvas()->getTopPlatformDevice().makeOpaque(
        dirty->left, dirty->top, dirty->right - dirty->left,
        dirty->bottom - dirty->top);
  }
#endif
  return NPERR_NO_ERROR;
}

bool NPHandleWidgetEvent(NPP instance, NPWidgetID id, NPPepperEvent* event) {
  WidgetMap::iterator iter = g_widgets.Get().find(id);
  if (iter == g_widgets.Get().end())
    return false;

  return iter->second->HandleEvent(*event);
}

NPError NPGetWidgetProperty(NPP instance,
                            NPWidgetID id,
                            NPWidgetProperty property,
                            void* value) {
  WidgetMap::iterator iter = g_widgets.Get().find(id);
  if (iter == g_widgets.Get().end())
    return NPERR_INVALID_PARAM;

  iter->second->GetProperty(property, value);
  return NPERR_NO_ERROR;
}

NPError NPSetWidgetProperty(NPP instance,
                            NPWidgetID id,
                            NPWidgetProperty property,
                            void* value) {
  WidgetMap::iterator iter = g_widgets.Get().find(id);
  if (iter == g_widgets.Get().end())
    return NPERR_INVALID_PARAM;

  iter->second->SetProperty(property, value);
  return NPERR_NO_ERROR;
}

NPWidgetExtensions g_widget_extensions = {
  NPCreateWidget,
  NPDestroyWidget,
  NPPaintWidget,
  NPHandleWidgetEvent,
  NPGetWidgetProperty,
  NPSetWidgetProperty
};

// static
NPWidgetExtensions* PepperWidget::GetWidgetExtensions() {
  return &g_widget_extensions;
}

PepperWidget::PepperWidget() : instance_(NULL), id_(0) {
}

PepperWidget::~PepperWidget() {
  if (id_)
    g_widgets.Get().erase(id_);
}

void PepperWidget::Init(NPP instance, int id) {
  instance_ = instance;
  id_ = id;
  g_widgets.Get()[id] = this;
}

void PepperWidget::WidgetPropertyChanged(NPWidgetProperty property) {
  NPAPI::PluginInstance* instance =
      static_cast<NPAPI::PluginInstance*>(instance_->ndata);
  NPPExtensions* extensions = NULL;
  instance->NPP_GetValue(NPPVPepperExtensions, &extensions);
  if (!extensions)
    return;

  extensions->widgetPropertyChanged(instance_, id_, property);
}