| 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 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 if (!GetBookmarkIdAsInt64(id_string, &id)) | 317 if (!GetBookmarkIdAsInt64(id_string, &id)) |
| 318 return false; | 318 return false; |
| 319 const BookmarkNode* node = model->GetNodeByID(id); | 319 const BookmarkNode* node = model->GetNodeByID(id); |
| 320 if (!node) { | 320 if (!node) { |
| 321 error_ = keys::kNoNodeError; | 321 error_ = keys::kNoNodeError; |
| 322 return false; | 322 return false; |
| 323 } | 323 } |
| 324 bookmark_extension_helpers::AddNode(node, json.get(), false); | 324 bookmark_extension_helpers::AddNode(node, json.get(), false); |
| 325 } | 325 } |
| 326 | 326 |
| 327 result_.reset(json.release()); | 327 SetResult(json.release()); |
| 328 return true; | 328 return true; |
| 329 } | 329 } |
| 330 | 330 |
| 331 bool GetBookmarkChildrenFunction::RunImpl() { | 331 bool GetBookmarkChildrenFunction::RunImpl() { |
| 332 BookmarkModel* model = profile()->GetBookmarkModel(); | 332 BookmarkModel* model = profile()->GetBookmarkModel(); |
| 333 int64 id; | 333 int64 id; |
| 334 std::string id_string; | 334 std::string id_string; |
| 335 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | 335 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); |
| 336 if (!GetBookmarkIdAsInt64(id_string, &id)) | 336 if (!GetBookmarkIdAsInt64(id_string, &id)) |
| 337 return false; | 337 return false; |
| 338 scoped_ptr<ListValue> json(new ListValue()); | 338 scoped_ptr<ListValue> json(new ListValue()); |
| 339 const BookmarkNode* node = model->GetNodeByID(id); | 339 const BookmarkNode* node = model->GetNodeByID(id); |
| 340 if (!node) { | 340 if (!node) { |
| 341 error_ = keys::kNoNodeError; | 341 error_ = keys::kNoNodeError; |
| 342 return false; | 342 return false; |
| 343 } | 343 } |
| 344 int child_count = node->child_count(); | 344 int child_count = node->child_count(); |
| 345 for (int i = 0; i < child_count; ++i) { | 345 for (int i = 0; i < child_count; ++i) { |
| 346 const BookmarkNode* child = node->GetChild(i); | 346 const BookmarkNode* child = node->GetChild(i); |
| 347 bookmark_extension_helpers::AddNode(child, json.get(), false); | 347 bookmark_extension_helpers::AddNode(child, json.get(), false); |
| 348 } | 348 } |
| 349 | 349 |
| 350 result_.reset(json.release()); | 350 SetResult(json.release()); |
| 351 return true; | 351 return true; |
| 352 } | 352 } |
| 353 | 353 |
| 354 bool GetBookmarkRecentFunction::RunImpl() { | 354 bool GetBookmarkRecentFunction::RunImpl() { |
| 355 int number_of_items; | 355 int number_of_items; |
| 356 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items)); | 356 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items)); |
| 357 if (number_of_items < 1) | 357 if (number_of_items < 1) |
| 358 return false; | 358 return false; |
| 359 | 359 |
| 360 BookmarkModel* model = profile()->GetBookmarkModel(); | 360 BookmarkModel* model = profile()->GetBookmarkModel(); |
| 361 ListValue* json = new ListValue(); | 361 ListValue* json = new ListValue(); |
| 362 std::vector<const BookmarkNode*> nodes; | 362 std::vector<const BookmarkNode*> nodes; |
| 363 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes); | 363 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes); |
| 364 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 364 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); |
| 365 for (; i != nodes.end(); ++i) { | 365 for (; i != nodes.end(); ++i) { |
| 366 const BookmarkNode* node = *i; | 366 const BookmarkNode* node = *i; |
| 367 bookmark_extension_helpers::AddNode(node, json, false); | 367 bookmark_extension_helpers::AddNode(node, json, false); |
| 368 } | 368 } |
| 369 result_.reset(json); | 369 SetResult(json); |
| 370 return true; | 370 return true; |
| 371 } | 371 } |
| 372 | 372 |
| 373 bool GetBookmarkTreeFunction::RunImpl() { | 373 bool GetBookmarkTreeFunction::RunImpl() { |
| 374 BookmarkModel* model = profile()->GetBookmarkModel(); | 374 BookmarkModel* model = profile()->GetBookmarkModel(); |
| 375 scoped_ptr<ListValue> json(new ListValue()); | 375 scoped_ptr<ListValue> json(new ListValue()); |
| 376 const BookmarkNode* node = model->root_node(); | 376 const BookmarkNode* node = model->root_node(); |
| 377 bookmark_extension_helpers::AddNode(node, json.get(), true); | 377 bookmark_extension_helpers::AddNode(node, json.get(), true); |
| 378 result_.reset(json.release()); | 378 SetResult(json.release()); |
| 379 return true; | 379 return true; |
| 380 } | 380 } |
| 381 | 381 |
| 382 bool GetBookmarkSubTreeFunction::RunImpl() { | 382 bool GetBookmarkSubTreeFunction::RunImpl() { |
| 383 BookmarkModel* model = profile()->GetBookmarkModel(); | 383 BookmarkModel* model = profile()->GetBookmarkModel(); |
| 384 scoped_ptr<ListValue> json(new ListValue()); | 384 scoped_ptr<ListValue> json(new ListValue()); |
| 385 Value* arg0; | 385 Value* arg0; |
| 386 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); | 386 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); |
| 387 int64 id; | 387 int64 id; |
| 388 std::string id_string; | 388 std::string id_string; |
| 389 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); | 389 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); |
| 390 if (!GetBookmarkIdAsInt64(id_string, &id)) | 390 if (!GetBookmarkIdAsInt64(id_string, &id)) |
| 391 return false; | 391 return false; |
| 392 const BookmarkNode* node = model->GetNodeByID(id); | 392 const BookmarkNode* node = model->GetNodeByID(id); |
| 393 if (!node) { | 393 if (!node) { |
| 394 error_ = keys::kNoNodeError; | 394 error_ = keys::kNoNodeError; |
| 395 return false; | 395 return false; |
| 396 } | 396 } |
| 397 bookmark_extension_helpers::AddNode(node, json.get(), true); | 397 bookmark_extension_helpers::AddNode(node, json.get(), true); |
| 398 result_.reset(json.release()); | 398 SetResult(json.release()); |
| 399 return true; | 399 return true; |
| 400 } | 400 } |
| 401 | 401 |
| 402 bool SearchBookmarksFunction::RunImpl() { | 402 bool SearchBookmarksFunction::RunImpl() { |
| 403 string16 query; | 403 string16 query; |
| 404 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query)); | 404 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query)); |
| 405 | 405 |
| 406 BookmarkModel* model = profile()->GetBookmarkModel(); | 406 BookmarkModel* model = profile()->GetBookmarkModel(); |
| 407 ListValue* json = new ListValue(); | 407 ListValue* json = new ListValue(); |
| 408 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 408 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 409 std::vector<const BookmarkNode*> nodes; | 409 std::vector<const BookmarkNode*> nodes; |
| 410 bookmark_utils::GetBookmarksContainingText(model, query, | 410 bookmark_utils::GetBookmarksContainingText(model, query, |
| 411 std::numeric_limits<int>::max(), | 411 std::numeric_limits<int>::max(), |
| 412 lang, &nodes); | 412 lang, &nodes); |
| 413 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 413 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); |
| 414 for (; i != nodes.end(); ++i) { | 414 for (; i != nodes.end(); ++i) { |
| 415 const BookmarkNode* node = *i; | 415 const BookmarkNode* node = *i; |
| 416 bookmark_extension_helpers::AddNode(node, json, false); | 416 bookmark_extension_helpers::AddNode(node, json, false); |
| 417 } | 417 } |
| 418 | 418 |
| 419 result_.reset(json); | 419 SetResult(json); |
| 420 return true; | 420 return true; |
| 421 } | 421 } |
| 422 | 422 |
| 423 // static | 423 // static |
| 424 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, | 424 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, |
| 425 std::list<int64>* ids, | 425 std::list<int64>* ids, |
| 426 bool* invalid_id) { | 426 bool* invalid_id) { |
| 427 std::string id_string; | 427 std::string id_string; |
| 428 if (!args->GetString(0, &id_string)) | 428 if (!args->GetString(0, &id_string)) |
| 429 return false; | 429 return false; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 else | 515 else |
| 516 node = model->AddFolder(parent, index, title); | 516 node = model->AddFolder(parent, index, title); |
| 517 DCHECK(node); | 517 DCHECK(node); |
| 518 if (!node) { | 518 if (!node) { |
| 519 error_ = keys::kNoNodeError; | 519 error_ = keys::kNoNodeError; |
| 520 return false; | 520 return false; |
| 521 } | 521 } |
| 522 | 522 |
| 523 DictionaryValue* ret = | 523 DictionaryValue* ret = |
| 524 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 524 bookmark_extension_helpers::GetNodeDictionary(node, false, false); |
| 525 result_.reset(ret); | 525 SetResult(ret); |
| 526 | 526 |
| 527 return true; | 527 return true; |
| 528 } | 528 } |
| 529 | 529 |
| 530 // static | 530 // static |
| 531 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, | 531 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, |
| 532 std::list<int64>* ids, | 532 std::list<int64>* ids, |
| 533 bool* invalid_id) { | 533 bool* invalid_id) { |
| 534 // For now, Move accepts ID parameters in the same way as an Update. | 534 // For now, Move accepts ID parameters in the same way as an Update. |
| 535 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); | 535 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 return false; | 594 return false; |
| 595 } | 595 } |
| 596 } else { | 596 } else { |
| 597 index = parent->child_count(); | 597 index = parent->child_count(); |
| 598 } | 598 } |
| 599 | 599 |
| 600 model->Move(node, parent, index); | 600 model->Move(node, parent, index); |
| 601 | 601 |
| 602 DictionaryValue* ret = | 602 DictionaryValue* ret = |
| 603 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 603 bookmark_extension_helpers::GetNodeDictionary(node, false, false); |
| 604 result_.reset(ret); | 604 SetResult(ret); |
| 605 | 605 |
| 606 return true; | 606 return true; |
| 607 } | 607 } |
| 608 | 608 |
| 609 // static | 609 // static |
| 610 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, | 610 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, |
| 611 std::list<int64>* ids, | 611 std::list<int64>* ids, |
| 612 bool* invalid_id) { | 612 bool* invalid_id) { |
| 613 // For now, Update accepts ID parameters in the same way as an Remove. | 613 // For now, Update accepts ID parameters in the same way as an Remove. |
| 614 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); | 614 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 error_ = keys::kModifySpecialError; | 652 error_ = keys::kModifySpecialError; |
| 653 return false; | 653 return false; |
| 654 } | 654 } |
| 655 if (has_title) | 655 if (has_title) |
| 656 model->SetTitle(node, title); | 656 model->SetTitle(node, title); |
| 657 if (!url.is_empty()) | 657 if (!url.is_empty()) |
| 658 model->SetURL(node, url); | 658 model->SetURL(node, url); |
| 659 | 659 |
| 660 DictionaryValue* ret = | 660 DictionaryValue* ret = |
| 661 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 661 bookmark_extension_helpers::GetNodeDictionary(node, false, false); |
| 662 result_.reset(ret); | 662 SetResult(ret); |
| 663 | 663 |
| 664 return true; | 664 return true; |
| 665 } | 665 } |
| 666 | 666 |
| 667 // Mapper superclass for BookmarkFunctions. | 667 // Mapper superclass for BookmarkFunctions. |
| 668 template <typename BucketIdType> | 668 template <typename BucketIdType> |
| 669 class BookmarkBucketMapper : public BucketMapper { | 669 class BookmarkBucketMapper : public BucketMapper { |
| 670 public: | 670 public: |
| 671 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } | 671 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } |
| 672 protected: | 672 protected: |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 int index, | 945 int index, |
| 946 void* params) { | 946 void* params) { |
| 947 #if !defined(OS_ANDROID) | 947 #if !defined(OS_ANDROID) |
| 948 // Android does not have support for the standard exporter. | 948 // Android does not have support for the standard exporter. |
| 949 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 949 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
| 950 // Android. | 950 // Android. |
| 951 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); | 951 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); |
| 952 #endif | 952 #endif |
| 953 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 953 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 954 } | 954 } |
| OLD | NEW |