OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/extensions/api/context_menu/context_menu_api.h" | 5 #include "chrome/browser/extensions/api/context_menu/context_menu_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/menu_manager.h" | |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/api/context_menus.h" | |
14 #include "chrome/common/extensions/extension_error_utils.h" | 16 #include "chrome/common/extensions/extension_error_utils.h" |
17 #include "chrome/common/extensions/url_pattern_set.h" | |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 const char kCheckedKey[] = "checked"; | |
19 const char kContextsKey[] = "contexts"; | |
20 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; | |
21 const char kEnabledKey[] = "enabled"; | |
22 const char kGeneratedIdKey[] = "generatedId"; | 21 const char kGeneratedIdKey[] = "generatedId"; |
23 const char kIdKey[] = "id"; | |
24 const char kOnclickKey[] = "onclick"; | |
25 const char kParentIdKey[] = "parentId"; | |
26 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; | |
27 const char kTitleKey[] = "title"; | |
28 const char kTypeKey[] = "type"; | |
29 | 22 |
30 const char kCannotFindItemError[] = "Cannot find menu item with id *"; | 23 const char kCannotFindItemError[] = "Cannot find menu item with id *"; |
31 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " | 24 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " |
32 "pass an onclick parameter to chrome.contextMenus.create. Instead, use " | 25 "pass an onclick parameter to chrome.contextMenus.create. Instead, use " |
33 "the chrome.contextMenus.onClicked event."; | 26 "the chrome.contextMenus.onClicked event."; |
34 const char kCheckedError[] = | 27 const char kCheckedError[] = |
35 "Only items with type \"radio\" or \"checkbox\" can be checked"; | 28 "Only items with type \"radio\" or \"checkbox\" can be checked"; |
36 const char kDuplicateIDError[] = | 29 const char kDuplicateIDError[] = |
37 "Cannot create item with duplicate id *"; | 30 "Cannot create item with duplicate id *"; |
38 const char kIdRequiredError[] = "Extensions using event pages must pass an " | 31 const char kIdRequiredError[] = "Extensions using event pages must pass an " |
39 "id parameter to chrome.contextMenus.create"; | 32 "id parameter to chrome.contextMenus.create"; |
40 const char kInvalidValueError[] = "Invalid value for *"; | |
41 const char kInvalidTypeStringError[] = "Invalid type string '*'"; | |
42 const char kParentsMustBeNormalError[] = | 33 const char kParentsMustBeNormalError[] = |
43 "Parent items must have type \"normal\""; | 34 "Parent items must have type \"normal\""; |
44 const char kTitleNeededError[] = | 35 const char kTitleNeededError[] = |
45 "All menu items except for separators must have a title"; | 36 "All menu items except for separators must have a title"; |
46 | 37 |
47 std::string GetIDString(const extensions::MenuItem::Id& id) { | 38 std::string GetIDString(const extensions::MenuItem::Id& id) { |
48 if (id.uid == 0) | 39 if (id.uid == 0) |
49 return id.string_uid; | 40 return id.string_uid; |
50 else | 41 else |
51 return base::IntToString(id.uid); | 42 return base::IntToString(id.uid); |
52 } | 43 } |
53 | 44 |
54 } // namespace | 45 } // namespace |
55 | 46 |
56 namespace extensions { | 47 namespace extensions { |
57 | 48 |
58 bool ExtensionContextMenuFunction::ParseContexts( | 49 namespace Create = api::context_menus::Create; |
59 const DictionaryValue& properties, | 50 namespace Remove = api::context_menus::Remove; |
60 const char* key, | 51 namespace Update = api::context_menus::Update; |
61 MenuItem::ContextList* result) { | |
62 ListValue* list = NULL; | |
63 if (!properties.GetList(key, &list)) { | |
64 return true; | |
65 } | |
66 MenuItem::ContextList tmp_result; | |
67 | |
68 std::string value; | |
69 for (size_t i = 0; i < list->GetSize(); i++) { | |
70 if (!list->GetString(i, &value)) | |
71 return false; | |
72 | |
73 if (value == "all") { | |
74 tmp_result.Add(MenuItem::ALL); | |
75 } else if (value == "page") { | |
76 tmp_result.Add(MenuItem::PAGE); | |
77 } else if (value == "selection") { | |
78 tmp_result.Add(MenuItem::SELECTION); | |
79 } else if (value == "link") { | |
80 tmp_result.Add(MenuItem::LINK); | |
81 } else if (value == "editable") { | |
82 tmp_result.Add(MenuItem::EDITABLE); | |
83 } else if (value == "image") { | |
84 tmp_result.Add(MenuItem::IMAGE); | |
85 } else if (value == "video") { | |
86 tmp_result.Add(MenuItem::VIDEO); | |
87 } else if (value == "audio") { | |
88 tmp_result.Add(MenuItem::AUDIO); | |
89 } else if (value == "frame") { | |
90 tmp_result.Add(MenuItem::FRAME); | |
91 } else { | |
92 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidValueError, key); | |
93 return false; | |
94 } | |
95 } | |
96 *result = tmp_result; | |
97 return true; | |
98 } | |
99 | |
100 bool ExtensionContextMenuFunction::ParseType( | |
101 const DictionaryValue& properties, | |
102 const MenuItem::Type& default_value, | |
103 MenuItem::Type* result) { | |
104 DCHECK(result); | |
105 if (!properties.HasKey(kTypeKey)) { | |
106 *result = default_value; | |
107 return true; | |
108 } | |
109 | |
110 std::string type_string; | |
111 if (!properties.GetString(kTypeKey, &type_string)) | |
112 return false; | |
113 | |
114 if (type_string == "normal") { | |
115 *result = MenuItem::NORMAL; | |
116 } else if (type_string == "checkbox") { | |
117 *result = MenuItem::CHECKBOX; | |
118 } else if (type_string == "radio") { | |
119 *result = MenuItem::RADIO; | |
120 } else if (type_string == "separator") { | |
121 *result = MenuItem::SEPARATOR; | |
122 } else { | |
123 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidTypeStringError, | |
124 type_string); | |
125 return false; | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 bool ExtensionContextMenuFunction::ParseChecked( | |
131 MenuItem::Type type, | |
132 const DictionaryValue& properties, | |
133 bool default_value, | |
134 bool* checked) { | |
135 if (!properties.HasKey(kCheckedKey)) { | |
136 *checked = default_value; | |
137 return true; | |
138 } | |
139 if (!properties.GetBoolean(kCheckedKey, checked)) | |
140 return false; | |
141 if (checked && type != MenuItem::CHECKBOX && type != MenuItem::RADIO) { | |
142 error_ = kCheckedError; | |
143 return false; | |
144 } | |
145 return true; | |
146 } | |
147 | |
148 bool ExtensionContextMenuFunction::ParseID(const Value* value, | |
149 MenuItem::Id* result) { | |
150 return (value->GetAsInteger(&result->uid) || | |
151 value->GetAsString(&result->string_uid)); | |
152 } | |
153 | |
154 bool ExtensionContextMenuFunction::GetParent(const DictionaryValue& properties, | |
155 const MenuManager& manager, | |
156 MenuItem** result) { | |
157 if (!properties.HasKey(kParentIdKey)) | |
158 return true; | |
159 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); | |
160 Value* parent_id_value = NULL; | |
161 if (properties.Get(kParentIdKey, &parent_id_value) && | |
162 !ParseID(parent_id_value, &parent_id)) | |
163 return false; | |
164 | |
165 MenuItem* parent = manager.GetItemById(parent_id); | |
166 if (!parent) { | |
167 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
168 kCannotFindItemError, GetIDString(parent_id)); | |
169 return false; | |
170 } | |
171 if (parent->type() != MenuItem::NORMAL) { | |
172 error_ = kParentsMustBeNormalError; | |
173 return false; | |
174 } | |
175 *result = parent; | |
176 return true; | |
177 } | |
178 | 52 |
179 bool CreateContextMenuFunction::RunImpl() { | 53 bool CreateContextMenuFunction::RunImpl() { |
180 DictionaryValue* properties; | 54 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
181 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | 55 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); |
182 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | 56 EXTENSION_FUNCTION_VALIDATE(params.get()); |
183 | 57 |
184 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 58 if (params->create_properties.id.get()) { |
185 if (properties->HasKey(kIdKey)) { | 59 id.string_uid = *params->create_properties.id; |
186 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kIdKey, | |
187 &id.string_uid)); | |
188 } else { | 60 } else { |
189 if (GetExtension()->has_lazy_background_page()) { | 61 if (GetExtension()->has_lazy_background_page()) { |
190 error_ = kIdRequiredError; | 62 error_ = kIdRequiredError; |
191 return false; | 63 return false; |
192 } | 64 } |
65 | |
66 DictionaryValue* properties = NULL; | |
67 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | |
193 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 68 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
194 &id.uid)); | 69 &id.uid)); |
not at google - send to devlin
2012/07/30 15:36:53
yep, ignore previous comment. this is fine.
Could
chebert
2012/07/30 22:45:19
Done.
| |
195 } | 70 } |
196 | 71 |
197 std::string title; | 72 std::string title; |
198 if (properties->HasKey(kTitleKey) && | 73 if (params->create_properties.title.get()) |
199 !properties->GetString(kTitleKey, &title)) | 74 title = *params->create_properties.title; |
200 return false; | |
201 | 75 |
202 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); | 76 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); |
203 | 77 |
204 if (menu_manager->GetItemById(id)) { | 78 if (menu_manager->GetItemById(id)) { |
205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, | 79 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, |
206 GetIDString(id)); | 80 GetIDString(id)); |
207 return false; | 81 return false; |
208 } | 82 } |
209 | 83 |
210 if (GetExtension()->has_lazy_background_page() && | 84 if (GetExtension()->has_lazy_background_page() && |
211 properties->HasKey(kOnclickKey)) { | 85 params->create_properties.onclick.get()) { |
212 error_ = kOnclickDisallowedError; | 86 error_ = kOnclickDisallowedError; |
213 return false; | 87 return false; |
214 } | 88 } |
215 | 89 |
216 MenuItem::ContextList contexts(MenuItem::PAGE); | 90 MenuItem::ContextList contexts(MenuItem::PAGE); |
not at google - send to devlin
2012/07/30 15:36:53
I had a go at seeing whether JSC can do this, but
chebert
2012/07/30 22:45:19
Done.
| |
217 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 91 if (params->create_properties.contexts.get()) { |
218 return false; | 92 for (size_t i = 0; i < params->create_properties.contexts->size(); ++i) { |
93 switch (params->create_properties.contexts->at(i)) { | |
94 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_ALL: | |
95 contexts.Add(MenuItem::ALL); | |
96 break; | |
97 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_PAGE: | |
98 contexts.Add(MenuItem::PAGE); | |
99 break; | |
100 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_SELECTION: | |
101 contexts.Add(MenuItem::SELECTION); | |
102 break; | |
103 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_LINK: | |
104 contexts.Add(MenuItem::LINK); | |
105 break; | |
106 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_EDITABLE: | |
107 contexts.Add(MenuItem::EDITABLE); | |
108 break; | |
109 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_IMAGE: | |
110 contexts.Add(MenuItem::IMAGE); | |
111 break; | |
112 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_VIDEO: | |
113 contexts.Add(MenuItem::VIDEO); | |
114 break; | |
115 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_AUDIO: | |
116 contexts.Add(MenuItem::AUDIO); | |
117 break; | |
118 case Create::Params::CreateProperties::CONTEXTS_ELEMENT_FRAME: | |
119 contexts.Add(MenuItem::FRAME); | |
120 break; | |
121 default: | |
122 return false; | |
123 } | |
124 } | |
125 } | |
219 | 126 |
220 MenuItem::Type type; | 127 MenuItem::Type type; |
221 if (!ParseType(*properties, MenuItem::NORMAL, &type)) | 128 switch (params->create_properties.type) { |
222 return false; | 129 case Create::Params::CreateProperties::TYPE_NONE: |
not at google - send to devlin
2012/07/30 15:36:53
same comment as above re enum
chebert
2012/07/30 22:45:19
Done.
| |
130 case Create::Params::CreateProperties::TYPE_NORMAL: | |
131 type = MenuItem::NORMAL; | |
132 break; | |
133 case Create::Params::CreateProperties::TYPE_CHECKBOX: | |
134 type = MenuItem::CHECKBOX; | |
135 break; | |
136 case Create::Params::CreateProperties::TYPE_RADIO: | |
137 type = MenuItem::RADIO; | |
138 break; | |
139 case Create::Params::CreateProperties::TYPE_SEPARATOR: | |
140 type = MenuItem::SEPARATOR; | |
141 break; | |
142 } | |
223 | 143 |
224 if (title.empty() && type != MenuItem::SEPARATOR) { | 144 if (title.empty() && type != MenuItem::SEPARATOR) { |
225 error_ = kTitleNeededError; | 145 error_ = kTitleNeededError; |
226 return false; | 146 return false; |
227 } | 147 } |
228 | 148 |
229 bool checked; | 149 bool checked = false; |
230 if (!ParseChecked(type, *properties, false, &checked)) | 150 if (params->create_properties.checked.get()) |
231 return false; | 151 checked = *params->create_properties.checked; |
232 | 152 |
233 bool enabled = true; | 153 bool enabled = true; |
234 if (properties->HasKey(kEnabledKey)) | 154 if (params->create_properties.enabled.get()) |
235 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | 155 enabled = *params->create_properties.enabled; |
236 | 156 |
237 scoped_ptr<MenuItem> item( | 157 scoped_ptr<MenuItem> item( |
238 new MenuItem(id, title, checked, enabled, type, contexts)); | 158 new MenuItem(id, title, checked, enabled, type, contexts)); |
239 | 159 |
240 if (!item->PopulateURLPatterns( | 160 if (!item->PopulateURLPatterns( |
241 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | 161 params->create_properties.document_url_patterns.get(), |
162 params->create_properties.target_url_patterns.get(), &error_)) { | |
not at google - send to devlin
2012/07/30 15:36:53
nit: &error_ on next line
chebert
2012/07/30 22:45:19
Done.
| |
242 return false; | 163 return false; |
164 } | |
243 | 165 |
244 bool success = true; | 166 bool success = true; |
245 if (properties->HasKey(kParentIdKey)) { | 167 if (params->create_properties.parent_id_type != |
246 MenuItem* parent = NULL; | 168 Create::Params::CreateProperties::PARENT_ID_NONE) { |
247 if (!GetParent(*properties, *menu_manager, &parent)) | 169 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); |
170 switch (params->create_properties.parent_id_type) { | |
171 case Create::Params::CreateProperties::PARENT_ID_INTEGER: | |
172 parent_id.uid = *params->create_properties.parent_id_integer; | |
173 break; | |
174 case Create::Params::CreateProperties::PARENT_ID_STRING: | |
175 parent_id.string_uid = *params->create_properties.parent_id_string; | |
176 break; | |
177 default: | |
178 break; | |
179 } | |
180 MenuItem* parent = menu_manager->GetItemById(parent_id); | |
181 if (!parent) { | |
182 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
183 kCannotFindItemError, GetIDString(parent_id)); | |
248 return false; | 184 return false; |
185 } | |
186 if (parent->type() != MenuItem::NORMAL) { | |
187 error_ = kParentsMustBeNormalError; | |
188 return false; | |
189 } | |
190 | |
249 success = menu_manager->AddChildItem(parent->id(), item.release()); | 191 success = menu_manager->AddChildItem(parent->id(), item.release()); |
250 } else { | 192 } else { |
251 success = menu_manager->AddContextItem(GetExtension(), item.release()); | 193 success = menu_manager->AddContextItem(GetExtension(), item.release()); |
252 } | 194 } |
253 | 195 |
254 if (!success) | 196 if (!success) |
255 return false; | 197 return false; |
256 | 198 |
257 menu_manager->WriteToStorage(GetExtension()); | 199 menu_manager->WriteToStorage(GetExtension()); |
258 return true; | 200 return true; |
259 } | 201 } |
260 | 202 |
261 bool UpdateContextMenuFunction::RunImpl() { | 203 bool UpdateContextMenuFunction::RunImpl() { |
262 bool radioItemUpdated = false; | 204 bool radioItemUpdated = false; |
263 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); | 205 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); |
264 Value* id_value = NULL; | 206 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
265 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 207 |
266 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &item_id)); | 208 EXTENSION_FUNCTION_VALIDATE(params.get()); |
209 switch (params->id_type) { | |
210 case Update::Params::ID_STRING: | |
211 item_id.string_uid = *params->id_string; | |
not at google - send to devlin
2012/07/30 15:36:53
more switch with template stuff
chebert
2012/07/30 22:45:19
The enums are named differently here, so a helper
| |
212 break; | |
213 case Update::Params::ID_INTEGER: | |
214 item_id.uid = *params->id_integer; | |
215 break; | |
216 } | |
267 | 217 |
268 ExtensionService* service = profile()->GetExtensionService(); | 218 ExtensionService* service = profile()->GetExtensionService(); |
269 MenuManager* manager = service->menu_manager(); | 219 MenuManager* manager = service->menu_manager(); |
270 MenuItem* item = manager->GetItemById(item_id); | 220 MenuItem* item = manager->GetItemById(item_id); |
271 if (!item || item->extension_id() != extension_id()) { | 221 if (!item || item->extension_id() != extension_id()) { |
272 error_ = ExtensionErrorUtils::FormatErrorMessage( | 222 error_ = ExtensionErrorUtils::FormatErrorMessage( |
273 kCannotFindItemError, GetIDString(item_id)); | 223 kCannotFindItemError, GetIDString(item_id)); |
274 return false; | 224 return false; |
275 } | 225 } |
276 | 226 |
277 DictionaryValue* properties = NULL; | |
278 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | |
279 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | |
280 | |
281 // Type. | 227 // Type. |
282 MenuItem::Type type; | 228 MenuItem::Type type; |
283 if (!ParseType(*properties, item->type(), &type)) | 229 switch (params->update_properties.type) { |
284 return false; | 230 case Update::Params::UpdateProperties::TYPE_NONE: |
231 case Update::Params::UpdateProperties::TYPE_NORMAL: | |
232 type = MenuItem::NORMAL; | |
233 break; | |
234 case Update::Params::UpdateProperties::TYPE_CHECKBOX: | |
235 type = MenuItem::CHECKBOX; | |
236 break; | |
237 case Update::Params::UpdateProperties::TYPE_RADIO: | |
238 type = MenuItem::RADIO; | |
239 break; | |
240 case Update::Params::UpdateProperties::TYPE_SEPARATOR: | |
241 type = MenuItem::SEPARATOR; | |
242 break; | |
243 } | |
244 | |
285 if (type != item->type()) { | 245 if (type != item->type()) { |
286 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) { | 246 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) { |
287 radioItemUpdated = true; | 247 radioItemUpdated = true; |
288 } | 248 } |
289 item->set_type(type); | 249 item->set_type(type); |
290 } | 250 } |
291 | 251 |
292 // Title. | 252 // Title. |
293 if (properties->HasKey(kTitleKey)) { | 253 if (params->update_properties.title.get()) { |
294 std::string title; | 254 std::string title(*params->update_properties.title); |
295 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTitleKey, &title)); | 255 if (title.empty() && item->type() != MenuItem::SEPARATOR) { |
296 if (title.empty() && type != MenuItem::SEPARATOR) { | |
297 error_ = kTitleNeededError; | 256 error_ = kTitleNeededError; |
298 return false; | 257 return false; |
299 } | 258 } |
300 item->set_title(title); | 259 item->set_title(title); |
301 } | 260 } |
302 | 261 |
303 // Checked state. | 262 // Checked state. |
304 bool checked; | 263 if (params->update_properties.checked.get()) { |
305 if (!ParseChecked(item->type(), *properties, item->checked(), &checked)) | 264 bool checked = *params->update_properties.checked; |
306 return false; | 265 if (checked && item->type() != MenuItem::CHECKBOX && |
307 if (checked != item->checked()) { | 266 item->type() != MenuItem::RADIO) { |
308 if (!item->SetChecked(checked)) | 267 error_ = kCheckedError; |
309 return false; | 268 return false; |
310 radioItemUpdated = true; | 269 } |
270 if (checked != item->checked()) { | |
271 if (!item->SetChecked(checked)) { | |
272 error_ = kCheckedError; | |
273 return false; | |
274 } | |
275 radioItemUpdated = true; | |
276 } | |
311 } | 277 } |
312 | 278 |
313 // Enabled. | 279 // Enabled. |
314 bool enabled; | 280 if (params->update_properties.enabled.get()) |
315 if (properties->HasKey(kEnabledKey)) { | 281 item->set_enabled(*params->update_properties.enabled); |
316 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | |
317 item->set_enabled(enabled); | |
318 } | |
319 | 282 |
320 // Contexts. | 283 // Contexts. |
321 MenuItem::ContextList contexts(item->contexts()); | 284 MenuItem::ContextList contexts; |
322 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 285 if (params->update_properties.contexts.get()) { |
323 return false; | 286 for (size_t i = 0; i < params->update_properties.contexts->size(); ++i) { |
324 if (contexts != item->contexts()) | 287 Update::Params::UpdateProperties::ContextsElement context = |
325 item->set_contexts(contexts); | 288 params->update_properties.contexts->at(i); |
289 if (context == Update::Params::UpdateProperties::CONTEXTS_ELEMENT_ALL) { | |
290 contexts.Add(MenuItem::ALL); | |
291 } else if (context == | |
292 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_PAGE) { | |
293 contexts.Add(MenuItem::PAGE); | |
294 } else if (context == | |
295 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_SELECTION) { | |
296 contexts.Add(MenuItem::SELECTION); | |
297 } else if (context == | |
298 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_LINK) { | |
299 contexts.Add(MenuItem::LINK); | |
300 } else if (context == | |
301 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_EDITABLE) { | |
302 contexts.Add(MenuItem::EDITABLE); | |
303 } else if (context == | |
304 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_IMAGE) { | |
305 contexts.Add(MenuItem::IMAGE); | |
306 } else if (context == | |
307 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_VIDEO) { | |
308 contexts.Add(MenuItem::VIDEO); | |
309 } else if (context == | |
310 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_AUDIO) { | |
311 contexts.Add(MenuItem::AUDIO); | |
312 } else if (context == | |
313 Update::Params::UpdateProperties::CONTEXTS_ELEMENT_FRAME) { | |
314 contexts.Add(MenuItem::FRAME); | |
315 } | |
316 } | |
317 if (contexts != item->contexts()) | |
318 item->set_contexts(contexts); | |
319 } | |
326 | 320 |
327 // Parent id. | 321 // Parent id. |
328 MenuItem* parent = NULL; | 322 MenuItem* parent = NULL; |
329 if (!GetParent(*properties, *manager, &parent)) | 323 if (params->update_properties.parent_id_type != |
324 Update::Params::UpdateProperties::PARENT_ID_NONE) { | |
325 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); | |
326 switch (params->update_properties.parent_id_type) { | |
327 case Update::Params::UpdateProperties::PARENT_ID_STRING: | |
328 parent_id.string_uid = *params->update_properties.parent_id_string; | |
329 break; | |
330 case Update::Params::UpdateProperties::PARENT_ID_INTEGER: | |
331 parent_id.uid = *params->update_properties.parent_id_integer; | |
332 break; | |
333 default: | |
334 break; | |
not at google - send to devlin
2012/07/30 15:36:53
no point having this default branch
chebert
2012/07/30 22:45:19
Done.
| |
335 } | |
336 parent = manager->GetItemById(parent_id); | |
337 if (!parent) { | |
338 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
339 kCannotFindItemError, GetIDString(parent_id)); | |
340 return false; | |
341 } | |
342 if (parent->type() != MenuItem::NORMAL) { | |
343 error_ = kParentsMustBeNormalError; | |
344 return false; | |
345 } | |
346 if (parent && !manager->ChangeParent(item->id(), &parent->id())) | |
347 return false; | |
348 } | |
349 | |
350 // URL Patterns. | |
351 if (!item->PopulateURLPatterns( | |
352 params->update_properties.document_url_patterns.get(), | |
353 params->update_properties.target_url_patterns.get(), &error_)) { | |
330 return false; | 354 return false; |
331 if (parent && !manager->ChangeParent(item->id(), &parent->id())) | 355 } |
332 return false; | |
333 | |
334 if (!item->PopulateURLPatterns( | |
335 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | |
336 return false; | |
337 | 356 |
338 // There is no need to call ItemUpdated if ChangeParent is called because | 357 // There is no need to call ItemUpdated if ChangeParent is called because |
339 // all sanitation is taken care of in ChangeParent. | 358 // all sanitation is taken care of in ChangeParent. |
340 if (!parent && radioItemUpdated && !manager->ItemUpdated(item->id())) | 359 if (!parent && radioItemUpdated && !manager->ItemUpdated(item->id())) |
341 return false; | 360 return false; |
342 | 361 |
343 manager->WriteToStorage(GetExtension()); | 362 manager->WriteToStorage(GetExtension()); |
344 return true; | 363 return true; |
345 } | 364 } |
346 | 365 |
347 bool RemoveContextMenuFunction::RunImpl() { | 366 bool RemoveContextMenuFunction::RunImpl() { |
348 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 367 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
349 Value* id_value = NULL; | 368 |
350 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 369 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
351 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &id)); | 370 EXTENSION_FUNCTION_VALIDATE(params.get()); |
371 | |
372 switch (params->menu_item_id_type) { | |
373 case Remove::Params::MENU_ITEM_ID_STRING: | |
374 id.string_uid = *params->menu_item_id_string; | |
375 break; | |
376 case Remove::Params::MENU_ITEM_ID_INTEGER: | |
377 id.uid = *params->menu_item_id_integer; | |
378 } | |
379 | |
352 ExtensionService* service = profile()->GetExtensionService(); | 380 ExtensionService* service = profile()->GetExtensionService(); |
353 MenuManager* manager = service->menu_manager(); | 381 MenuManager* manager = service->menu_manager(); |
354 | 382 |
355 MenuItem* item = manager->GetItemById(id); | 383 MenuItem* item = manager->GetItemById(id); |
356 // Ensure one extension can't remove another's menu items. | 384 // Ensure one extension can't remove another's menu items. |
357 if (!item || item->extension_id() != extension_id()) { | 385 if (!item || item->extension_id() != extension_id()) { |
358 error_ = ExtensionErrorUtils::FormatErrorMessage( | 386 error_ = ExtensionErrorUtils::FormatErrorMessage( |
359 kCannotFindItemError, GetIDString(id)); | 387 kCannotFindItemError, GetIDString(id)); |
360 return false; | 388 return false; |
361 } | 389 } |
362 | 390 |
363 if (!manager->RemoveContextMenuItem(id)) | 391 if (!manager->RemoveContextMenuItem(id)) |
364 return false; | 392 return false; |
365 manager->WriteToStorage(GetExtension()); | 393 manager->WriteToStorage(GetExtension()); |
366 return true; | 394 return true; |
367 } | 395 } |
368 | 396 |
369 bool RemoveAllContextMenusFunction::RunImpl() { | 397 bool RemoveAllContextMenusFunction::RunImpl() { |
370 ExtensionService* service = profile()->GetExtensionService(); | 398 ExtensionService* service = profile()->GetExtensionService(); |
371 MenuManager* manager = service->menu_manager(); | 399 MenuManager* manager = service->menu_manager(); |
372 manager->RemoveAllContextItems(GetExtension()->id()); | 400 manager->RemoveAllContextItems(GetExtension()->id()); |
373 manager->WriteToStorage(GetExtension()); | 401 manager->WriteToStorage(GetExtension()); |
374 return true; | 402 return true; |
375 } | 403 } |
376 | 404 |
377 } // namespace extensions | 405 } // namespace extensions |
OLD | NEW |