1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/proxy/serialized_flash_menu.h"
7 #include "ipc/ipc_message.h"
8 #include "ppapi/c/private/ppb_flash_menu.h"
9 #include "ppapi/proxy/ppapi_param_traits.h"
15 // Maximum depth of submenus allowed (e.g., 1 indicates that submenus are
16 // allowed, but not sub-submenus).
17 const int kMaxMenuDepth
= 2;
19 bool CheckMenu(int depth
, const PP_Flash_Menu
* menu
);
20 void FreeMenu(const PP_Flash_Menu
* menu
);
21 void WriteMenu(IPC::Message
* m
, const PP_Flash_Menu
* menu
);
22 PP_Flash_Menu
* ReadMenu(int depth
, const IPC::Message
* m
, PickleIterator
* iter
);
24 bool CheckMenuItem(int depth
, const PP_Flash_MenuItem
* item
) {
25 if (item
->type
== PP_FLASH_MENUITEM_TYPE_SUBMENU
)
26 return CheckMenu(depth
, item
->submenu
);
30 bool CheckMenu(int depth
, const PP_Flash_Menu
* menu
) {
31 if (depth
> kMaxMenuDepth
|| !menu
)
35 if (menu
->count
&& !menu
->items
)
38 for (uint32_t i
= 0; i
< menu
->count
; ++i
) {
39 if (!CheckMenuItem(depth
, menu
->items
+ i
))
45 void WriteMenuItem(IPC::Message
* m
, const PP_Flash_MenuItem
* menu_item
) {
46 PP_Flash_MenuItem_Type type
= menu_item
->type
;
48 m
->WriteString(menu_item
->name
? menu_item
->name
: "");
49 m
->WriteInt(menu_item
->id
);
50 IPC::ParamTraits
<PP_Bool
>::Write(m
, menu_item
->enabled
);
51 IPC::ParamTraits
<PP_Bool
>::Write(m
, menu_item
->checked
);
52 if (type
== PP_FLASH_MENUITEM_TYPE_SUBMENU
)
53 WriteMenu(m
, menu_item
->submenu
);
56 void WriteMenu(IPC::Message
* m
, const PP_Flash_Menu
* menu
) {
57 m
->WriteUInt32(menu
->count
);
58 for (uint32_t i
= 0; i
< menu
->count
; ++i
)
59 WriteMenuItem(m
, menu
->items
+ i
);
62 void FreeMenuItem(const PP_Flash_MenuItem
* menu_item
) {
64 delete [] menu_item
->name
;
65 if (menu_item
->submenu
)
66 FreeMenu(menu_item
->submenu
);
69 void FreeMenu(const PP_Flash_Menu
* menu
) {
71 for (uint32_t i
= 0; i
< menu
->count
; ++i
)
72 FreeMenuItem(menu
->items
+ i
);
73 delete [] menu
->items
;
78 bool ReadMenuItem(int depth
,
79 const IPC::Message
* m
,
81 PP_Flash_MenuItem
* menu_item
) {
83 if (!m
->ReadUInt32(iter
, &type
))
85 if (type
> PP_FLASH_MENUITEM_TYPE_SUBMENU
)
87 menu_item
->type
= static_cast<PP_Flash_MenuItem_Type
>(type
);
89 if (!m
->ReadString(iter
, &name
))
91 menu_item
->name
= new char[name
.size() + 1];
92 std::copy(name
.begin(), name
.end(), menu_item
->name
);
93 menu_item
->name
[name
.size()] = 0;
94 if (!m
->ReadInt(iter
, &menu_item
->id
))
96 if (!IPC::ParamTraits
<PP_Bool
>::Read(m
, iter
, &menu_item
->enabled
))
98 if (!IPC::ParamTraits
<PP_Bool
>::Read(m
, iter
, &menu_item
->checked
))
100 if (type
== PP_FLASH_MENUITEM_TYPE_SUBMENU
) {
101 menu_item
->submenu
= ReadMenu(depth
, m
, iter
);
102 if (!menu_item
->submenu
)
108 PP_Flash_Menu
* ReadMenu(int depth
,
109 const IPC::Message
* m
,
110 PickleIterator
* iter
) {
111 if (depth
> kMaxMenuDepth
)
115 PP_Flash_Menu
* menu
= new PP_Flash_Menu
;
118 if (!m
->ReadUInt32(iter
, &menu
->count
)) {
123 if (menu
->count
== 0)
126 menu
->items
= new PP_Flash_MenuItem
[menu
->count
];
127 memset(menu
->items
, 0, sizeof(PP_Flash_MenuItem
) * menu
->count
);
128 for (uint32_t i
= 0; i
< menu
->count
; ++i
) {
129 if (!ReadMenuItem(depth
, m
, iter
, menu
->items
+ i
)) {
137 } // anonymous namespace
139 SerializedFlashMenu::SerializedFlashMenu()
144 SerializedFlashMenu::~SerializedFlashMenu() {
149 bool SerializedFlashMenu::SetPPMenu(const PP_Flash_Menu
* menu
) {
151 if (!CheckMenu(0, menu
))
159 void SerializedFlashMenu::WriteToMessage(IPC::Message
* m
) const {
160 WriteMenu(m
, pp_menu_
);
163 bool SerializedFlashMenu::ReadFromMessage(const IPC::Message
* m
,
164 PickleIterator
* iter
) {
166 pp_menu_
= ReadMenu(0, m
, iter
);