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; |
43 | 44 |
45 namespace Create = extensions::api::bookmarks::Create; | |
brettw
2012/07/26 18:03:15
These namespaces seem really weird to me. It's con
mitchellwrosen
2012/07/26 18:05:14
It's the only way of using the JSON schema compile
brettw
2012/07/27 20:05:10
It wouldn't look so weird if the users of this cod
mitchellwrosen
2012/07/27 22:53:21
Done.
| |
46 namespace Get = extensions::api::bookmarks::Get; | |
47 namespace GetChildren = extensions::api::bookmarks::GetChildren; | |
48 namespace GetRecent = extensions::api::bookmarks::GetRecent; | |
49 namespace GetSubTree = extensions::api::bookmarks::GetSubTree; | |
50 namespace GetTree = extensions::api::bookmarks::GetTree; | |
51 namespace Move = extensions::api::bookmarks::Move; | |
52 namespace Remove = extensions::api::bookmarks::Remove; | |
53 namespace Search = extensions::api::bookmarks::Search; | |
54 namespace Update = extensions::api::bookmarks::Update; | |
55 | |
44 using base::TimeDelta; | 56 using base::TimeDelta; |
45 using content::BrowserThread; | 57 using content::BrowserThread; |
46 using content::WebContents; | 58 using content::WebContents; |
59 using extensions::api::bookmarks::BookmarkTreeNode; | |
47 | 60 |
48 typedef QuotaLimitHeuristic::Bucket Bucket; | 61 typedef QuotaLimitHeuristic::Bucket Bucket; |
49 typedef QuotaLimitHeuristic::Config Config; | 62 typedef QuotaLimitHeuristic::Config Config; |
50 typedef QuotaLimitHeuristic::BucketList BucketList; | 63 typedef QuotaLimitHeuristic::BucketList BucketList; |
51 typedef ExtensionsQuotaService::TimedLimit TimedLimit; | 64 typedef ExtensionsQuotaService::TimedLimit TimedLimit; |
52 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit; | 65 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit; |
53 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; | 66 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; |
54 | 67 |
55 namespace { | 68 namespace { |
56 | 69 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 base::JSONWriter::Write(&args, &json_args); | 198 base::JSONWriter::Write(&args, &json_args); |
186 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); | 199 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); |
187 } | 200 } |
188 | 201 |
189 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 202 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
190 const BookmarkNode* parent, | 203 const BookmarkNode* parent, |
191 int index) { | 204 int index) { |
192 ListValue args; | 205 ListValue args; |
193 const BookmarkNode* node = parent->GetChild(index); | 206 const BookmarkNode* node = parent->GetChild(index); |
194 args.Append(new StringValue(base::Int64ToString(node->id()))); | 207 args.Append(new StringValue(base::Int64ToString(node->id()))); |
195 DictionaryValue* obj = | 208 BookmarkTreeNode* obj = |
196 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 209 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false); |
197 args.Append(obj); | 210 args.Append(obj->ToValue().get()); |
198 | 211 |
199 std::string json_args; | 212 std::string json_args; |
200 base::JSONWriter::Write(&args, &json_args); | 213 base::JSONWriter::Write(&args, &json_args); |
201 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); | 214 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); |
202 } | 215 } |
203 | 216 |
204 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( | 217 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( |
205 BookmarkModel* model, | 218 BookmarkModel* model, |
206 const BookmarkNode* parent, | 219 const BookmarkNode* parent, |
207 int index, | 220 int index, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 BookmarkModel* model) { | 294 BookmarkModel* model) { |
282 ListValue args; | 295 ListValue args; |
283 std::string json_args; | 296 std::string json_args; |
284 base::JSONWriter::Write(&args, &json_args); | 297 base::JSONWriter::Write(&args, &json_args); |
285 DispatchEvent(model->profile(), | 298 DispatchEvent(model->profile(), |
286 keys::kOnBookmarkImportEnded, | 299 keys::kOnBookmarkImportEnded, |
287 json_args); | 300 json_args); |
288 } | 301 } |
289 | 302 |
290 bool GetBookmarksFunction::RunImpl() { | 303 bool GetBookmarksFunction::RunImpl() { |
304 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); | |
305 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
306 | |
307 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | |
291 BookmarkModel* model = profile()->GetBookmarkModel(); | 308 BookmarkModel* model = profile()->GetBookmarkModel(); |
292 scoped_ptr<ListValue> json(new ListValue()); | 309 if (params->id_or_id_list_type == Get::Params::ID_OR_ID_LIST_ARRAY) { |
293 Value* arg0; | 310 std::vector<std::string>* ids = params->id_or_id_list_array.get(); |
294 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); | 311 size_t count = ids->size(); |
295 if (arg0->IsType(Value::TYPE_LIST)) { | |
296 const ListValue* ids = static_cast<const ListValue*>(arg0); | |
297 size_t count = ids->GetSize(); | |
298 EXTENSION_FUNCTION_VALIDATE(count > 0); | 312 EXTENSION_FUNCTION_VALIDATE(count > 0); |
299 for (size_t i = 0; i < count; ++i) { | 313 for (size_t i = 0; i < count; ++i) { |
300 int64 id; | 314 int64 id; |
301 std::string id_string; | 315 if (!GetBookmarkIdAsInt64(ids->at(i), &id)) |
302 EXTENSION_FUNCTION_VALIDATE(ids->GetString(i, &id_string)); | |
303 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
304 return false; | 316 return false; |
305 const BookmarkNode* node = model->GetNodeByID(id); | 317 const BookmarkNode* node = model->GetNodeByID(id); |
306 if (!node) { | 318 if (!node) { |
307 error_ = keys::kNoNodeError; | 319 error_ = keys::kNoNodeError; |
308 return false; | 320 return false; |
309 } else { | 321 } else { |
310 bookmark_extension_helpers::AddNode(node, json.get(), false); | 322 bookmark_extension_helpers::AddNode(node, &nodes, false); |
311 } | 323 } |
312 } | 324 } |
313 } else { | 325 } else { |
314 int64 id; | 326 int64 id; |
315 std::string id_string; | 327 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; | 328 return false; |
319 const BookmarkNode* node = model->GetNodeByID(id); | 329 const BookmarkNode* node = model->GetNodeByID(id); |
320 if (!node) { | 330 if (!node) { |
321 error_ = keys::kNoNodeError; | 331 error_ = keys::kNoNodeError; |
322 return false; | 332 return false; |
323 } | 333 } |
324 bookmark_extension_helpers::AddNode(node, json.get(), false); | 334 bookmark_extension_helpers::AddNode(node, &nodes, false); |
325 } | 335 } |
326 | 336 |
327 SetResult(json.release()); | 337 results_ = Get::Results::Create(nodes); |
328 return true; | 338 return true; |
329 } | 339 } |
330 | 340 |
331 bool GetBookmarkChildrenFunction::RunImpl() { | 341 bool GetBookmarkChildrenFunction::RunImpl() { |
332 BookmarkModel* model = profile()->GetBookmarkModel(); | 342 scoped_ptr<GetChildren::Params> params(GetChildren::Params::Create(*args_)); |
343 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
344 | |
333 int64 id; | 345 int64 id; |
334 std::string id_string; | 346 if (!GetBookmarkIdAsInt64(params->id, &id)) |
335 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | |
336 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
337 return false; | 347 return false; |
338 scoped_ptr<ListValue> json(new ListValue()); | 348 |
339 const BookmarkNode* node = model->GetNodeByID(id); | 349 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
350 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); | |
340 if (!node) { | 351 if (!node) { |
341 error_ = keys::kNoNodeError; | 352 error_ = keys::kNoNodeError; |
342 return false; | 353 return false; |
343 } | 354 } |
344 int child_count = node->child_count(); | 355 int child_count = node->child_count(); |
345 for (int i = 0; i < child_count; ++i) { | 356 for (int i = 0; i < child_count; ++i) { |
346 const BookmarkNode* child = node->GetChild(i); | 357 const BookmarkNode* child = node->GetChild(i); |
347 bookmark_extension_helpers::AddNode(child, json.get(), false); | 358 bookmark_extension_helpers::AddNode(child, &nodes, false); |
348 } | 359 } |
349 | 360 |
350 SetResult(json.release()); | 361 results_ = GetChildren::Results::Create(nodes); |
351 return true; | 362 return true; |
352 } | 363 } |
353 | 364 |
354 bool GetBookmarkRecentFunction::RunImpl() { | 365 bool GetBookmarkRecentFunction::RunImpl() { |
355 int number_of_items; | 366 scoped_ptr<GetRecent::Params> params(GetRecent::Params::Create(*args_)); |
356 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items)); | 367 EXTENSION_FUNCTION_VALIDATE(params.get()); |
357 if (number_of_items < 1) | 368 if (params->number_of_items < 1) |
358 return false; | 369 return false; |
359 | 370 |
360 BookmarkModel* model = profile()->GetBookmarkModel(); | |
361 ListValue* json = new ListValue(); | |
362 std::vector<const BookmarkNode*> nodes; | 371 std::vector<const BookmarkNode*> nodes; |
363 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes); | 372 bookmark_utils::GetMostRecentlyAddedEntries(profile()->GetBookmarkModel(), |
373 params->number_of_items, | |
374 &nodes); | |
375 | |
376 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; | |
364 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 377 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); |
365 for (; i != nodes.end(); ++i) { | 378 for (; i != nodes.end(); ++i) { |
366 const BookmarkNode* node = *i; | 379 const BookmarkNode* node = *i; |
367 bookmark_extension_helpers::AddNode(node, json, false); | 380 bookmark_extension_helpers::AddNode(node, &tree_nodes, false); |
368 } | 381 } |
369 SetResult(json); | 382 |
383 results_ = GetRecent::Results::Create(tree_nodes); | |
370 return true; | 384 return true; |
371 } | 385 } |
372 | 386 |
373 bool GetBookmarkTreeFunction::RunImpl() { | 387 bool GetBookmarkTreeFunction::RunImpl() { |
374 BookmarkModel* model = profile()->GetBookmarkModel(); | 388 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
375 scoped_ptr<ListValue> json(new ListValue()); | 389 const BookmarkNode* node = profile()->GetBookmarkModel()->root_node(); |
376 const BookmarkNode* node = model->root_node(); | 390 bookmark_extension_helpers::AddNode(node, &nodes, true); |
377 bookmark_extension_helpers::AddNode(node, json.get(), true); | 391 results_ = GetTree::Results::Create(nodes); |
378 SetResult(json.release()); | |
379 return true; | 392 return true; |
380 } | 393 } |
381 | 394 |
382 bool GetBookmarkSubTreeFunction::RunImpl() { | 395 bool GetBookmarkSubTreeFunction::RunImpl() { |
383 BookmarkModel* model = profile()->GetBookmarkModel(); | 396 scoped_ptr<GetSubTree::Params> params(GetSubTree::Params::Create(*args_)); |
384 scoped_ptr<ListValue> json(new ListValue()); | 397 EXTENSION_FUNCTION_VALIDATE(params.get()); |
385 Value* arg0; | 398 |
386 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); | |
387 int64 id; | 399 int64 id; |
388 std::string id_string; | 400 if (!GetBookmarkIdAsInt64(params->id, &id)) |
389 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); | |
390 if (!GetBookmarkIdAsInt64(id_string, &id)) | |
391 return false; | 401 return false; |
392 const BookmarkNode* node = model->GetNodeByID(id); | 402 |
403 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); | |
393 if (!node) { | 404 if (!node) { |
394 error_ = keys::kNoNodeError; | 405 error_ = keys::kNoNodeError; |
395 return false; | 406 return false; |
396 } | 407 } |
397 bookmark_extension_helpers::AddNode(node, json.get(), true); | 408 |
398 SetResult(json.release()); | 409 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
410 bookmark_extension_helpers::AddNode(node, &nodes, true); | |
411 results_ = GetSubTree::Results::Create(nodes); | |
399 return true; | 412 return true; |
400 } | 413 } |
401 | 414 |
402 bool SearchBookmarksFunction::RunImpl() { | 415 bool SearchBookmarksFunction::RunImpl() { |
403 string16 query; | 416 scoped_ptr<Search::Params> params(Search::Params::Create(*args_)); |
404 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query)); | 417 EXTENSION_FUNCTION_VALIDATE(params.get()); |
405 | 418 |
406 BookmarkModel* model = profile()->GetBookmarkModel(); | |
407 ListValue* json = new ListValue(); | |
408 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 419 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
409 std::vector<const BookmarkNode*> nodes; | 420 std::vector<const BookmarkNode*> nodes; |
410 bookmark_utils::GetBookmarksContainingText(model, query, | 421 bookmark_utils::GetBookmarksContainingText(profile()->GetBookmarkModel(), |
422 UTF8ToUTF16(params->query), | |
411 std::numeric_limits<int>::max(), | 423 std::numeric_limits<int>::max(), |
412 lang, &nodes); | 424 lang, |
413 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 425 &nodes); |
414 for (; i != nodes.end(); ++i) { | 426 |
415 const BookmarkNode* node = *i; | 427 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; |
416 bookmark_extension_helpers::AddNode(node, json, false); | 428 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); |
429 node_iter != nodes.end(); ++node_iter) { | |
430 bookmark_extension_helpers::AddNode(*node_iter, &tree_nodes, false); | |
417 } | 431 } |
418 | 432 |
419 SetResult(json); | 433 results_ = Search::Results::Create(tree_nodes); |
420 return true; | 434 return true; |
421 } | 435 } |
422 | 436 |
423 // static | 437 // static |
424 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, | 438 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, |
425 std::list<int64>* ids, | 439 std::list<int64>* ids, |
426 bool* invalid_id) { | 440 bool* invalid_id) { |
427 std::string id_string; | 441 std::string id_string; |
428 if (!args->GetString(0, &id_string)) | 442 if (!args->GetString(0, &id_string)) |
429 return false; | 443 return false; |
430 int64 id; | 444 int64 id; |
431 if (base::StringToInt64(id_string, &id)) | 445 if (base::StringToInt64(id_string, &id)) |
432 ids->push_back(id); | 446 ids->push_back(id); |
433 else | 447 else |
434 *invalid_id = true; | 448 *invalid_id = true; |
435 return true; | 449 return true; |
436 } | 450 } |
437 | 451 |
438 bool RemoveBookmarkFunction::RunImpl() { | 452 bool RemoveBookmarkFunction::RunImpl() { |
439 if (!EditBookmarksEnabled()) | 453 if (!EditBookmarksEnabled()) |
440 return false; | 454 return false; |
441 std::list<int64> ids; | 455 |
442 bool invalid_id = false; | 456 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
443 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 457 EXTENSION_FUNCTION_VALIDATE(params.get()); |
444 if (invalid_id) { | 458 |
459 int64 id; | |
460 if (!base::StringToInt64(params->id, &id)) { | |
445 error_ = keys::kInvalidIdError; | 461 error_ = keys::kInvalidIdError; |
446 return false; | 462 return false; |
447 } | 463 } |
464 | |
448 bool recursive = false; | 465 bool recursive = false; |
449 if (name() == RemoveTreeBookmarkFunction::function_name()) | 466 if (name() == RemoveTreeBookmarkFunction::function_name()) |
450 recursive = true; | 467 recursive = true; |
451 | 468 |
452 BookmarkModel* model = profile()->GetBookmarkModel(); | 469 BookmarkModel* model = profile()->GetBookmarkModel(); |
453 size_t count = ids.size(); | 470 if (!bookmark_extension_helpers::RemoveNode(model, id, recursive, &error_)) |
454 EXTENSION_FUNCTION_VALIDATE(count > 0); | 471 return false; |
455 for (std::list<int64>::iterator it = ids.begin(); it != ids.end(); ++it) { | 472 |
456 if (!bookmark_extension_helpers::RemoveNode(model, *it, recursive, &error_)) | |
457 return false; | |
458 } | |
459 return true; | 473 return true; |
460 } | 474 } |
461 | 475 |
462 bool CreateBookmarkFunction::RunImpl() { | 476 bool CreateBookmarkFunction::RunImpl() { |
463 if (!EditBookmarksEnabled()) | 477 if (!EditBookmarksEnabled()) |
464 return false; | 478 return false; |
465 DictionaryValue* json; | 479 |
466 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); | 480 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); |
467 EXTENSION_FUNCTION_VALIDATE(json != NULL); | 481 EXTENSION_FUNCTION_VALIDATE(params.get()); |
468 | 482 |
469 BookmarkModel* model = profile()->GetBookmarkModel(); | 483 BookmarkModel* model = profile()->GetBookmarkModel(); |
470 int64 parentId; | 484 int64 parentId; |
471 if (!json->HasKey(keys::kParentIdKey)) { | 485 |
486 if (!params->bookmark.parent_id.get()) { | |
472 // Optional, default to "other bookmarks". | 487 // Optional, default to "other bookmarks". |
473 parentId = model->other_node()->id(); | 488 parentId = model->other_node()->id(); |
474 } else { | 489 } else { |
475 std::string parentId_string; | 490 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; | 491 return false; |
480 } | 492 } |
481 const BookmarkNode* parent = model->GetNodeByID(parentId); | 493 const BookmarkNode* parent = model->GetNodeByID(parentId); |
482 if (!parent) { | 494 if (!parent) { |
483 error_ = keys::kNoParentError; | 495 error_ = keys::kNoParentError; |
484 return false; | 496 return false; |
485 } | 497 } |
486 if (parent->is_root()) { // Can't create children of the root. | 498 if (parent->is_root()) { // Can't create children of the root. |
487 error_ = keys::kModifySpecialError; | 499 error_ = keys::kModifySpecialError; |
488 return false; | 500 return false; |
489 } | 501 } |
490 | 502 |
491 int index; | 503 int index; |
492 if (!json->HasKey(keys::kIndexKey)) { // Optional (defaults to end). | 504 if (!params->bookmark.index.get()) { // Optional (defaults to end). |
493 index = parent->child_count(); | 505 index = parent->child_count(); |
494 } else { | 506 } else { |
495 EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIndexKey, &index)); | 507 index = *params->bookmark.index; |
496 if (index > parent->child_count() || index < 0) { | 508 if (index > parent->child_count() || index < 0) { |
497 error_ = keys::kInvalidIndexError; | 509 error_ = keys::kInvalidIndexError; |
498 return false; | 510 return false; |
499 } | 511 } |
500 } | 512 } |
501 | 513 |
502 string16 title; | 514 string16 title; // Optional. |
503 json->GetString(keys::kTitleKey, &title); // Optional. | 515 if (params->bookmark.title.get()) |
504 std::string url_string; | 516 title = UTF8ToUTF16(*params->bookmark.title.get()); |
505 json->GetString(keys::kUrlKey, &url_string); // Optional. | 517 |
518 std::string url_string; // Optional. | |
519 if (params->bookmark.url.get()) | |
520 url_string = *params->bookmark.url.get(); | |
521 | |
506 GURL url(url_string); | 522 GURL url(url_string); |
507 if (!url.is_empty() && !url.is_valid()) { | 523 if (!url.is_empty() && !url.is_valid()) { |
508 error_ = keys::kInvalidUrlError; | 524 error_ = keys::kInvalidUrlError; |
509 return false; | 525 return false; |
510 } | 526 } |
511 | 527 |
512 const BookmarkNode* node; | 528 const BookmarkNode* node; |
513 if (url_string.length()) | 529 if (url_string.length()) |
514 node = model->AddURL(parent, index, title, url); | 530 node = model->AddURL(parent, index, title, url); |
515 else | 531 else |
516 node = model->AddFolder(parent, index, title); | 532 node = model->AddFolder(parent, index, title); |
517 DCHECK(node); | 533 DCHECK(node); |
518 if (!node) { | 534 if (!node) { |
519 error_ = keys::kNoNodeError; | 535 error_ = keys::kNoNodeError; |
520 return false; | 536 return false; |
521 } | 537 } |
522 | 538 |
523 DictionaryValue* ret = | 539 scoped_ptr<BookmarkTreeNode> ret( |
524 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 540 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
525 SetResult(ret); | 541 results_ = Create::Results::Create(*ret); |
526 | 542 |
527 return true; | 543 return true; |
528 } | 544 } |
529 | 545 |
530 // static | 546 // static |
531 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, | 547 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, |
532 std::list<int64>* ids, | 548 std::list<int64>* ids, |
533 bool* invalid_id) { | 549 bool* invalid_id) { |
534 // For now, Move accepts ID parameters in the same way as an Update. | 550 // For now, Move accepts ID parameters in the same way as an Update. |
535 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); | 551 return UpdateBookmarkFunction::ExtractIds(args, ids, invalid_id); |
536 } | 552 } |
537 | 553 |
538 bool MoveBookmarkFunction::RunImpl() { | 554 bool MoveBookmarkFunction::RunImpl() { |
539 if (!EditBookmarksEnabled()) | 555 if (!EditBookmarksEnabled()) |
540 return false; | 556 return false; |
541 std::list<int64> ids; | 557 |
542 bool invalid_id = false; | 558 scoped_ptr<Move::Params> params(Move::Params::Create(*args_)); |
543 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 559 EXTENSION_FUNCTION_VALIDATE(params.get()); |
544 if (invalid_id) { | 560 |
561 int64 id; | |
562 if (!base::StringToInt64(params->id, &id)) { | |
545 error_ = keys::kInvalidIdError; | 563 error_ = keys::kInvalidIdError; |
546 return false; | 564 return false; |
547 } | 565 } |
548 EXTENSION_FUNCTION_VALIDATE(ids.size() == 1); | |
549 | |
550 DictionaryValue* destination; | |
551 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &destination)); | |
552 | 566 |
553 BookmarkModel* model = profile()->GetBookmarkModel(); | 567 BookmarkModel* model = profile()->GetBookmarkModel(); |
554 const BookmarkNode* node = model->GetNodeByID(ids.front()); | 568 const BookmarkNode* node = model->GetNodeByID(id); |
555 if (!node) { | 569 if (!node) { |
556 error_ = keys::kNoNodeError; | 570 error_ = keys::kNoNodeError; |
557 return false; | 571 return false; |
558 } | 572 } |
559 if (model->is_permanent_node(node)) { | 573 if (model->is_permanent_node(node)) { |
560 error_ = keys::kModifySpecialError; | 574 error_ = keys::kModifySpecialError; |
561 return false; | 575 return false; |
562 } | 576 } |
563 | 577 |
564 const BookmarkNode* parent = NULL; | 578 const BookmarkNode* parent = NULL; |
565 if (!destination->HasKey(keys::kParentIdKey)) { | 579 if (!params->destination.parent_id.get()) { |
566 // Optional, defaults to current parent. | 580 // Optional, defaults to current parent. |
567 parent = node->parent(); | 581 parent = node->parent(); |
568 } else { | 582 } else { |
569 std::string parentId_string; | |
570 EXTENSION_FUNCTION_VALIDATE(destination->GetString(keys::kParentIdKey, | |
571 &parentId_string)); | |
572 int64 parentId; | 583 int64 parentId; |
573 if (!GetBookmarkIdAsInt64(parentId_string, &parentId)) | 584 if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId)) |
574 return false; | 585 return false; |
575 | 586 |
576 parent = model->GetNodeByID(parentId); | 587 parent = model->GetNodeByID(parentId); |
577 } | 588 } |
578 if (!parent) { | 589 if (!parent) { |
579 error_ = keys::kNoParentError; | 590 error_ = keys::kNoParentError; |
580 // TODO(erikkay) return an error message. | 591 // TODO(erikkay) return an error message. |
581 return false; | 592 return false; |
582 } | 593 } |
583 if (parent == model->root_node()) { | 594 if (parent == model->root_node()) { |
584 error_ = keys::kModifySpecialError; | 595 error_ = keys::kModifySpecialError; |
585 return false; | 596 return false; |
586 } | 597 } |
587 | 598 |
588 int index; | 599 int index; |
589 if (destination->HasKey(keys::kIndexKey)) { // Optional (defaults to end). | 600 if (params->destination.index.get()) { // Optional (defaults to end). |
590 EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kIndexKey, | 601 index = *params->destination.index; |
591 &index)); | |
592 if (index > parent->child_count() || index < 0) { | 602 if (index > parent->child_count() || index < 0) { |
593 error_ = keys::kInvalidIndexError; | 603 error_ = keys::kInvalidIndexError; |
594 return false; | 604 return false; |
595 } | 605 } |
596 } else { | 606 } else { |
597 index = parent->child_count(); | 607 index = parent->child_count(); |
598 } | 608 } |
599 | 609 |
600 model->Move(node, parent, index); | 610 model->Move(node, parent, index); |
601 | 611 |
602 DictionaryValue* ret = | 612 scoped_ptr<BookmarkTreeNode> tree_node( |
603 bookmark_extension_helpers::GetNodeDictionary(node, false, false); | 613 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
604 SetResult(ret); | 614 results_ = Move::Results::Create(*tree_node); |
605 | 615 |
606 return true; | 616 return true; |
607 } | 617 } |
608 | 618 |
609 // static | 619 // static |
610 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, | 620 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, |
611 std::list<int64>* ids, | 621 std::list<int64>* ids, |
612 bool* invalid_id) { | 622 bool* invalid_id) { |
613 // For now, Update accepts ID parameters in the same way as an Remove. | 623 // For now, Update accepts ID parameters in the same way as an Remove. |
614 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); | 624 return RemoveBookmarkFunction::ExtractIds(args, ids, invalid_id); |
615 } | 625 } |
616 | 626 |
617 bool UpdateBookmarkFunction::RunImpl() { | 627 bool UpdateBookmarkFunction::RunImpl() { |
618 if (!EditBookmarksEnabled()) | 628 if (!EditBookmarksEnabled()) |
619 return false; | 629 return false; |
620 std::list<int64> ids; | 630 |
621 bool invalid_id = false; | 631 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
622 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 632 EXTENSION_FUNCTION_VALIDATE(params.get()); |
623 if (invalid_id) { | 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_ = 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 |