Chromium Code Reviews| Index: ppapi/proxy/ppb_flash_menu_proxy.cc | 
| diff --git a/ppapi/proxy/ppb_flash_menu_proxy.cc b/ppapi/proxy/ppb_flash_menu_proxy.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..5ecdb16a94ce3b0eb86c359bb8b92dc71163d83b | 
| --- /dev/null | 
| +++ b/ppapi/proxy/ppb_flash_menu_proxy.cc | 
| @@ -0,0 +1,201 @@ | 
| +// Copyright (c) 2011 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_menu_proxy.h" | 
| + | 
| +#include "ppapi/c/pp_errors.h" | 
| +#include "ppapi/c/private/ppb_flash_menu.h" | 
| +#include "ppapi/proxy/ppapi_messages.h" | 
| + | 
| +namespace pp { | 
| +namespace proxy { | 
| + | 
| 
 
viettrungluu
2011/02/08 18:18:09
Nit: extraneous blank line.
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + | 
| +class FlashMenu : public PluginResource { | 
| + public: | 
| + explicit FlashMenu(const HostResource& resource) | 
| + : PluginResource(resource), | 
| + callback_(PP_BlockUntilComplete()), | 
| + selected_id_ptr_(NULL) { | 
| + } | 
| + | 
| + virtual ~FlashMenu() { } | 
| 
 
viettrungluu
2011/02/08 18:18:09
I think mostly we do |{}|, not |{ }|.
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + | 
| + // Resource overrides. | 
| + virtual FlashMenu* AsFlashMenu() { return this; } | 
| + | 
| + int32_t* selected_id_ptr() const { return selected_id_ptr_; } | 
| + void set_selected_id_ptr(int32_t* ptr) { selected_id_ptr_ = ptr; } | 
| + | 
| + PP_CompletionCallback callback() const { | 
| + return callback_; | 
| 
 
viettrungluu
2011/02/08 18:18:09
For consistency (with the above), you may as well
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + } | 
| + | 
| + void set_callback(PP_CompletionCallback cb) { | 
| + callback_ = cb; | 
| + } | 
| + | 
| + private: | 
| + PP_CompletionCallback callback_; | 
| 
 
viettrungluu
2011/02/08 18:18:09
Side question: Do you know if brettw has a plan fo
 
piman
2011/02/15 02:11:22
Not really, I think this is something that we'll n
 
 | 
| + int32_t* selected_id_ptr_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(FlashMenu); | 
| +}; | 
| + | 
| +namespace { | 
| + | 
| +PP_Resource Create(PP_Instance instance_id, const PP_Flash_Menu* menu_data) { | 
| + PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); | 
| + if (!dispatcher) | 
| + return 0; | 
| + | 
| + HostResource result; | 
| + pp::proxy::SerializedFlashMenu serialized_menu; | 
| + if (!serialized_menu.SetPPMenu(menu_data)) | 
| + return 0; | 
| + dispatcher->Send(new PpapiHostMsg_PPBFlashMenu_Create( | 
| + INTERFACE_ID_PPB_FLASH_MENU, instance_id, serialized_menu, &result)); | 
| + | 
| + if (result.is_null()) | 
| 
 
viettrungluu
2011/02/08 18:18:09
Super-nit: I'd prefer there not to be a blank line
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + return 0; | 
| + linked_ptr<FlashMenu> menu(new FlashMenu(result)); | 
| + return PluginResourceTracker::GetInstance()->AddResource(menu); | 
| +} | 
| + | 
| +PP_Bool IsFlashMenu(PP_Resource resource) { | 
| + return BoolToPPBool(!!PluginResource::GetAs<FlashMenu>(resource)); | 
| +} | 
| + | 
| +int32_t Show(PP_Resource menu_id, | 
| + const PP_Point* location, | 
| + int32_t* selected_id, | 
| + PP_CompletionCallback callback) { | 
| + FlashMenu* object = PluginResource::GetAs<FlashMenu>(menu_id); | 
| + if (!object) | 
| + return PP_ERROR_BADRESOURCE; | 
| + PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 
| + object->instance()); | 
| + if (!dispatcher) | 
| + return PP_ERROR_FAILED; | 
| + | 
| + if (object->callback().func) | 
| + return PP_ERROR_INPROGRESS; | 
| + | 
| + object->set_selected_id_ptr(selected_id); | 
| + object->set_callback(callback); | 
| + | 
| + dispatcher->Send(new PpapiHostMsg_PPBFlashMenu_Show( | 
| + INTERFACE_ID_PPB_FLASH_MENU, object->host_resource(), *location)); | 
| + | 
| + return PP_ERROR_WOULDBLOCK; | 
| +} | 
| + | 
| +const PPB_Flash_Menu ppb_flash_menu = { | 
| + &Create, | 
| + &IsFlashMenu, | 
| + &Show, | 
| +}; | 
| + | 
| +} // namespace | 
| + | 
| +PPB_Flash_Menu_Proxy::PPB_Flash_Menu_Proxy(Dispatcher* dispatcher, | 
| + const void* target_interface) | 
| + : InterfaceProxy(dispatcher, target_interface), | 
| + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 
| +} | 
| + | 
| +PPB_Flash_Menu_Proxy::~PPB_Flash_Menu_Proxy() { | 
| +} | 
| + | 
| +const void* PPB_Flash_Menu_Proxy::GetSourceInterface() const { | 
| + return &ppb_flash_menu; | 
| +} | 
| + | 
| +InterfaceID PPB_Flash_Menu_Proxy::GetInterfaceId() const { | 
| + return INTERFACE_ID_PPB_FLASH_MENU; | 
| +} | 
| + | 
| +bool PPB_Flash_Menu_Proxy::OnMessageReceived(const IPC::Message& msg) { | 
| + bool handled = true; | 
| + IPC_BEGIN_MESSAGE_MAP(PPB_Flash_Menu_Proxy, msg) | 
| + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMenu_Create, | 
| + OnMsgCreate) | 
| + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMenu_Show, | 
| + OnMsgShow) | 
| + | 
| + IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashMenu_ShowACK, | 
| + OnMsgShowACK) | 
| + IPC_MESSAGE_UNHANDLED(handled = false) | 
| + IPC_END_MESSAGE_MAP() | 
| + // FIXME(brettw) handle bad messages! | 
| + return handled; | 
| +} | 
| + | 
| +void PPB_Flash_Menu_Proxy::OnMsgCreate(PP_Instance instance_id, | 
| + const SerializedFlashMenu& menu_data, | 
| + HostResource* result) { | 
| + PP_Resource resource = ppb_flash_menu_target()->Create(instance_id, | 
| + menu_data.pp_menu()); | 
| + result->SetHostResource(instance_id, resource); | 
| +} | 
| + | 
| +struct PPB_Flash_Menu_Proxy::ShowRequest { | 
| + HostResource menu; | 
| + int32_t selected_id; | 
| +}; | 
| + | 
| +void PPB_Flash_Menu_Proxy::OnMsgShow(const HostResource& menu, | 
| + const PP_Point& location) { | 
| + ShowRequest* request = new ShowRequest; | 
| + request->menu = menu; | 
| + CompletionCallback callback = callback_factory_.NewCallback( | 
| + &PPB_Flash_Menu_Proxy::SendShowACKToPlugin, request); | 
| + int32_t result = ppb_flash_menu_target()->Show( | 
| + menu.host_resource(), | 
| + &location, | 
| + &request->selected_id, | 
| + callback.pp_completion_callback()); | 
| + if (result != PP_ERROR_WOULDBLOCK) { | 
| + // There was some error, so we won't get a flush callback. We need to now | 
| 
 
viettrungluu
2011/02/08 18:18:09
s/flush/???/
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + // issue the ACK to the plugin hears about the error. This will also clean | 
| 
 
viettrungluu
2011/02/08 18:18:09
s/to/so/
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + // up the data associated with the callback. | 
| + callback.Run(result); | 
| + } | 
| +} | 
| + | 
| +void PPB_Flash_Menu_Proxy::OnMsgShowACK(const HostResource& menu, | 
| + int32_t selected_id, | 
| + int32_t result) { | 
| + PP_Resource plugin_resource = | 
| + PluginResourceTracker::GetInstance()->PluginResourceForHostResource(menu); | 
| + if (!plugin_resource) | 
| + return; | 
| + FlashMenu* object = PluginResource::GetAs<FlashMenu>(plugin_resource); | 
| + if (!object) { | 
| + // The plugin has released the FlashMenu object so don't issue the | 
| 
 
viettrungluu
2011/02/08 18:18:09
Hrm. I suppose that someone will have ensured that
 
 | 
| + // callback. | 
| + return; | 
| + } | 
| + | 
| + // Be careful to make the callback NULL again before issuing the callback | 
| + // since the plugin might want to flush from within the callback. | 
| 
 
viettrungluu
2011/02/08 18:18:09
s/flush/???/
 
piman
2011/02/15 02:11:22
Done.
 
 | 
| + PP_CompletionCallback callback = object->callback(); | 
| + object->set_callback(PP_BlockUntilComplete()); | 
| + *(object->selected_id_ptr()) = selected_id; | 
| + PP_RunCompletionCallback(&callback, result); | 
| +} | 
| + | 
| +void PPB_Flash_Menu_Proxy::SendShowACKToPlugin( | 
| + int32_t result, | 
| + ShowRequest* request) { | 
| + dispatcher()->Send(new PpapiMsg_PPBFlashMenu_ShowACK( | 
| + INTERFACE_ID_PPB_FLASH_MENU, | 
| + request->menu, | 
| + request->selected_id, | 
| + result)); | 
| + delete request; | 
| +} | 
| + | 
| +} // namespace proxy | 
| +} // namespace pp |