resource_converter.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#ifndef CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#define CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <vector>
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/basictypes.h"
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/callback.h"
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/compiler_specific.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/memory/ref_counted.h"
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/common/content_export.h"
154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/renderer/pepper/host_resource_var.h"
16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ppapi/c/pp_instance.h"
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ppapi/c/pp_var.h"
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "v8/include/v8.h"
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace IPC {
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class Message;
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace ppapi {
25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class ScopedPPVar;
26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace content {
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class RendererPpapiHost;
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// This class is responsible for converting V8 vars to Pepper resources.
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class CONTENT_EXPORT ResourceConverter {
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual ~ResourceConverter();
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Flush() must be called before any vars created by the ResourceConverter
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // are valid. It handles creating any resource hosts that need to be created.
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  virtual void Flush(const base::Callback<void(bool)>& callback) = 0;
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Attempts to convert a V8 object to a PP_Var with type PP_VARTYPE_RESOURCE.
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // On success, writes the resulting var to |result|, sets |was_resource| to
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // true and returns true. If |val| is not a resource, sets |was_resource| to
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // false and returns true. If an error occurs, returns false.
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual bool FromV8Value(v8::Handle<v8::Object> val,
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           v8::Handle<v8::Context> context,
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           PP_Var* result,
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           bool* was_resource) = 0;
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Attempts to convert a PP_Var to a V8 object. |var| must have type
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // PP_VARTYPE_RESOURCE. On success, writes the resulting value to |result| and
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // returns true. If an error occurs, returns false.
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool ToV8Value(const PP_Var& var,
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         v8::Handle<v8::Context> context,
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         v8::Handle<v8::Value>* result) = 0;
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class ResourceConverterImpl : public ResourceConverter {
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ResourceConverterImpl(PP_Instance instance, RendererPpapiHost* host);
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual ~ResourceConverterImpl();
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // ResourceConverter overrides.
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void Flush(const base::Callback<void(bool)>& callback) OVERRIDE;
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual bool FromV8Value(v8::Handle<v8::Object> val,
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           v8::Handle<v8::Context> context,
674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           PP_Var* result,
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           bool* was_resource) OVERRIDE;
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool ToV8Value(const PP_Var& var,
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         v8::Handle<v8::Context> context,
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         v8::Handle<v8::Value>* result) OVERRIDE;
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Creates a resource var with the given |pending_renderer_id| and
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |create_message| to be sent to the plugin.
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<HostResourceVar> CreateResourceVar(
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      int pending_renderer_id,
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const IPC::Message& create_message);
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Creates a resource var with the given |pending_renderer_id| and
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |create_message| to be sent to the plugin. Also sends
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |browser_host_create_message| to the browser, and asynchronously stores the
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // resulting browser host ID in the newly created var.
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<HostResourceVar> CreateResourceVarWithBrowserHost(
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      int pending_renderer_id,
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const IPC::Message& create_message,
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const IPC::Message& browser_host_create_message);
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The instance this ResourceConverter is associated with.
89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  PP_Instance instance_;
90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The RendererPpapiHost to use to create browser hosts.
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  RendererPpapiHost* host_;
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A list of the messages to create the browser hosts. This is a parallel
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // array to |browser_vars|. It is kept as a parallel array so that it can be
954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // conveniently passed to |CreateBrowserResourceHosts|.
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<IPC::Message> browser_host_create_messages_;
97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A list of the resource vars associated with browser hosts.
984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::vector<scoped_refptr<HostResourceVar> > browser_vars;
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl);
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace content
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif  // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
105