| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H | 5 #ifndef CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H |
| 6 #define CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H | 6 #define CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 14 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 15 #include "content/renderer/pepper/host_resource_var.h" | 16 #include "content/renderer/pepper/host_resource_var.h" |
| 16 #include "ppapi/c/pp_instance.h" | 17 #include "ppapi/c/pp_instance.h" |
| 17 #include "ppapi/c/pp_var.h" | 18 #include "ppapi/c/pp_var.h" |
| 18 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 19 | 20 |
| 20 namespace IPC { | 21 namespace IPC { |
| 21 class Message; | 22 class Message; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace ppapi { | 25 namespace ppapi { |
| 26 |
| 25 class ScopedPPVar; | 27 class ScopedPPVar; |
| 28 |
| 29 namespace proxy { |
| 30 struct CompletedBrowserResourceHosts; |
| 26 } | 31 } |
| 27 | 32 |
| 33 } // namespace ppapi |
| 34 |
| 28 namespace content { | 35 namespace content { |
| 29 | 36 |
| 30 // This class is responsible for converting V8 vars to Pepper resources. | 37 // This class is responsible for converting V8 vars to Pepper resources. |
| 31 class CONTENT_EXPORT ResourceConverter { | 38 class CONTENT_EXPORT ResourceConverter { |
| 32 public: | 39 public: |
| 33 virtual ~ResourceConverter(); | 40 virtual ~ResourceConverter(); |
| 34 | 41 |
| 35 // Reset the state of the resource converter. | 42 // Reset the state of the resource converter. |
| 36 virtual void Reset() = 0; | 43 virtual void Reset() = 0; |
| 37 | 44 |
| 38 // Returns true if Flush() needs to be called before using any vars created | 45 // Returns true if Flush() needs to be called before using any vars created |
| 39 // by the resource converter. | 46 // by the resource converter. |
| 40 virtual bool NeedsFlush() = 0; | 47 virtual bool NeedsFlush() = 0; |
| 41 | 48 |
| 42 // If NeedsFlush() is true then Flush() must be called before any vars created | 49 // If NeedsFlush() is true then Flush() must be called before any vars created |
| 43 // by the ResourceConverter are valid. It handles creating any resource hosts | 50 // by the ResourceConverter are valid. It handles creating any resource hosts |
| 44 // that need to be created. |callback| will always be called asynchronously. | 51 // that need to be created. |callback| will always be called asynchronously. |
| 45 virtual void Flush(const base::Callback<void(bool)>& callback) = 0; | 52 virtual void Flush(const base::Callback<void(bool)>& callback) = 0; |
| 53 // Flush, but don't return until the resource hosts are all created, including |
| 54 // in the browser side. |
| 55 virtual void FlushSync() = 0; |
| 46 | 56 |
| 47 // Attempts to convert a V8 object to a PP_Var with type PP_VARTYPE_RESOURCE. | 57 // Attempts to convert a V8 object to a PP_Var with type PP_VARTYPE_RESOURCE. |
| 48 // On success, writes the resulting var to |result|, sets |was_resource| to | 58 // On success, writes the resulting var to |result|, sets |was_resource| to |
| 49 // true and returns true. If |val| is not a resource, sets |was_resource| to | 59 // true and returns true. If |val| is not a resource, sets |was_resource| to |
| 50 // false and returns true. If an error occurs, returns false. | 60 // false and returns true. If an error occurs, returns false. |
| 51 virtual bool FromV8Value(v8::Handle<v8::Object> val, | 61 virtual bool FromV8Value(v8::Handle<v8::Object> val, |
| 52 v8::Handle<v8::Context> context, | 62 v8::Handle<v8::Context> context, |
| 53 PP_Var* result, | 63 PP_Var* result, |
| 54 bool* was_resource) = 0; | 64 bool* was_resource) = 0; |
| 55 | 65 |
| 56 // Attempts to convert a PP_Var to a V8 object. |var| must have type | 66 // Attempts to convert a PP_Var to a V8 object. |var| must have type |
| 57 // PP_VARTYPE_RESOURCE. On success, writes the resulting value to |result| and | 67 // PP_VARTYPE_RESOURCE. On success, writes the resulting value to |result| and |
| 58 // returns true. If an error occurs, returns false. | 68 // returns true. If an error occurs, returns false. |
| 59 virtual bool ToV8Value(const PP_Var& var, | 69 virtual bool ToV8Value(const PP_Var& var, |
| 60 v8::Handle<v8::Context> context, | 70 v8::Handle<v8::Context> context, |
| 61 v8::Handle<v8::Value>* result) = 0; | 71 v8::Handle<v8::Value>* result) = 0; |
| 62 }; | 72 }; |
| 63 | 73 |
| 64 class ResourceConverterImpl : public ResourceConverter { | 74 class ResourceConverterImpl : public ResourceConverter { |
| 65 public: | 75 public: |
| 66 explicit ResourceConverterImpl(PP_Instance instance); | 76 explicit ResourceConverterImpl(PP_Instance instance); |
| 67 ~ResourceConverterImpl() override; | 77 ~ResourceConverterImpl() override; |
| 68 | 78 |
| 69 // ResourceConverter overrides. | 79 // ResourceConverter overrides. |
| 70 void Reset() override; | 80 void Reset() override; |
| 71 bool NeedsFlush() override; | 81 bool NeedsFlush() override; |
| 72 void Flush(const base::Callback<void(bool)>& callback) override; | 82 void Flush(const base::Callback<void(bool)>& callback) override; |
| 83 void FlushSync() override; |
| 73 bool FromV8Value(v8::Handle<v8::Object> val, | 84 bool FromV8Value(v8::Handle<v8::Object> val, |
| 74 v8::Handle<v8::Context> context, | 85 v8::Handle<v8::Context> context, |
| 75 PP_Var* result, | 86 PP_Var* result, |
| 76 bool* was_resource) override; | 87 bool* was_resource) override; |
| 77 bool ToV8Value(const PP_Var& var, | 88 bool ToV8Value(const PP_Var& var, |
| 78 v8::Handle<v8::Context> context, | 89 v8::Handle<v8::Context> context, |
| 79 v8::Handle<v8::Value>* result) override; | 90 v8::Handle<v8::Value>* result) override; |
| 80 | 91 |
| 81 private: | 92 private: |
| 93 // A list of the resource vars associated with browser hosts. |
| 94 typedef std::vector<scoped_refptr<HostResourceVar>> VarVector; |
| 95 |
| 96 struct PendingConversion { |
| 97 PendingConversion(); |
| 98 ~PendingConversion(); |
| 99 int sequence_num; |
| 100 base::Callback<void(bool)> callback; |
| 101 VarVector browser_vars; |
| 102 }; |
| 103 |
| 82 // Creates a resource var with the given |pending_renderer_id| and | 104 // Creates a resource var with the given |pending_renderer_id| and |
| 83 // |create_message| to be sent to the plugin. | 105 // |create_message| to be sent to the plugin. |
| 84 scoped_refptr<HostResourceVar> CreateResourceVar( | 106 scoped_refptr<HostResourceVar> CreateResourceVar( |
| 85 int pending_renderer_id, | 107 int pending_renderer_id, |
| 86 const IPC::Message& create_message); | 108 const IPC::Message& create_message); |
| 87 // Creates a resource var with the given |pending_renderer_id| and | 109 // Creates a resource var with the given |pending_renderer_id| and |
| 88 // |create_message| to be sent to the plugin. Also sends | 110 // |create_message| to be sent to the plugin. Also sends |
| 89 // |browser_host_create_message| to the browser, and asynchronously stores the | 111 // |browser_host_create_message| to the browser, and asynchronously stores the |
| 90 // resulting browser host ID in the newly created var. | 112 // resulting browser host ID in the newly created var. |
| 91 scoped_refptr<HostResourceVar> CreateResourceVarWithBrowserHost( | 113 scoped_refptr<HostResourceVar> CreateResourceVarWithBrowserHost( |
| 92 int pending_renderer_id, | 114 int pending_renderer_id, |
| 93 const IPC::Message& create_message, | 115 const IPC::Message& create_message, |
| 94 const IPC::Message& browser_host_create_message); | 116 const IPC::Message& browser_host_create_message); |
| 95 | 117 |
| 118 void FlushComplete(const ppapi::proxy::CompletedBrowserResourceHosts& hosts); |
| 119 |
| 96 // The instance this ResourceConverter is associated with. | 120 // The instance this ResourceConverter is associated with. |
| 97 PP_Instance instance_; | 121 PP_Instance instance_; |
| 98 | 122 |
| 99 // A list of the messages to create the browser hosts. This is a parallel | 123 // A list of the messages to create the browser hosts. This is a parallel |
| 100 // array to |browser_vars|. It is kept as a parallel array so that it can be | 124 // array to |browser_vars|. It is kept as a parallel array so that it can be |
| 101 // conveniently passed to |CreateBrowserResourceHosts|. | 125 // conveniently passed to |CreateBrowserResourceHosts|. |
| 102 std::vector<IPC::Message> browser_host_create_messages_; | 126 std::vector<IPC::Message> browser_host_create_messages_; |
| 103 // A list of the resource vars associated with browser hosts. | 127 VarVector browser_vars_; |
| 104 std::vector<scoped_refptr<HostResourceVar> > browser_vars_; | 128 |
| 129 std::queue<PendingConversion> pending_conversions_; |
| 130 |
| 131 base::WeakPtrFactory<ResourceConverterImpl> weak_ptr_factory_; |
| 105 | 132 |
| 106 DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl); | 133 DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl); |
| 107 }; | 134 }; |
| 108 | 135 |
| 109 } // namespace content | 136 } // namespace content |
| 110 #endif // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H | 137 #endif // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H |
| OLD | NEW |