summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_flash_proxy.cc
blob: 5614e6a4656c6661eedd282073455b45559a56b9 (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
// 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 "ppapi/proxy/ppb_flash_proxy.h"

#include "base/logging.h"
#include "build/build_config.h"
#include "ppapi/c/dev/pp_file_info_dev.h"
#include "ppapi/c/dev/ppb_font_dev.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/serialized_var.h"
#include "webkit/plugins/ppapi/ppb_flash.h"

namespace pp {
namespace proxy {

namespace {

// Given an error code and a handle result from a Pepper API call, converts
// to a PlatformFileForTransit, possibly also updating the error value if
// an error occurred.
IPC::PlatformFileForTransit PlatformFileToPlatformFileForTransit(
    int32_t* error,
    base::PlatformFile file) {
  if (*error != PP_OK)
    return IPC::InvalidPlatformFileForTransit();
#if defined(OS_WIN)
/* TODO(brettw): figure out how to get the target process handle.
  HANDLE result;
  if (!::DuplicateHandle(::GetCurrentProcess(), file,
                         target_process, &result, 0, false,
                         DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
    *error = PP_ERROR_NOACCESS;
    return INVALID_HANDLE_VALUE;
  }
  return result;
*/
  NOTIMPLEMENTED();
  *error = PP_ERROR_NOACCESS;
  return INVALID_HANDLE_VALUE;
#elif defined(OS_POSIX)
  return base::FileDescriptor(file, true);
#endif
}

void SetInstanceAlwaysOnTop(PP_Instance pp_instance, bool on_top) {
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBFlash_SetInstanceAlwaysOnTop(
          INTERFACE_ID_PPB_FLASH, pp_instance, on_top));
}

bool DrawGlyphs(PP_Resource pp_image_data,
                const PP_FontDescription_Dev* font_desc,
                uint32_t color,
                PP_Point position,
                PP_Rect clip,
                const float transformation[3][3],
                uint32_t glyph_count,
                const uint16_t glyph_indices[],
                const PP_Point glyph_advances[]) {
  Dispatcher* dispatcher = PluginDispatcher::Get();

  PPBFlash_DrawGlyphs_Params params;
  params.pp_image_data = pp_image_data;
  params.font_desc.SetFromPPFontDescription(dispatcher, *font_desc, true);
  params.color = color;
  params.position = position;
  params.clip = clip;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++)
      params.transformation[i][j] = transformation[i][j];
  }

  params.glyph_indices.insert(params.glyph_indices.begin(),
                              &glyph_indices[0],
                              &glyph_indices[glyph_count]);
  params.glyph_advances.insert(params.glyph_advances.begin(),
                               &glyph_advances[0],
                               &glyph_advances[glyph_count]);

  bool result = false;
  dispatcher->Send(new PpapiHostMsg_PPBFlash_DrawGlyphs(
      INTERFACE_ID_PPB_FLASH, params, &result));
  return result;
}

PP_Var GetProxyForURL(PP_Module pp_module, const char* url) {
  ReceiveSerializedVarReturnValue result;
  PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBFlash_GetProxyForURL(
      INTERFACE_ID_PPB_FLASH, pp_module, url, &result));
  return result.Return(PluginDispatcher::Get());
}

int32_t OpenModuleLocalFile(PP_Module module,
                            const char* path,
                            int32_t mode,
                            PP_FileHandle* file) {
  int32_t result = PP_ERROR_FAILED;
  IPC::PlatformFileForTransit transit;
  PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBFlash_OpenModuleLocalFile(
      INTERFACE_ID_PPB_FLASH, module, path, mode, &transit, &result));
  *file = IPC::PlatformFileForTransitToPlatformFile(transit);
  return result;
}

int32_t RenameModuleLocalFile(PP_Module module,
                              const char* path_from,
                              const char* path_to) {
  int32_t result = PP_ERROR_FAILED;
  PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBFlash_RenameModuleLocalFile(
      INTERFACE_ID_PPB_FLASH, module, path_from, path_to, &result));
  return result;
}

int32_t DeleteModuleLocalFileOrDir(PP_Module module,
                                   const char* path,
                                   bool recursive) {
  int32_t result = PP_ERROR_FAILED;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBFlash_DeleteModuleLocalFileOrDir(
          INTERFACE_ID_PPB_FLASH, module, path, recursive, &result));
  return result;
}

int32_t CreateModuleLocalDir(PP_Module module, const char* path) {
  int32_t result = PP_ERROR_FAILED;
  PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBFlash_CreateModuleLocalDir(
      INTERFACE_ID_PPB_FLASH, module, path, &result));
  return result;
}

int32_t QueryModuleLocalFile(PP_Module module,
                             const char* path,
                             PP_FileInfo_Dev* info) {
  int32_t result = PP_ERROR_FAILED;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBFlash_QueryModuleLocalFile(
          INTERFACE_ID_PPB_FLASH, module, path, info, &result));
  return result;
}

int32_t GetModuleLocalDirContents(PP_Module module,
                                  const char* path,
                                  PP_DirContents_Dev** contents) {
  int32_t result = PP_ERROR_FAILED;
  std::vector<SerializedDirEntry> entries;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBFlash_GetModuleLocalDirContents(
          INTERFACE_ID_PPB_FLASH, module, path, &entries, &result));

  if (result != PP_OK)
    return result;

  // Copy the serialized dir entries to the output struct.
  *contents = new PP_DirContents_Dev;
  (*contents)->count = static_cast<int32_t>(entries.size());
  (*contents)->entries = new PP_DirEntry_Dev[entries.size()];
  for (size_t i = 0; i < entries.size(); i++) {
    const SerializedDirEntry& source = entries[i];
    PP_DirEntry_Dev* dest = &(*contents)->entries[i];

    char* name_copy = new char[source.name.size() + 1];
    memcpy(name_copy, source.name.c_str(), source.name.size() + 1);
    dest->name = name_copy;
    dest->is_dir = source.is_dir;
  }

  return result;
}

void FreeModuleLocalDirContents(PP_Module module,
                                PP_DirContents_Dev* contents) {
  for (int32_t i = 0; i < contents->count; ++i)
    delete[] contents->entries[i].name;
  delete[] contents->entries;
  delete contents;
}

bool NavigateToURL(PP_Instance pp_instance,
                   const char* url,
                   const char* target) {
  bool result = false;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBFlash_NavigateToURL(
          INTERFACE_ID_PPB_FLASH, pp_instance, url, target, &result));
  return result;
}

const PPB_Flash ppb_flash = {
  &SetInstanceAlwaysOnTop,
  &DrawGlyphs,
  &GetProxyForURL,
  &OpenModuleLocalFile,
  &RenameModuleLocalFile,
  &DeleteModuleLocalFileOrDir,
  &CreateModuleLocalDir,
  &QueryModuleLocalFile,
  &GetModuleLocalDirContents,
  &FreeModuleLocalDirContents,
  &NavigateToURL,
};

}  // namespace

PPB_Flash_Proxy::PPB_Flash_Proxy(Dispatcher* dispatcher,
                                   const void* target_interface)
    : InterfaceProxy(dispatcher, target_interface) {
}

PPB_Flash_Proxy::~PPB_Flash_Proxy() {
}

const void* PPB_Flash_Proxy::GetSourceInterface() const {
  return &ppb_flash;
}

InterfaceID PPB_Flash_Proxy::GetInterfaceId() const {
  return INTERFACE_ID_PPB_FLASH;
}

void PPB_Flash_Proxy::OnMessageReceived(const IPC::Message& msg) {
  IPC_BEGIN_MESSAGE_MAP(PPB_Flash_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_SetInstanceAlwaysOnTop,
                        OnMsgSetInstanceAlwaysOnTop)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_DrawGlyphs,
                        OnMsgDrawGlyphs)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_GetProxyForURL,
                        OnMsgGetProxyForURL)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_OpenModuleLocalFile,
                        OnMsgOpenModuleLocalFile)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_RenameModuleLocalFile,
                        OnMsgRenameModuleLocalFile)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_DeleteModuleLocalFileOrDir,
                        OnMsgDeleteModuleLocalFileOrDir)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_CreateModuleLocalDir,
                        OnMsgCreateModuleLocalDir)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_QueryModuleLocalFile,
                        OnMsgQueryModuleLocalFile)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_GetModuleLocalDirContents,
                        OnMsgGetModuleLocalDirContents)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlash_NavigateToURL, OnMsgNavigateToURL)
  IPC_END_MESSAGE_MAP()
  // TODO(brettw) handle bad messages!
}

void PPB_Flash_Proxy::OnMsgSetInstanceAlwaysOnTop(
    PP_Instance instance,
    bool on_top) {
  ppb_flash_target()->SetInstanceAlwaysOnTop(instance, on_top);
}

void PPB_Flash_Proxy::OnMsgDrawGlyphs(
    const pp::proxy::PPBFlash_DrawGlyphs_Params& params,
    bool* result) {
  *result = false;

  PP_FontDescription_Dev font_desc;
  params.font_desc.SetToPPFontDescription(dispatcher(), &font_desc, false);

  if (params.glyph_indices.size() != params.glyph_advances.size() ||
      params.glyph_indices.empty())
    return;

  *result = ppb_flash_target()->DrawGlyphs(
      params.pp_image_data, &font_desc, params.color,
      params.position, params.clip,
      const_cast<float(*)[3]>(params.transformation),
      static_cast<uint32_t>(params.glyph_indices.size()),
      const_cast<uint16_t*>(&params.glyph_indices[0]),
      const_cast<PP_Point*>(&params.glyph_advances[0]));
}

void PPB_Flash_Proxy::OnMsgGetProxyForURL(PP_Module module,
                                          const std::string& url,
                                          SerializedVarReturnValue result) {
  result.Return(dispatcher(), ppb_flash_target()->GetProxyForURL(
      module, url.c_str()));
}

void PPB_Flash_Proxy::OnMsgOpenModuleLocalFile(
    PP_Module module,
    const std::string& path,
    int32_t mode,
    IPC::PlatformFileForTransit* file_handle,
    int32_t* result) {
  base::PlatformFile file;
  *result = ppb_flash_target()->OpenModuleLocalFile(module, path.c_str(), mode,
                                                    &file);
  *file_handle = PlatformFileToPlatformFileForTransit(result, file);
}

void PPB_Flash_Proxy::OnMsgRenameModuleLocalFile(
    PP_Module module,
    const std::string& path_from,
    const std::string& path_to,
    int32_t* result) {
  *result = ppb_flash_target()->RenameModuleLocalFile(module, path_from.c_str(),
                                                      path_to.c_str());
}

void PPB_Flash_Proxy::OnMsgDeleteModuleLocalFileOrDir(
    PP_Module module,
    const std::string& path,
    bool recursive,
    int32_t* result) {
  *result = ppb_flash_target()->DeleteModuleLocalFileOrDir(module, path.c_str(),
                                                           recursive);
}

void PPB_Flash_Proxy::OnMsgCreateModuleLocalDir(PP_Module module,
                                                const std::string& path,
                                                int32_t* result) {
  *result = ppb_flash_target()->CreateModuleLocalDir(module, path.c_str());
}

void PPB_Flash_Proxy::OnMsgQueryModuleLocalFile(PP_Module module,
                                                const std::string& path,
                                                PP_FileInfo_Dev* info,
                                                int32_t* result) {
  *result = ppb_flash_target()->QueryModuleLocalFile(module, path.c_str(),
                                                     info);
}

void PPB_Flash_Proxy::OnMsgGetModuleLocalDirContents(
    PP_Module module,
    const std::string& path,
    std::vector<pp::proxy::SerializedDirEntry>* entries,
    int32_t* result) {
  PP_DirContents_Dev* contents = NULL;
  *result = ppb_flash_target()->GetModuleLocalDirContents(module, path.c_str(),
                                                          &contents);
  if (*result != PP_OK)
    return;

  // Convert the list of entries to the serialized version.
  entries->resize(contents->count);
  for (int32_t i = 0; i < contents->count; i++) {
    (*entries)[i].name.assign(contents->entries[i].name);
    (*entries)[i].is_dir = contents->entries[i].is_dir;
  }
  ppb_flash_target()->FreeModuleLocalDirContents(module, contents);
}

void PPB_Flash_Proxy::OnMsgNavigateToURL(PP_Instance instance,
                                         const std::string& url,
                                         const std::string& target,
                                         bool* result) {
  *result = ppb_flash_target()->NavigateToURL(instance, url.c_str(),
                                              target.c_str());
}

}  // namespace proxy
}  // namespace pp