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/bookmarks/bookmark_extension_api.h" | 5 #include "chrome/browser/bookmarks/bookmark_extension_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 27 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
28 #include "chrome/browser/extensions/extensions_quota_service.h" | 28 #include "chrome/browser/extensions/extensions_quota_service.h" |
29 #include "chrome/browser/importer/importer_data_types.h" | 29 #include "chrome/browser/importer/importer_data_types.h" |
30 #include "chrome/browser/importer/importer_host.h" | 30 #include "chrome/browser/importer/importer_host.h" |
31 #include "chrome/browser/prefs/pref_service.h" | 31 #include "chrome/browser/prefs/pref_service.h" |
32 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
33 #include "chrome/browser/ui/browser_list.h" | 33 #include "chrome/browser/ui/browser_list.h" |
34 #include "chrome/browser/ui/chrome_select_file_policy.h" | 34 #include "chrome/browser/ui/chrome_select_file_policy.h" |
35 #include "chrome/common/chrome_notification_types.h" | 35 #include "chrome/common/chrome_notification_types.h" |
36 #include "chrome/common/chrome_paths.h" | 36 #include "chrome/common/chrome_paths.h" |
| 37 #include "chrome/common/extensions/api/bookmarks.h" |
37 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
38 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
39 #include "grit/generated_resources.h" | 40 #include "grit/generated_resources.h" |
40 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
41 | 42 |
42 namespace keys = bookmark_extension_api_constants; | 43 namespace keys = bookmark_extension_api_constants; |
| 44 namespace bookmarks = extensions::api::bookmarks; |
43 | 45 |
44 using base::TimeDelta; | 46 using base::TimeDelta; |
| 47 using bookmarks::BookmarkTreeNode; |
45 using content::BrowserThread; | 48 using content::BrowserThread; |
46 using content::WebContents; | 49 using content::WebContents; |
47 | 50 |
48 typedef QuotaLimitHeuristic::Bucket Bucket; | 51 typedef QuotaLimitHeuristic::Bucket Bucket; |
49 typedef QuotaLimitHeuristic::Config Config; | 52 typedef QuotaLimitHeuristic::Config Config; |
50 typedef QuotaLimitHeuristic::BucketList BucketList; | 53 typedef QuotaLimitHeuristic::BucketList BucketList; |
51 typedef ExtensionsQuotaService::TimedLimit TimedLimit; | 54 typedef ExtensionsQuotaService::TimedLimit TimedLimit; |
52 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit; | 55 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit; |
53 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; | 56 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; |
54 | 57 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 base::JSONWriter::Write(&args, &json_args); | 188 base::JSONWriter::Write(&args, &json_args); |
186 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); | 189 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); |
187 } | 190 } |
188 | 191 |
189 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 192 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
190 const BookmarkNode* parent, | 193 const BookmarkNode* parent, |
191 int index) { | 194 int index) { |
192 ListValue args; | 195 ListValue args; |
193 const BookmarkNode* node = parent->GetChild(index); | 196 const BookmarkNode* node = parent->GetChild(index); |
194 args.Append(new StringValue(base::Int64ToString(node->id()))); | 197 args.Append(new StringValue(base::Int64ToString(node->id()))); |
195 DictionaryValue* obj = | 198 BookmarkTreeNode* tree_node = |
196 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 199 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false); |
197 args.Append(obj); | 200 args.Append(tree_node->ToValue().release()); |
198 | 201 |
199 std::string json_args; | 202 std::string json_args; |
200 base::JSONWriter::Write(&args, &json_args); | 203 base::JSONWriter::Write(&args, &json_args); |
201 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); | 204 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); |
202 } | 205 } |
203 | 206 |
204 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( | 207 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( |
205 BookmarkModel* model, | 208 BookmarkModel* model, |
206 const BookmarkNode* parent, | 209 const BookmarkNode* parent, |
207 int index, | 210 int index, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 BookmarkModel* model) { | 284 BookmarkModel* model) { |
282 ListValue args; | 285 ListValue args; |
283 std::string json_args; | 286 std::string json_args; |
284 base::JSONWriter::Write(&args, &json_args); | 287 base::JSONWriter::Write(&args, &json_args); |
285 DispatchEvent(model->profile(), | 288 DispatchEvent(model->profile(), |
286 keys::kOnBookmarkImportEnded, | 289 keys::kOnBookmarkImportEnded, |
287 json_args); | 290 json_args); |
288 } | 291 } |
289 | 292 |
290 bool GetBookmarksFunction::RunImpl() { | 293 bool GetBookmarksFunction::RunImpl() { |
| 294 scoped_ptr<bookmarks::Get::Params> params( |
| 295 bookmarks::Get::Params::Create(*args_)); |
| 296 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 297 |
| 298 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
291 BookmarkModel* model = profile()->GetBookmarkModel(); | 299 BookmarkModel* model = profile()->GetBookmarkModel(); |
292 scoped_ptr<ListValue> json(new ListValue()); | 300 if (params->id_or_id_list_type == |
293 Value* arg0; | 301 bookmarks::Get::Params::ID_OR_ID_LIST_ARRAY) { |
294 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); | 302 std::vector<std::string>* ids = params->id_or_id_list_array.get(); |
295 if (arg0->IsType(Value::TYPE_LIST)) { | 303 size_t count = ids->size(); |
296 const ListValue* ids = static_cast<const ListValue*>(arg0); | |
297 size_t count = ids->GetSize(); | |
298 EXTENSION_FUNCTION_VALIDATE(count > 0); | 304 EXTENSION_FUNCTION_VALIDATE(count > 0); |
299 for (size_t i = 0; i < count; ++i) { | 305 for (size_t i = 0; i < count; ++i) { |
300 int64 id; | 306 int64 id; |
301 std::string id_string; | 307 if (!GetBookmarkIdAsInt64(ids->at(i), &id)) |
302 EXTENSION_FUNCTION_VALIDATE(ids->GetString(i, &id_string)); | |
303 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
304 return false; | 308 return false; |
305 const BookmarkNode* node = model->GetNodeByID(id); | 309 const BookmarkNode* node = model->GetNodeByID(id); |
306 if (!node) { | 310 if (!node) { |
307 error_ = keys::kNoNodeError; | 311 error_ = keys::kNoNodeError; |
308 return false; | 312 return false; |
309 } else { | 313 } else { |
310 bookmark_extension_helpers::AddNode(node, json.get(), false); | 314 bookmark_extension_helpers::AddNode(node, &nodes, false); |
311 } | 315 } |
312 } | 316 } |
313 } else { | 317 } else { |
314 int64 id; | 318 int64 id; |
315 std::string id_string; | 319 if (!GetBookmarkIdAsInt64(*params->id_or_id_list_string, &id)) |
316 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); | |
317 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
318 return false; | 320 return false; |
319 const BookmarkNode* node = model->GetNodeByID(id); | 321 const BookmarkNode* node = model->GetNodeByID(id); |
320 if (!node) { | 322 if (!node) { |
321 error_ = keys::kNoNodeError; | 323 error_ = keys::kNoNodeError; |
322 return false; | 324 return false; |
323 } | 325 } |
324 bookmark_extension_helpers::AddNode(node, json.get(), false); | 326 bookmark_extension_helpers::AddNode(node, &nodes, false); |
325 } | 327 } |
326 | 328 |
327 SetResult(json.release()); | 329 results_ = bookmarks::Get::Results::Create(nodes); |
328 return true; | 330 return true; |
329 } | 331 } |
330 | 332 |
331 bool GetBookmarkChildrenFunction::RunImpl() { | 333 bool GetBookmarkChildrenFunction::RunImpl() { |
332 BookmarkModel* model = profile()->GetBookmarkModel(); | 334 scoped_ptr<bookmarks::GetChildren::Params> params( |
| 335 bookmarks::GetChildren::Params::Create(*args_)); |
| 336 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 337 |
333 int64 id; | 338 int64 id; |
334 std::string id_string; | 339 if (!GetBookmarkIdAsInt64(params->id, &id)) |
335 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | |
336 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
337 return false; | 340 return false; |
338 scoped_ptr<ListValue> json(new ListValue()); | 341 |
339 const BookmarkNode* node = model->GetNodeByID(id); | 342 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
| 343 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); |
340 if (!node) { | 344 if (!node) { |
341 error_ = keys::kNoNodeError; | 345 error_ = keys::kNoNodeError; |
342 return false; | 346 return false; |
343 } | 347 } |
344 int child_count = node->child_count(); | 348 int child_count = node->child_count(); |
345 for (int i = 0; i < child_count; ++i) { | 349 for (int i = 0; i < child_count; ++i) { |
346 const BookmarkNode* child = node->GetChild(i); | 350 const BookmarkNode* child = node->GetChild(i); |
347 bookmark_extension_helpers::AddNode(child, json.get(), false); | 351 bookmark_extension_helpers::AddNode(child, &nodes, false); |
348 } | 352 } |
349 | 353 |
350 SetResult(json.release()); | 354 results_ = bookmarks::GetChildren::Results::Create(nodes); |
351 return true; | 355 return true; |
352 } | 356 } |
353 | 357 |
354 bool GetBookmarkRecentFunction::RunImpl() { | 358 bool GetBookmarkRecentFunction::RunImpl() { |
355 int number_of_items; | 359 scoped_ptr<bookmarks::GetRecent::Params> params( |
356 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items)); | 360 bookmarks::GetRecent::Params::Create(*args_)); |
357 if (number_of_items < 1) | 361 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 362 if (params->number_of_items < 1) |
358 return false; | 363 return false; |
359 | 364 |
360 BookmarkModel* model = profile()->GetBookmarkModel(); | |
361 ListValue* json = new ListValue(); | |
362 std::vector<const BookmarkNode*> nodes; | 365 std::vector<const BookmarkNode*> nodes; |
363 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes); | 366 bookmark_utils::GetMostRecentlyAddedEntries(profile()->GetBookmarkModel(), |
| 367 params->number_of_items, |
| 368 &nodes); |
| 369 |
| 370 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; |
364 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 371 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); |
365 for (; i != nodes.end(); ++i) { | 372 for (; i != nodes.end(); ++i) { |
366 const BookmarkNode* node = *i; | 373 const BookmarkNode* node = *i; |
367 bookmark_extension_helpers::AddNode(node, json, false); | 374 bookmark_extension_helpers::AddNode(node, &tree_nodes, false); |
368 } | 375 } |
369 SetResult(json); | 376 |
| 377 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); |
370 return true; | 378 return true; |
371 } | 379 } |
372 | 380 |
373 bool GetBookmarkTreeFunction::RunImpl() { | 381 bool GetBookmarkTreeFunction::RunImpl() { |
374 BookmarkModel* model = profile()->GetBookmarkModel(); | 382 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
375 scoped_ptr<ListValue> json(new ListValue()); | 383 const BookmarkNode* node = profile()->GetBookmarkModel()->root_node(); |
376 const BookmarkNode* node = model->root_node(); | 384 bookmark_extension_helpers::AddNode(node, &nodes, true); |
377 bookmark_extension_helpers::AddNode(node, json.get(), true); | 385 results_ = bookmarks::GetTree::Results::Create(nodes); |
378 SetResult(json.release()); | |
379 return true; | 386 return true; |
380 } | 387 } |
381 | 388 |
382 bool GetBookmarkSubTreeFunction::RunImpl() { | 389 bool GetBookmarkSubTreeFunction::RunImpl() { |
383 BookmarkModel* model = profile()->GetBookmarkModel(); | 390 scoped_ptr<bookmarks::GetSubTree::Params> params( |
384 scoped_ptr<ListValue> json(new ListValue()); | 391 bookmarks::GetSubTree::Params::Create(*args_)); |
385 Value* arg0; | 392 EXTENSION_FUNCTION_VALIDATE(params.get()); |
386 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); | 393 |
387 int64 id; | 394 int64 id; |
388 std::string id_string; | 395 if (!GetBookmarkIdAsInt64(params->id, &id)) |
389 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); | |
390 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
391 return false; | 396 return false; |
392 const BookmarkNode* node = model->GetNodeByID(id); | 397 |
| 398 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); |
393 if (!node) { | 399 if (!node) { |
394 error_ = keys::kNoNodeError; | 400 error_ = keys::kNoNodeError; |
395 return false; | 401 return false; |
396 } | 402 } |
397 bookmark_extension_helpers::AddNode(node, json.get(), true); | 403 |
398 SetResult(json.release()); | 404 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
| 405 bookmark_extension_helpers::AddNode(node, &nodes, true); |
| 406 results_ = bookmarks::GetSubTree::Results::Create(nodes); |
399 return true; | 407 return true; |
400 } | 408 } |
401 | 409 |
402 bool SearchBookmarksFunction::RunImpl() { | 410 bool SearchBookmarksFunction::RunImpl() { |
403 string16 query; | 411 scoped_ptr<bookmarks::Search::Params> params( |
404 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query)); | 412 bookmarks::Search::Params::Create(*args_)); |
| 413 EXTENSION_FUNCTION_VALIDATE(params.get()); |
405 | 414 |
406 BookmarkModel* model = profile()->GetBookmarkModel(); | |
407 ListValue* json = new ListValue(); | |
408 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 415 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
409 std::vector<const BookmarkNode*> nodes; | 416 std::vector<const BookmarkNode*> nodes; |
410 bookmark_utils::GetBookmarksContainingText(model, query, | 417 bookmark_utils::GetBookmarksContainingText(profile()->GetBookmarkModel(), |
| 418 UTF8ToUTF16(params->query), |
411 std::numeric_limits<int>::max(), | 419 std::numeric_limits<int>::max(), |
412 lang, &nodes); | 420 lang, |
413 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 421 &nodes); |
414 for (; i != nodes.end(); ++i) { | 422 |
415 const BookmarkNode* node = *i; | 423 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; |
416 bookmark_extension_helpers::AddNode(node, json, false); | 424 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); |
| 425 node_iter != nodes.end(); ++node_iter) { |
| 426 bookmark_extension_helpers::AddNode(*node_iter, &tree_nodes, false); |
417 } | 427 } |
418 | 428 |
419 SetResult(json); | 429 results_ = bookmarks::Search::Results::Create(tree_nodes); |
420 return true; | 430 return true; |
421 } | 431 } |
422 | 432 |
423 // static | 433 // static |
424 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, | 434 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, |
425 std::list<int64>* ids, | 435 std::list<int64>* ids, |
426 bool* invalid_id) { | 436 bool* invalid_id) { |
427 std::string id_string; | 437 std::string id_string; |
428 if (!args->GetString(0, &id_string)) | 438 if (!args->GetString(0, &id_string)) |
429 return false; | 439 return false; |
430 int64 id; | 440 int64 id; |
431 if (base::StringToInt64(id_string, &id)) | 441 if (base::StringToInt64(id_string, &id)) |
432 ids->push_back(id); | 442 ids->push_back(id); |
433 else | 443 else |
434 *invalid_id = true; | 444 *invalid_id = true; |
435 return true; | 445 return true; |
436 } | 446 } |
437 | 447 |
438 bool RemoveBookmarkFunction::RunImpl() { | 448 bool RemoveBookmarkFunction::RunImpl() { |
439 if (!EditBookmarksEnabled()) | 449 if (!EditBookmarksEnabled()) |
440 return false; | 450 return false; |
441 std::list<int64> ids; | 451 |
442 bool invalid_id = false; | 452 scoped_ptr<bookmarks::Remove::Params> params( |
443 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 453 bookmarks::Remove::Params::Create(*args_)); |
444 if (invalid_id) { | 454 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 455 |
| 456 int64 id; |
| 457 if (!base::StringToInt64(params->id, &id)) { |
445 error_ = keys::kInvalidIdError; | 458 error_ = keys::kInvalidIdError; |
446 return false; | 459 return false; |
447 } | 460 } |
| 461 |
448 bool recursive = false; | 462 bool recursive = false; |
449 if (name() == RemoveTreeBookmarkFunction::function_name()) | 463 if (name() == RemoveTreeBookmarkFunction::function_name()) |
450 recursive = true; | 464 recursive = true; |
451 | 465 |
452 BookmarkModel* model = profile()->GetBookmarkModel(); | 466 BookmarkModel* model = profile()->GetBookmarkModel(); |
453 size_t count = ids.size(); | 467 if (!bookmark_extension_helpers::RemoveNode(model, id, recursive, &error_)) |
454 EXTENSION_FUNCTION_VALIDATE(count > 0); | 468 return false; |
455 for (std::list<int64>::iterator it = ids.begin(); it != ids.end(); ++it) { | 469 |
456 if (!bookmark_extension_helpers::RemoveNode(model, *it, recursive, &error_)) | |
457 return false; | |
458 } | |
459 return true; | 470 return true; |
460 } | 471 } |
461 | 472 |
462 bool CreateBookmarkFunction::RunImpl() { | 473 bool CreateBookmarkFunction::RunImpl() { |
463 if (!EditBookmarksEnabled()) | 474 if (!EditBookmarksEnabled()) |
464 return false; | 475 return false; |
465 DictionaryValue* json; | 476 |
466 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); | 477 scoped_ptr<bookmarks::Create::Params> params( |
467 EXTENSION_FUNCTION_VALIDATE(json != NULL); | 478 bookmarks::Create::Params::Create(*args_)); |
| 479 EXTENSION_FUNCTION_VALIDATE(params.get()); |
468 | 480 |
469 BookmarkModel* model = profile()->GetBookmarkModel(); | 481 BookmarkModel* model = profile()->GetBookmarkModel(); |
470 int64 parentId; | 482 int64 parentId; |
471 if (!json->HasKey(keys::kParentIdKey)) { | 483 |
| 484 if (!params->bookmark.parent_id.get()) { |
472 // Optional, default to "other bookmarks". | 485 // Optional, default to "other bookmarks". |
473 parentId = model->other_node()->id(); | 486 parentId = model->other_node()->id(); |
474 } else { | 487 } else { |
475 std::string parentId_string; | 488 if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId)) |
476 EXTENSION_FUNCTION_VALIDATE(json->GetString(keys::kParentIdKey, | |
477 &parentId_string)); | |
478 if (!GetBookmarkIdAsInt64(parentId_string, &parentId)) | |
479 return false; | 489 return false; |
480 } | 490 } |
481 const BookmarkNode* parent = model->GetNodeByID(parentId); | 491 const BookmarkNode* parent = model->GetNodeByID(parentId); |
482 if (!parent) { | 492 if (!parent) { |
483 error_ = keys::kNoParentError; | 493 error_ = keys::kNoParentError; |
484 return false; | 494 return false; |
485 } | 495 } |
486 if (parent->is_root()) { // Can't create children of the root. | 496 if (parent->is_root()) { // Can't create children of the root. |
487 error_ = keys::kModifySpecialError; | 497 error_ = keys::kModifySpecialError; |
488 return false; | 498 return false; |
489 } | 499 } |
490 | 500 |
491 int index; | 501 int index; |
492 if (!json->HasKey(keys::kIndexKey)) { // Optional (defaults to end). | 502 if (!params->bookmark.index.get()) { // Optional (defaults to end). |
493 index = parent->child_count(); | 503 index = parent->child_count(); |
494 } else { | 504 } else { |
495 EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIndexKey, &index)); | 505 index = *params->bookmark.index; |
496 if (index > parent->child_count() || index < 0) { | 506 if (index > parent->child_count() || index < 0) { |
497 error_ = keys::kInvalidIndexError; | 507 error_ = keys::kInvalidIndexError; |
498 return false; | 508 return false; |
499 } | 509 } |
500 } | 510 } |
501 | 511 |
502 string16 title; | 512 string16 title; // Optional. |
503 json->GetString(keys::kTitleKey, &title); // Optional. | 513 if (params->bookmark.title.get()) |
504 std::string url_string; | 514 title = UTF8ToUTF16(*params->bookmark.title.get()); |
505 json->GetString(keys::kUrlKey, &url_string); // Optional. | 515 |
| 516 std::string url_string; // Optional. |
| 517 if (params->bookmark.url.get()) |
| 518 url_string = *params->bookmark.url.get(); |
| 519 |
506 GURL url(url_string); | 520 GURL url(url_string); |
507 if (!url_string.empty() && !url.is_valid()) { | 521 if (!url_string.empty() && !url.is_valid()) { |
508 error_ = keys::kInvalidUrlError; | 522 error_ = keys::kInvalidUrlError; |
509 return false; | 523 return false; |
510 } | 524 } |
511 | 525 |
512 const BookmarkNode* node; | 526 const BookmarkNode* node; |
513 if (url_string.length()) | 527 if (url_string.length()) |
514 node = model->AddURL(parent, index, title, url); | 528 node = model->AddURL(parent, index, title, url); |
515 else | 529 else |
516 node = model->AddFolder(parent, index, title); | 530 node = model->AddFolder(parent, index, title); |
517 DCHECK(node); | 531 DCHECK(node); |
518 if (!node) { | 532 if (!node) { |
519 error_ = keys::kNoNodeError; | 533 error_ = keys::kNoNodeError; |
520 return false; | 534 return false; |
521 } | 535 } |
522 | 536 |
523 DictionaryValue* ret = | 537 scoped_ptr<BookmarkTreeNode> ret( |
524 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 538 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
525 SetResult(ret); | 539 results_ = bookmarks::Create::Results::Create(*ret); |
526 | 540 |
527 return true; | 541 return true; |
528 } | 542 } |
529 | 543 |
530 // static | 544 // static |
531 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, | 545 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, |
532 std::list<int64>* ids, | 546 std::list<int64>* ids, |
533 bool* invalid_id) { | 547 bool* invalid_id) { |
534 // For now, Move accepts ID parameters in the same way as an Update. | 548 // For now, Move accepts ID parameters in the same way as an Update. |
535 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); | 549 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); |
536 } | 550 } |
537 | 551 |
538 bool MoveBookmarkFunction::RunImpl() { | 552 bool MoveBookmarkFunction::RunImpl() { |
539 if (!EditBookmarksEnabled()) | 553 if (!EditBookmarksEnabled()) |
540 return false; | 554 return false; |
541 std::list<int64> ids; | 555 |
542 bool invalid_id = false; | 556 scoped_ptr<bookmarks::Move::Params> params( |
543 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 557 bookmarks::Move::Params::Create(*args_)); |
544 if (invalid_id) { | 558 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 559 |
| 560 int64 id; |
| 561 if (!base::StringToInt64(params->id, &id)) { |
545 error_ = keys::kInvalidIdError; | 562 error_ = keys::kInvalidIdError; |
546 return false; | 563 return false; |
547 } | 564 } |
548 EXTENSION_FUNCTION_VALIDATE(ids.size() == 1); | |
549 | |
550 DictionaryValue* destination; | |
551 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &destination)); | |
552 | 565 |
553 BookmarkModel* model = profile()->GetBookmarkModel(); | 566 BookmarkModel* model = profile()->GetBookmarkModel(); |
554 const BookmarkNode* node = model->GetNodeByID(ids.front()); | 567 const BookmarkNode* node = model->GetNodeByID(id); |
555 if (!node) { | 568 if (!node) { |
556 error_ = keys::kNoNodeError; | 569 error_ = keys::kNoNodeError; |
557 return false; | 570 return false; |
558 } | 571 } |
559 if (model->is_permanent_node(node)) { | 572 if (model->is_permanent_node(node)) { |
560 error_ = keys::kModifySpecialError; | 573 error_ = keys::kModifySpecialError; |
561 return false; | 574 return false; |
562 } | 575 } |
563 | 576 |
564 const BookmarkNode* parent = NULL; | 577 const BookmarkNode* parent = NULL; |
565 if (!destination->HasKey(keys::kParentIdKey)) { | 578 if (!params->destination.parent_id.get()) { |
566 // Optional, defaults to current parent. | 579 // Optional, defaults to current parent. |
567 parent = node->parent(); | 580 parent = node->parent(); |
568 } else { | 581 } else { |
569 std::string parentId_string; | |
570 EXTENSION_FUNCTION_VALIDATE(destination->GetString(keys::kParentIdKey, | |
571 &parentId_string)); | |
572 int64 parentId; | 582 int64 parentId; |
573 if (!GetBookmarkIdAsInt64(parentId_string, &parentId)) | 583 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) |
574 return false; | 584 return false; |
575 | 585 |
576 parent = model->GetNodeByID(parentId); | 586 parent = model->GetNodeByID(parentId); |
577 } | 587 } |
578 if (!parent) { | 588 if (!parent) { |
579 error_ = keys::kNoParentError; | 589 error_ = keys::kNoParentError; |
580 // TODO(erikkay) return an error message. | 590 // TODO(erikkay) return an error message. |
581 return false; | 591 return false; |
582 } | 592 } |
583 if (parent == model->root_node()) { | 593 if (parent == model->root_node()) { |
584 error_ = keys::kModifySpecialError; | 594 error_ = keys::kModifySpecialError; |
585 return false; | 595 return false; |
586 } | 596 } |
587 | 597 |
588 int index; | 598 int index; |
589 if (destination->HasKey(keys::kIndexKey)) { // Optional (defaults to end). | 599 if (params->destination.index.get()) { // Optional (defaults to end). |
590 EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kIndexKey, | 600 index = *params->destination.index; |
591 &index)); | |
592 if (index > parent->child_count() || index < 0) { | 601 if (index > parent->child_count() || index < 0) { |
593 error_ = keys::kInvalidIndexError; | 602 error_ = keys::kInvalidIndexError; |
594 return false; | 603 return false; |
595 } | 604 } |
596 } else { | 605 } else { |
597 index = parent->child_count(); | 606 index = parent->child_count(); |
598 } | 607 } |
599 | 608 |
600 model->Move(node, parent, index); | 609 model->Move(node, parent, index); |
601 | 610 |
602 DictionaryValue* ret = | 611 scoped_ptr<BookmarkTreeNode> tree_node( |
603 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 612 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
604 SetResult(ret); | 613 results_ = bookmarks::Move::Results::Create(*tree_node); |
605 | 614 |
606 return true; | 615 return true; |
607 } | 616 } |
608 | 617 |
609 // static | 618 // static |
610 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, | 619 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, |
611 std::list<int64>* ids, | 620 std::list<int64>* ids, |
612 bool* invalid_id) { | 621 bool* invalid_id) { |
613 // For now, Update accepts ID parameters in the same way as an Remove. | 622 // For now, Update accepts ID parameters in the same way as an Remove. |
614 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); | 623 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); |
615 } | 624 } |
616 | 625 |
617 bool UpdateBookmarkFunction::RunImpl() { | 626 bool UpdateBookmarkFunction::RunImpl() { |
618 if (!EditBookmarksEnabled()) | 627 if (!EditBookmarksEnabled()) |
619 return false; | 628 return false; |
620 std::list<int64> ids; | 629 |
621 bool invalid_id = false; | 630 scoped_ptr<bookmarks::Update::Params> params( |
622 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 631 bookmarks::Update::Params::Create(*args_)); |
623 if (invalid_id) { | 632 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 633 |
| 634 int64 id; |
| 635 if (!base::StringToInt64(params->id, &id)) { |
624 error_ = keys::kInvalidIdError; | 636 error_ = keys::kInvalidIdError; |
625 return false; | 637 return false; |
626 } | 638 } |
627 EXTENSION_FUNCTION_VALIDATE(ids.size() == 1); | |
628 | 639 |
629 DictionaryValue* updates; | 640 BookmarkModel* model = profile()->GetBookmarkModel(); |
630 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &updates)); | |
631 | 641 |
632 // Optional but we need to distinguish non present from an empty title. | 642 // Optional but we need to distinguish non present from an empty title. |
633 string16 title; | 643 string16 title; |
634 const bool has_title = updates->GetString(keys::kTitleKey, &title); | 644 bool has_title = false; |
| 645 if (params->changes.title.get()) { |
| 646 title = UTF8ToUTF16(*params->changes.title); |
| 647 has_title = true; |
| 648 } |
635 | 649 |
636 // Optional. | 650 // Optional. |
637 std::string url_string; | 651 std::string url_string; |
638 updates->GetString(keys::kUrlKey, &url_string); | 652 if (params->changes.url.get()) |
| 653 url_string = *params->changes.url; |
639 GURL url(url_string); | 654 GURL url(url_string); |
640 if (!url_string.empty() && !url.is_valid()) { | 655 if (!url_string.empty() && !url.is_valid()) { |
641 error_ = keys::kInvalidUrlError; | 656 error_ = keys::kInvalidUrlError; |
642 return false; | 657 return false; |
643 } | 658 } |
644 | 659 |
645 BookmarkModel* model = profile()->GetBookmarkModel(); | 660 const BookmarkNode* node = model->GetNodeByID(id); |
646 const BookmarkNode* node = model->GetNodeByID(ids.front()); | |
647 if (!node) { | 661 if (!node) { |
648 error_ = keys::kNoNodeError; | 662 error_ = keys::kNoNodeError; |
649 return false; | 663 return false; |
650 } | 664 } |
651 if (model->is_permanent_node(node)) { | 665 if (model->is_permanent_node(node)) { |
652 error_ = keys::kModifySpecialError; | 666 error_ = keys::kModifySpecialError; |
653 return false; | 667 return false; |
654 } | 668 } |
655 if (has_title) | 669 if (has_title) |
656 model->SetTitle(node, title); | 670 model->SetTitle(node, title); |
657 if (!url.is_empty()) | 671 if (!url.is_empty()) |
658 model->SetURL(node, url); | 672 model->SetURL(node, url); |
659 | 673 |
660 DictionaryValue* ret = | 674 scoped_ptr<BookmarkTreeNode> tree_node( |
661 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 675 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
662 SetResult(ret); | 676 results_ = bookmarks::Update::Results::Create(*tree_node); |
663 | |
664 return true; | 677 return true; |
665 } | 678 } |
666 | 679 |
667 // Mapper superclass for BookmarkFunctions. | 680 // Mapper superclass for BookmarkFunctions. |
668 template <typename BucketIdType> | 681 template <typename BucketIdType> |
669 class BookmarkBucketMapper : public BucketMapper { | 682 class BookmarkBucketMapper : public BucketMapper { |
670 public: | 683 public: |
671 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } | 684 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } |
672 protected: | 685 protected: |
673 Bucket* GetBucket(const BucketIdType& id) { | 686 Bucket* GetBucket(const BucketIdType& id) { |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 int index, | 958 int index, |
946 void* params) { | 959 void* params) { |
947 #if !defined(OS_ANDROID) | 960 #if !defined(OS_ANDROID) |
948 // Android does not have support for the standard exporter. | 961 // Android does not have support for the standard exporter. |
949 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 962 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
950 // Android. | 963 // Android. |
951 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); | 964 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); |
952 #endif | 965 #endif |
953 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 966 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
954 } | 967 } |
OLD | NEW |