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_manager_extension_api.h" | 5 #include "chrome/browser/bookmarks/bookmark_manager_extension_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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/bookmarks/bookmark_extension_api_constants.h" | 12 #include "chrome/browser/bookmarks/bookmark_extension_api_constants.h" |
13 #include "chrome/browser/bookmarks/bookmark_extension_helpers.h" | 13 #include "chrome/browser/bookmarks/bookmark_extension_helpers.h" |
14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
15 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 16 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
16 #include "chrome/browser/bookmarks/bookmark_utils.h" | 17 #include "chrome/browser/bookmarks/bookmark_utils.h" |
17 #include "chrome/browser/extensions/event_router.h" | 18 #include "chrome/browser/extensions/event_router.h" |
18 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 19 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
19 #include "chrome/browser/extensions/extension_web_ui.h" | 20 #include "chrome/browser/extensions/extension_web_ui.h" |
20 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
23 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
24 #include "chrome/browser/view_type_utils.h" | 25 #include "chrome/browser/view_type_utils.h" |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (bookmark_drag_data_.is_valid()) | 217 if (bookmark_drag_data_.is_valid()) |
217 return &bookmark_drag_data_; | 218 return &bookmark_drag_data_; |
218 return NULL; | 219 return NULL; |
219 } | 220 } |
220 | 221 |
221 void BookmarkManagerExtensionEventRouter::ClearBookmarkNodeData() { | 222 void BookmarkManagerExtensionEventRouter::ClearBookmarkNodeData() { |
222 bookmark_drag_data_.Clear(); | 223 bookmark_drag_data_.Clear(); |
223 } | 224 } |
224 | 225 |
225 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut) { | 226 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut) { |
226 BookmarkModel* model = profile()->GetBookmarkModel(); | 227 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
227 std::vector<const BookmarkNode*> nodes; | 228 std::vector<const BookmarkNode*> nodes; |
228 EXTENSION_FUNCTION_VALIDATE(GetNodesFromArguments(model, args_.get(), | 229 EXTENSION_FUNCTION_VALIDATE(GetNodesFromArguments(model, args_.get(), |
229 0, &nodes)); | 230 0, &nodes)); |
230 bookmark_utils::CopyToClipboard(model, nodes, cut); | 231 bookmark_utils::CopyToClipboard(model, nodes, cut); |
231 return true; | 232 return true; |
232 } | 233 } |
233 | 234 |
234 bool CopyBookmarkManagerFunction::RunImpl() { | 235 bool CopyBookmarkManagerFunction::RunImpl() { |
235 return CopyOrCut(false); | 236 return CopyOrCut(false); |
236 } | 237 } |
237 | 238 |
238 bool CutBookmarkManagerFunction::RunImpl() { | 239 bool CutBookmarkManagerFunction::RunImpl() { |
239 if (!EditBookmarksEnabled()) | 240 if (!EditBookmarksEnabled()) |
240 return false; | 241 return false; |
241 return CopyOrCut(true); | 242 return CopyOrCut(true); |
242 } | 243 } |
243 | 244 |
244 bool PasteBookmarkManagerFunction::RunImpl() { | 245 bool PasteBookmarkManagerFunction::RunImpl() { |
245 if (!EditBookmarksEnabled()) | 246 if (!EditBookmarksEnabled()) |
246 return false; | 247 return false; |
247 BookmarkModel* model = profile()->GetBookmarkModel(); | 248 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
248 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 249 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
249 if (!parent_node) { | 250 if (!parent_node) { |
250 error_ = keys::kNoParentError; | 251 error_ = keys::kNoParentError; |
251 return false; | 252 return false; |
252 } | 253 } |
253 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 254 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); |
254 if (!can_paste) | 255 if (!can_paste) |
255 return false; | 256 return false; |
256 | 257 |
257 // We want to use the highest index of the selected nodes as a destination. | 258 // We want to use the highest index of the selected nodes as a destination. |
258 std::vector<const BookmarkNode*> nodes; | 259 std::vector<const BookmarkNode*> nodes; |
259 // No need to test return value, if we got an empty list, we insert at end. | 260 // No need to test return value, if we got an empty list, we insert at end. |
260 GetNodesFromArguments(model, args_.get(), 1, &nodes); | 261 GetNodesFromArguments(model, args_.get(), 1, &nodes); |
261 int highest_index = -1; // -1 means insert at end of list. | 262 int highest_index = -1; // -1 means insert at end of list. |
262 for (size_t node = 0; node < nodes.size(); ++node) { | 263 for (size_t node = 0; node < nodes.size(); ++node) { |
263 // + 1 so that we insert after the selection. | 264 // + 1 so that we insert after the selection. |
264 int this_node_index = parent_node->GetIndexOf(nodes[node]) + 1; | 265 int this_node_index = parent_node->GetIndexOf(nodes[node]) + 1; |
265 if (this_node_index > highest_index) | 266 if (this_node_index > highest_index) |
266 highest_index = this_node_index; | 267 highest_index = this_node_index; |
267 } | 268 } |
268 | 269 |
269 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); | 270 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); |
270 return true; | 271 return true; |
271 } | 272 } |
272 | 273 |
273 bool CanPasteBookmarkManagerFunction::RunImpl() { | 274 bool CanPasteBookmarkManagerFunction::RunImpl() { |
274 if (!EditBookmarksEnabled()) | 275 if (!EditBookmarksEnabled()) |
275 return false; | 276 return false; |
276 BookmarkModel* model = profile()->GetBookmarkModel(); | 277 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
277 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 278 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
278 if (!parent_node) { | 279 if (!parent_node) { |
279 error_ = keys::kNoParentError; | 280 error_ = keys::kNoParentError; |
280 return false; | 281 return false; |
281 } | 282 } |
282 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 283 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); |
283 SetResult(Value::CreateBooleanValue(can_paste)); | 284 SetResult(Value::CreateBooleanValue(can_paste)); |
284 return true; | 285 return true; |
285 } | 286 } |
286 | 287 |
287 bool SortChildrenBookmarkManagerFunction::RunImpl() { | 288 bool SortChildrenBookmarkManagerFunction::RunImpl() { |
288 if (!EditBookmarksEnabled()) | 289 if (!EditBookmarksEnabled()) |
289 return false; | 290 return false; |
290 BookmarkModel* model = profile()->GetBookmarkModel(); | 291 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
291 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); | 292 const BookmarkNode* parent_node = GetNodeFromArguments(model, args_.get()); |
292 if (!parent_node) { | 293 if (!parent_node) { |
293 error_ = keys::kNoParentError; | 294 error_ = keys::kNoParentError; |
294 return false; | 295 return false; |
295 } | 296 } |
296 model->SortChildren(parent_node); | 297 model->SortChildren(parent_node); |
297 return true; | 298 return true; |
298 } | 299 } |
299 | 300 |
300 bool BookmarkManagerGetStringsFunction::RunImpl() { | 301 bool BookmarkManagerGetStringsFunction::RunImpl() { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 // This is needed because unlike the rest of these functions, this class | 377 // This is needed because unlike the rest of these functions, this class |
377 // inherits from AsyncFunction directly, rather than BookmarkFunction. | 378 // inherits from AsyncFunction directly, rather than BookmarkFunction. |
378 SendResponse(true); | 379 SendResponse(true); |
379 | 380 |
380 return true; | 381 return true; |
381 } | 382 } |
382 | 383 |
383 bool StartDragBookmarkManagerFunction::RunImpl() { | 384 bool StartDragBookmarkManagerFunction::RunImpl() { |
384 if (!EditBookmarksEnabled()) | 385 if (!EditBookmarksEnabled()) |
385 return false; | 386 return false; |
386 BookmarkModel* model = profile()->GetBookmarkModel(); | 387 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
387 std::vector<const BookmarkNode*> nodes; | 388 std::vector<const BookmarkNode*> nodes; |
388 EXTENSION_FUNCTION_VALIDATE( | 389 EXTENSION_FUNCTION_VALIDATE( |
389 GetNodesFromArguments(model, args_.get(), 0, &nodes)); | 390 GetNodesFromArguments(model, args_.get(), 0, &nodes)); |
390 | 391 |
391 WebContents* web_contents = | 392 WebContents* web_contents = |
392 WebContents::FromRenderViewHost(render_view_host_); | 393 WebContents::FromRenderViewHost(render_view_host_); |
393 if (chrome::GetViewType(web_contents) == chrome::VIEW_TYPE_TAB_CONTENTS) { | 394 if (chrome::GetViewType(web_contents) == chrome::VIEW_TYPE_TAB_CONTENTS) { |
394 WebContents* web_contents = | 395 WebContents* web_contents = |
395 dispatcher()->delegate()->GetAssociatedWebContents(); | 396 dispatcher()->delegate()->GetAssociatedWebContents(); |
396 CHECK(web_contents); | 397 CHECK(web_contents); |
397 bookmark_utils::DragBookmarks(profile(), nodes, | 398 bookmark_utils::DragBookmarks(profile(), nodes, |
398 web_contents->GetNativeView()); | 399 web_contents->GetNativeView()); |
399 | 400 |
400 return true; | 401 return true; |
401 } else { | 402 } else { |
402 NOTREACHED(); | 403 NOTREACHED(); |
403 return false; | 404 return false; |
404 } | 405 } |
405 } | 406 } |
406 | 407 |
407 bool DropBookmarkManagerFunction::RunImpl() { | 408 bool DropBookmarkManagerFunction::RunImpl() { |
408 if (!EditBookmarksEnabled()) | 409 if (!EditBookmarksEnabled()) |
409 return false; | 410 return false; |
410 | 411 |
411 BookmarkModel* model = profile()->GetBookmarkModel(); | 412 BookmarkModel* model =BookmarkModelFactory::GetForProfile(profile()); |
412 | 413 |
413 int64 id; | 414 int64 id; |
414 std::string id_string; | 415 std::string id_string; |
415 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | 416 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); |
416 | 417 |
417 if (!base::StringToInt64(id_string, &id)) { | 418 if (!base::StringToInt64(id_string, &id)) { |
418 error_ = keys::kInvalidIdError; | 419 error_ = keys::kInvalidIdError; |
419 return false; | 420 return false; |
420 } | 421 } |
421 | 422 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 | 456 |
456 router->ClearBookmarkNodeData(); | 457 router->ClearBookmarkNodeData(); |
457 return true; | 458 return true; |
458 } else { | 459 } else { |
459 NOTREACHED(); | 460 NOTREACHED(); |
460 return false; | 461 return false; |
461 } | 462 } |
462 } | 463 } |
463 | 464 |
464 bool GetSubtreeBookmarkManagerFunction::RunImpl() { | 465 bool GetSubtreeBookmarkManagerFunction::RunImpl() { |
465 BookmarkModel* model = profile()->GetBookmarkModel(); | 466 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
466 const BookmarkNode* node; | 467 const BookmarkNode* node; |
467 int64 id; | 468 int64 id; |
468 std::string id_string; | 469 std::string id_string; |
469 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); | 470 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); |
470 bool folders_only; | 471 bool folders_only; |
471 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &folders_only)); | 472 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &folders_only)); |
472 if (id_string == "") { | 473 if (id_string == "") { |
473 node = model->root_node(); | 474 node = model->root_node(); |
474 } else { | 475 } else { |
475 if (!base::StringToInt64(id_string, &id)) { | 476 if (!base::StringToInt64(id_string, &id)) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 bool can_open_new_windows = true; | 510 bool can_open_new_windows = true; |
510 | 511 |
511 #if defined(OS_WIN) | 512 #if defined(OS_WIN) |
512 if (base::win::IsMetroProcess()) | 513 if (base::win::IsMetroProcess()) |
513 can_open_new_windows = false; | 514 can_open_new_windows = false; |
514 #endif // OS_WIN | 515 #endif // OS_WIN |
515 | 516 |
516 SetResult(Value::CreateBooleanValue(can_open_new_windows)); | 517 SetResult(Value::CreateBooleanValue(can_open_new_windows)); |
517 return true; | 518 return true; |
518 } | 519 } |
OLD | NEW |