| 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/bookmark_manager_private/bookmark_manage
r_private_api.h" | 5 #include "chrome/browser/extensions/api/bookmark_manager_private/bookmark_manage
r_private_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/prefs/public/pref_service_base.h" | 10 #include "base/prefs/public/pref_service_base.h" |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 233 |
| 234 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut) { | 234 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut) { |
| 235 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 235 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 236 std::vector<const BookmarkNode*> nodes; | 236 std::vector<const BookmarkNode*> nodes; |
| 237 EXTENSION_FUNCTION_VALIDATE(GetNodesFromArguments(model, args_.get(), | 237 EXTENSION_FUNCTION_VALIDATE(GetNodesFromArguments(model, args_.get(), |
| 238 0, &nodes)); | 238 0, &nodes)); |
| 239 bookmark_utils::CopyToClipboard(model, nodes, cut); | 239 bookmark_utils::CopyToClipboard(model, nodes, cut); |
| 240 return true; | 240 return true; |
| 241 } | 241 } |
| 242 | 242 |
| 243 bool CopyBookmarkManagerFunction::RunImpl() { | 243 bool BookmarkManagerPrivateCopyFunction::RunImpl() { |
| 244 return CopyOrCut(false); | 244 return CopyOrCut(false); |
| 245 } | 245 } |
| 246 | 246 |
| 247 bool CutBookmarkManagerFunction::RunImpl() { | 247 bool BookmarkManagerPrivateCutFunction::RunImpl() { |
| 248 if (!EditBookmarksEnabled()) | 248 if (!EditBookmarksEnabled()) |
| 249 return false; | 249 return false; |
| 250 return CopyOrCut(true); | 250 return CopyOrCut(true); |
| 251 } | 251 } |
| 252 | 252 |
| 253 bool PasteBookmarkManagerFunction::RunImpl() { | 253 bool BookmarkManagerPrivatePasteFunction::RunImpl() { |
| 254 if (!EditBookmarksEnabled()) | 254 if (!EditBookmarksEnabled()) |
| 255 return false; | 255 return false; |
| 256 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 256 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 257 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 257 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
| 258 if (!parent_node) { | 258 if (!parent_node) { |
| 259 error_ = bookmark_keys::kNoParentError; | 259 error_ = bookmark_keys::kNoParentError; |
| 260 return false; | 260 return false; |
| 261 } | 261 } |
| 262 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 262 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); |
| 263 if (!can_paste) | 263 if (!can_paste) |
| 264 return false; | 264 return false; |
| 265 | 265 |
| 266 // We want to use the highest index of the selected nodes as a destination. | 266 // We want to use the highest index of the selected nodes as a destination. |
| 267 std::vector<const BookmarkNode*> nodes; | 267 std::vector<const BookmarkNode*> nodes; |
| 268 // No need to test return value, if we got an empty list, we insert at end. | 268 // No need to test return value, if we got an empty list, we insert at end. |
| 269 GetNodesFromArguments(model, args_.get(), 1, &nodes); | 269 GetNodesFromArguments(model, args_.get(), 1, &nodes); |
| 270 int highest_index = -1; // -1 means insert at end of list. | 270 int highest_index = -1; // -1 means insert at end of list. |
| 271 for (size_t node = 0; node < nodes.size(); ++node) { | 271 for (size_t node = 0; node < nodes.size(); ++node) { |
| 272 // + 1 so that we insert after the selection. | 272 // + 1 so that we insert after the selection. |
| 273 int this_node_index = parent_node->GetIndexOf(nodes[node]) + 1; | 273 int this_node_index = parent_node->GetIndexOf(nodes[node]) + 1; |
| 274 if (this_node_index > highest_index) | 274 if (this_node_index > highest_index) |
| 275 highest_index = this_node_index; | 275 highest_index = this_node_index; |
| 276 } | 276 } |
| 277 | 277 |
| 278 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); | 278 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); |
| 279 return true; | 279 return true; |
| 280 } | 280 } |
| 281 | 281 |
| 282 bool CanPasteBookmarkManagerFunction::RunImpl() { | 282 bool BookmarkManagerPrivateCanPasteFunction::RunImpl() { |
| 283 if (!EditBookmarksEnabled()) | 283 if (!EditBookmarksEnabled()) |
| 284 return false; | 284 return false; |
| 285 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 285 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 286 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 286 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
| 287 if (!parent_node) { | 287 if (!parent_node) { |
| 288 error_ = bookmark_keys::kNoParentError; | 288 error_ = bookmark_keys::kNoParentError; |
| 289 return false; | 289 return false; |
| 290 } | 290 } |
| 291 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 291 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); |
| 292 SetResult(new base::FundamentalValue(can_paste)); | 292 SetResult(new base::FundamentalValue(can_paste)); |
| 293 return true; | 293 return true; |
| 294 } | 294 } |
| 295 | 295 |
| 296 bool SortChildrenBookmarkManagerFunction::RunImpl() { | 296 bool BookmarkManagerPrivateSortChildrenFunction::RunImpl() { |
| 297 if (!EditBookmarksEnabled()) | 297 if (!EditBookmarksEnabled()) |
| 298 return false; | 298 return false; |
| 299 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 299 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 300 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 300 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
| 301 if (!parent_node) { | 301 if (!parent_node) { |
| 302 error_ = bookmark_keys::kNoParentError; | 302 error_ = bookmark_keys::kNoParentError; |
| 303 return false; | 303 return false; |
| 304 } | 304 } |
| 305 model->SortChildren(parent_node); | 305 model->SortChildren(parent_node); |
| 306 return true; | 306 return true; |
| 307 } | 307 } |
| 308 | 308 |
| 309 bool BookmarkManagerGetStringsFunction::RunImpl() { | 309 bool BookmarkManagerPrivateGetStringsFunction::RunImpl() { |
| 310 DictionaryValue* localized_strings = new DictionaryValue(); | 310 DictionaryValue* localized_strings = new DictionaryValue(); |
| 311 | 311 |
| 312 localized_strings->SetString("title", | 312 localized_strings->SetString("title", |
| 313 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_TITLE)); | 313 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_TITLE)); |
| 314 localized_strings->SetString("search_button", | 314 localized_strings->SetString("search_button", |
| 315 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SEARCH_BUTTON)); | 315 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SEARCH_BUTTON)); |
| 316 localized_strings->SetString("show_in_folder", | 316 localized_strings->SetString("show_in_folder", |
| 317 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER)); | 317 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER)); |
| 318 localized_strings->SetString("sort", | 318 localized_strings->SetString("sort", |
| 319 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SORT)); | 319 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_SORT)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 | 382 |
| 383 SetResult(localized_strings); | 383 SetResult(localized_strings); |
| 384 | 384 |
| 385 // This is needed because unlike the rest of these functions, this class | 385 // This is needed because unlike the rest of these functions, this class |
| 386 // inherits from AsyncFunction directly, rather than BookmarkFunction. | 386 // inherits from AsyncFunction directly, rather than BookmarkFunction. |
| 387 SendResponse(true); | 387 SendResponse(true); |
| 388 | 388 |
| 389 return true; | 389 return true; |
| 390 } | 390 } |
| 391 | 391 |
| 392 bool StartDragBookmarkManagerFunction::RunImpl() { | 392 bool BookmarkManagerPrivateStartDragFunction::RunImpl() { |
| 393 if (!EditBookmarksEnabled()) | 393 if (!EditBookmarksEnabled()) |
| 394 return false; | 394 return false; |
| 395 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 395 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 396 std::vector<const BookmarkNode*> nodes; | 396 std::vector<const BookmarkNode*> nodes; |
| 397 EXTENSION_FUNCTION_VALIDATE( | 397 EXTENSION_FUNCTION_VALIDATE( |
| 398 GetNodesFromArguments(model, args_.get(), 0, &nodes)); | 398 GetNodesFromArguments(model, args_.get(), 0, &nodes)); |
| 399 | 399 |
| 400 WebContents* web_contents = | 400 WebContents* web_contents = |
| 401 WebContents::FromRenderViewHost(render_view_host_); | 401 WebContents::FromRenderViewHost(render_view_host_); |
| 402 if (chrome::GetViewType(web_contents) == chrome::VIEW_TYPE_TAB_CONTENTS) { | 402 if (chrome::GetViewType(web_contents) == chrome::VIEW_TYPE_TAB_CONTENTS) { |
| 403 WebContents* web_contents = | 403 WebContents* web_contents = |
| 404 dispatcher()->delegate()->GetAssociatedWebContents(); | 404 dispatcher()->delegate()->GetAssociatedWebContents(); |
| 405 CHECK(web_contents); | 405 CHECK(web_contents); |
| 406 bookmark_utils::DragBookmarks(profile(), nodes, | 406 bookmark_utils::DragBookmarks(profile(), nodes, |
| 407 web_contents->GetNativeView()); | 407 web_contents->GetNativeView()); |
| 408 | 408 |
| 409 return true; | 409 return true; |
| 410 } else { | 410 } else { |
| 411 NOTREACHED(); | 411 NOTREACHED(); |
| 412 return false; | 412 return false; |
| 413 } | 413 } |
| 414 } | 414 } |
| 415 | 415 |
| 416 bool DropBookmarkManagerFunction::RunImpl() { | 416 bool BookmarkManagerPrivateDropFunction::RunImpl() { |
| 417 if (!EditBookmarksEnabled()) | 417 if (!EditBookmarksEnabled()) |
| 418 return false; | 418 return false; |
| 419 | 419 |
| 420 BookmarkModel* model =BookmarkModelFactory::GetForProfile(profile()); | 420 BookmarkModel* model =BookmarkModelFactory::GetForProfile(profile()); |
| 421 | 421 |
| 422 int64 id; | 422 int64 id; |
| 423 std::string id_string; | 423 std::string id_string; |
| 424 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | 424 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); |
| 425 | 425 |
| 426 if (!base::StringToInt64(id_string, &id)) { | 426 if (!base::StringToInt64(id_string, &id)) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 drop_parent, drop_index); | 463 drop_parent, drop_index); |
| 464 | 464 |
| 465 router->ClearBookmarkNodeData(); | 465 router->ClearBookmarkNodeData(); |
| 466 return true; | 466 return true; |
| 467 } else { | 467 } else { |
| 468 NOTREACHED(); | 468 NOTREACHED(); |
| 469 return false; | 469 return false; |
| 470 } | 470 } |
| 471 } | 471 } |
| 472 | 472 |
| 473 bool GetSubtreeBookmarkManagerFunction::RunImpl() { | 473 bool BookmarkManagerPrivateGetSubtreeFunction::RunImpl() { |
| 474 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 474 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 475 const BookmarkNode* node; | 475 const BookmarkNode* node; |
| 476 int64 id; | 476 int64 id; |
| 477 std::string id_string; | 477 std::string id_string; |
| 478 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | 478 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); |
| 479 bool folders_only; | 479 bool folders_only; |
| 480 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &folders_only)); | 480 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &folders_only)); |
| 481 if (id_string == "") { | 481 if (id_string == "") { |
| 482 node = model->root_node(); | 482 node = model->root_node(); |
| 483 } else { | 483 } else { |
| 484 if (!base::StringToInt64(id_string, &id)) { | 484 if (!base::StringToInt64(id_string, &id)) { |
| 485 error_ = bookmark_keys::kInvalidIdError; | 485 error_ = bookmark_keys::kInvalidIdError; |
| 486 return false; | 486 return false; |
| 487 } | 487 } |
| 488 node = model->GetNodeByID(id); | 488 node = model->GetNodeByID(id); |
| 489 } | 489 } |
| 490 if (!node) { | 490 if (!node) { |
| 491 error_ = bookmark_keys::kNoNodeError; | 491 error_ = bookmark_keys::kNoNodeError; |
| 492 return false; | 492 return false; |
| 493 } | 493 } |
| 494 scoped_ptr<ListValue> json(new ListValue()); | 494 scoped_ptr<ListValue> json(new ListValue()); |
| 495 if (folders_only) | 495 if (folders_only) |
| 496 bookmark_api_helpers::AddNodeFoldersOnly(node, json.get(), true); | 496 bookmark_api_helpers::AddNodeFoldersOnly(node, json.get(), true); |
| 497 else | 497 else |
| 498 bookmark_api_helpers::AddNode(node, json.get(), true); | 498 bookmark_api_helpers::AddNode(node, json.get(), true); |
| 499 SetResult(json.release()); | 499 SetResult(json.release()); |
| 500 return true; | 500 return true; |
| 501 } | 501 } |
| 502 | 502 |
| 503 bool CanEditBookmarkManagerFunction::RunImpl() { | 503 bool BookmarkManagerPrivateCanEditFunction::RunImpl() { |
| 504 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile_); | 504 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile_); |
| 505 SetResult(new base::FundamentalValue( | 505 SetResult(new base::FundamentalValue( |
| 506 prefs->GetBoolean(prefs::kEditBookmarksEnabled))); | 506 prefs->GetBoolean(prefs::kEditBookmarksEnabled))); |
| 507 return true; | 507 return true; |
| 508 } | 508 } |
| 509 | 509 |
| 510 bool RecordLaunchBookmarkFunction::RunImpl() { | 510 bool RecordLaunchBookmarkFunction::RunImpl() { |
| 511 bookmark_utils::RecordBookmarkLaunch(bookmark_utils::LAUNCH_MANAGER); | 511 bookmark_utils::RecordBookmarkLaunch(bookmark_utils::LAUNCH_MANAGER); |
| 512 return true; | 512 return true; |
| 513 } | 513 } |
| 514 | 514 |
| 515 bool CanOpenNewWindowsBookmarkFunction::RunImpl() { | 515 bool BookmarkManagerPrivateCanOpenNewWindowsFunction::RunImpl() { |
| 516 bool can_open_new_windows = true; | 516 bool can_open_new_windows = true; |
| 517 | 517 |
| 518 #if defined(OS_WIN) | 518 #if defined(OS_WIN) |
| 519 if (win8::IsSingleWindowMetroMode()) | 519 if (win8::IsSingleWindowMetroMode()) |
| 520 can_open_new_windows = false; | 520 can_open_new_windows = false; |
| 521 #endif // OS_WIN | 521 #endif // OS_WIN |
| 522 | 522 |
| 523 SetResult(new base::FundamentalValue(can_open_new_windows)); | 523 SetResult(new base::FundamentalValue(can_open_new_windows)); |
| 524 return true; | 524 return true; |
| 525 } | 525 } |
| 526 | 526 |
| 527 } // namespace extensions | 527 } // namespace extensions |
| OLD | NEW |