OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_context_menu_api.h" | 5 #include "chrome/browser/extensions/extension_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_util.h" | 11 #include "base/string_util.h" |
11 #include "chrome/browser/extensions/extensions_service.h" | 12 #include "chrome/browser/extensions/extensions_service.h" |
12 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
13 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
14 | 15 |
15 const wchar_t kCheckedKey[] = L"checked"; | 16 const wchar_t kCheckedKey[] = L"checked"; |
16 const wchar_t kContextsKey[] = L"contexts"; | 17 const wchar_t kContextsKey[] = L"contexts"; |
17 const wchar_t kDocumentUrlPatternsKey[] = L"documentUrlPatterns"; | 18 const wchar_t kDocumentUrlPatternsKey[] = L"documentUrlPatterns"; |
18 const wchar_t kGeneratedIdKey[] = L"generatedId"; | 19 const wchar_t kGeneratedIdKey[] = L"generatedId"; |
19 const wchar_t kParentIdKey[] = L"parentId"; | 20 const wchar_t kParentIdKey[] = L"parentId"; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 ExtensionMenuItem** result) { | 182 ExtensionMenuItem** result) { |
182 if (!properties.HasKey(kParentIdKey)) | 183 if (!properties.HasKey(kParentIdKey)) |
183 return true; | 184 return true; |
184 ExtensionMenuItem::Id parent_id(extension_id(), 0); | 185 ExtensionMenuItem::Id parent_id(extension_id(), 0); |
185 if (properties.HasKey(kParentIdKey) && | 186 if (properties.HasKey(kParentIdKey) && |
186 !properties.GetInteger(kParentIdKey, &parent_id.second)) | 187 !properties.GetInteger(kParentIdKey, &parent_id.second)) |
187 return false; | 188 return false; |
188 | 189 |
189 ExtensionMenuItem* parent = manager.GetItemById(parent_id); | 190 ExtensionMenuItem* parent = manager.GetItemById(parent_id); |
190 if (!parent) { | 191 if (!parent) { |
191 error_ = "Cannot find menu item with id " + IntToString(parent_id.second); | 192 error_ = "Cannot find menu item with id " + |
| 193 base::IntToString(parent_id.second); |
192 return false; | 194 return false; |
193 } | 195 } |
194 if (parent->type() != ExtensionMenuItem::NORMAL) { | 196 if (parent->type() != ExtensionMenuItem::NORMAL) { |
195 error_ = kParentsMustBeNormalError; | 197 error_ = kParentsMustBeNormalError; |
196 return false; | 198 return false; |
197 } | 199 } |
198 *result = parent; | 200 *result = parent; |
199 return true; | 201 return true; |
200 } | 202 } |
201 | 203 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 return false; | 241 return false; |
240 | 242 |
241 bool success = true; | 243 bool success = true; |
242 if (properties->HasKey(kParentIdKey)) { | 244 if (properties->HasKey(kParentIdKey)) { |
243 ExtensionMenuItem::Id parent_id(extension_id(), 0); | 245 ExtensionMenuItem::Id parent_id(extension_id(), 0); |
244 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kParentIdKey, | 246 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kParentIdKey, |
245 &parent_id.second)); | 247 &parent_id.second)); |
246 ExtensionMenuItem* parent = menu_manager->GetItemById(parent_id); | 248 ExtensionMenuItem* parent = menu_manager->GetItemById(parent_id); |
247 if (!parent) { | 249 if (!parent) { |
248 error_ = ExtensionErrorUtils::FormatErrorMessage( | 250 error_ = ExtensionErrorUtils::FormatErrorMessage( |
249 kCannotFindItemError, IntToString(parent_id.second)); | 251 kCannotFindItemError, base::IntToString(parent_id.second)); |
250 return false; | 252 return false; |
251 } | 253 } |
252 if (parent->type() != ExtensionMenuItem::NORMAL) { | 254 if (parent->type() != ExtensionMenuItem::NORMAL) { |
253 error_ = kParentsMustBeNormalError; | 255 error_ = kParentsMustBeNormalError; |
254 return false; | 256 return false; |
255 } | 257 } |
256 success = menu_manager->AddChildItem(parent_id, item.release()); | 258 success = menu_manager->AddChildItem(parent_id, item.release()); |
257 } else { | 259 } else { |
258 success = menu_manager->AddContextItem(GetExtension(), item.release()); | 260 success = menu_manager->AddContextItem(GetExtension(), item.release()); |
259 } | 261 } |
260 | 262 |
261 if (!success) | 263 if (!success) |
262 return false; | 264 return false; |
263 | 265 |
264 return true; | 266 return true; |
265 } | 267 } |
266 | 268 |
267 bool UpdateContextMenuFunction::RunImpl() { | 269 bool UpdateContextMenuFunction::RunImpl() { |
268 ExtensionMenuItem::Id item_id(extension_id(), 0); | 270 ExtensionMenuItem::Id item_id(extension_id(), 0); |
269 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &item_id.second)); | 271 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &item_id.second)); |
270 | 272 |
271 ExtensionsService* service = profile()->GetExtensionsService(); | 273 ExtensionsService* service = profile()->GetExtensionsService(); |
272 ExtensionMenuManager* manager = service->menu_manager(); | 274 ExtensionMenuManager* manager = service->menu_manager(); |
273 ExtensionMenuItem* item = manager->GetItemById(item_id); | 275 ExtensionMenuItem* item = manager->GetItemById(item_id); |
274 if (!item || item->extension_id() != extension_id()) { | 276 if (!item || item->extension_id() != extension_id()) { |
275 error_ = ExtensionErrorUtils::FormatErrorMessage( | 277 error_ = ExtensionErrorUtils::FormatErrorMessage( |
276 kCannotFindItemError, IntToString(item_id.second)); | 278 kCannotFindItemError, base::IntToString(item_id.second)); |
277 return false; | 279 return false; |
278 } | 280 } |
279 | 281 |
280 DictionaryValue *properties = NULL; | 282 DictionaryValue *properties = NULL; |
281 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | 283 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); |
282 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | 284 EXTENSION_FUNCTION_VALIDATE(properties != NULL); |
283 | 285 |
284 ExtensionMenuManager* menu_manager = | 286 ExtensionMenuManager* menu_manager = |
285 profile()->GetExtensionsService()->menu_manager(); | 287 profile()->GetExtensionsService()->menu_manager(); |
286 | 288 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 bool RemoveContextMenuFunction::RunImpl() { | 336 bool RemoveContextMenuFunction::RunImpl() { |
335 ExtensionMenuItem::Id id(extension_id(), 0); | 337 ExtensionMenuItem::Id id(extension_id(), 0); |
336 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id.second)); | 338 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id.second)); |
337 ExtensionsService* service = profile()->GetExtensionsService(); | 339 ExtensionsService* service = profile()->GetExtensionsService(); |
338 ExtensionMenuManager* manager = service->menu_manager(); | 340 ExtensionMenuManager* manager = service->menu_manager(); |
339 | 341 |
340 ExtensionMenuItem* item = manager->GetItemById(id); | 342 ExtensionMenuItem* item = manager->GetItemById(id); |
341 // Ensure one extension can't remove another's menu items. | 343 // Ensure one extension can't remove another's menu items. |
342 if (!item || item->extension_id() != extension_id()) { | 344 if (!item || item->extension_id() != extension_id()) { |
343 error_ = ExtensionErrorUtils::FormatErrorMessage( | 345 error_ = ExtensionErrorUtils::FormatErrorMessage( |
344 kCannotFindItemError, IntToString(id.second)); | 346 kCannotFindItemError, base::IntToString(id.second)); |
345 return false; | 347 return false; |
346 } | 348 } |
347 | 349 |
348 return manager->RemoveContextMenuItem(id); | 350 return manager->RemoveContextMenuItem(id); |
349 } | 351 } |
350 | 352 |
351 bool RemoveAllContextMenusFunction::RunImpl() { | 353 bool RemoveAllContextMenusFunction::RunImpl() { |
352 ExtensionsService* service = profile()->GetExtensionsService(); | 354 ExtensionsService* service = profile()->GetExtensionsService(); |
353 ExtensionMenuManager* manager = service->menu_manager(); | 355 ExtensionMenuManager* manager = service->menu_manager(); |
354 manager->RemoveAllContextItems(extension_id()); | 356 manager->RemoveAllContextItems(extension_id()); |
355 return true; | 357 return true; |
356 } | 358 } |
OLD | NEW |