OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_utils.h" | 5 #include "chrome/browser/bookmarks/bookmark_utils.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/bookmarks/bookmark_model.h" | 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
16 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 16 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
17 #if defined(OS_MACOSX) | |
18 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" | |
19 #endif | |
20 #include "chrome/browser/browser_list.h" | 17 #include "chrome/browser/browser_list.h" |
21 #include "chrome/browser/browser_window.h" | 18 #include "chrome/browser/browser_window.h" |
22 #include "chrome/browser/history/query_parser.h" | 19 #include "chrome/browser/history/query_parser.h" |
23 #include "chrome/browser/platform_util.h" | 20 #include "chrome/browser/platform_util.h" |
24 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
25 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
28 #include "content/browser/tab_contents/page_navigator.h" | 25 #include "content/browser/tab_contents/page_navigator.h" |
29 #include "content/browser/tab_contents/tab_contents.h" | 26 #include "content/browser/tab_contents/tab_contents.h" |
30 #include "content/common/notification_service.h" | 27 #include "content/common/notification_service.h" |
31 #include "grit/app_strings.h" | 28 #include "grit/app_strings.h" |
32 #include "grit/chromium_strings.h" | 29 #include "grit/chromium_strings.h" |
33 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
34 #include "net/base/net_util.h" | 31 #include "net/base/net_util.h" |
35 #include "ui/base/dragdrop/drag_drop_types.h" | 32 #include "ui/base/dragdrop/drag_drop_types.h" |
36 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
37 #include "ui/base/models/tree_node_iterator.h" | 34 #include "ui/base/models/tree_node_iterator.h" |
38 | 35 |
| 36 #if defined(OS_MACOSX) |
| 37 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" |
| 38 #endif |
| 39 |
39 #if defined(TOOLKIT_VIEWS) | 40 #if defined(TOOLKIT_VIEWS) |
40 #include "ui/base/dragdrop/os_exchange_data.h" | 41 #include "ui/base/dragdrop/os_exchange_data.h" |
41 #include "views/drag_utils.h" | 42 #include "views/drag_utils.h" |
42 #include "views/events/event.h" | 43 #include "views/events/event.h" |
43 #include "views/widget/native_widget.h" | 44 #include "views/widget/native_widget.h" |
44 #include "views/widget/widget.h" | 45 #include "views/widget/widget.h" |
45 #elif defined(TOOLKIT_GTK) | 46 #endif |
| 47 |
| 48 #if defined(TOOLKIT_GTK) |
46 #include "chrome/browser/ui/gtk/custom_drag.h" | 49 #include "chrome/browser/ui/gtk/custom_drag.h" |
47 #endif | 50 #endif |
48 | 51 |
49 using base::Time; | 52 using base::Time; |
50 | 53 |
51 namespace { | 54 namespace { |
52 | 55 |
53 // A PageNavigator implementation that creates a new Browser. This is used when | 56 // A PageNavigator implementation that creates a new Browser. This is used when |
54 // opening a url and there is no Browser open. The Browser is created the first | 57 // opening a url and there is no Browser open. The Browser is created the first |
55 // time the PageNavigator method is invoked. | 58 // time the PageNavigator method is invoked. |
56 class NewBrowserPageNavigator : public PageNavigator { | 59 class NewBrowserPageNavigator : public PageNavigator { |
57 public: | 60 public: |
58 explicit NewBrowserPageNavigator(Profile* profile) | 61 explicit NewBrowserPageNavigator(Profile* profile) |
59 : profile_(profile), | 62 : profile_(profile), |
60 browser_(NULL) {} | 63 browser_(NULL) {} |
61 | 64 |
62 virtual ~NewBrowserPageNavigator() { | 65 virtual ~NewBrowserPageNavigator() { |
63 if (browser_) | 66 if (browser_) |
64 browser_->window()->Show(); | 67 browser_->window()->Show(); |
65 } | 68 } |
66 | 69 |
67 Browser* browser() const { return browser_; } | 70 Browser* browser() const { return browser_; } |
68 | 71 |
69 virtual void OpenURL(const GURL& url, | 72 virtual void OpenURL(const GURL& url, |
70 const GURL& referrer, | 73 const GURL& referrer, |
71 WindowOpenDisposition disposition, | 74 WindowOpenDisposition disposition, |
72 PageTransition::Type transition) { | 75 PageTransition::Type transition) OVERRIDE { |
73 if (!browser_) { | 76 if (!browser_) { |
74 Profile* profile = (disposition == OFF_THE_RECORD) ? | 77 Profile* profile = (disposition == OFF_THE_RECORD) ? |
75 profile_->GetOffTheRecordProfile() : profile_; | 78 profile_->GetOffTheRecordProfile() : profile_; |
76 browser_ = Browser::Create(profile); | 79 browser_ = Browser::Create(profile); |
77 // Always open the first tab in the foreground. | 80 // Always open the first tab in the foreground. |
78 disposition = NEW_FOREGROUND_TAB; | 81 disposition = NEW_FOREGROUND_TAB; |
79 } | 82 } |
80 browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, transition); | 83 browser_->OpenURL(url, referrer, NEW_FOREGROUND_TAB, transition); |
81 } | 84 } |
82 | 85 |
(...skipping 12 matching lines...) Expand all Loading... |
95 model->AddURL(parent, index_to_add_at, element.title, element.url); | 98 model->AddURL(parent, index_to_add_at, element.title, element.url); |
96 } else { | 99 } else { |
97 const BookmarkNode* new_folder = model->AddFolder(parent, | 100 const BookmarkNode* new_folder = model->AddFolder(parent, |
98 index_to_add_at, | 101 index_to_add_at, |
99 element.title); | 102 element.title); |
100 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | 103 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) |
101 CloneBookmarkNodeImpl(model, element.children[i], new_folder, i); | 104 CloneBookmarkNodeImpl(model, element.children[i], new_folder, i); |
102 } | 105 } |
103 } | 106 } |
104 | 107 |
105 // Returns the number of descendants of node that are of type url. | 108 // Returns the number of children of |node| that are of type url. |
106 int DescendantURLCount(const BookmarkNode* node) { | 109 int ChildURLCount(const BookmarkNode* node) { |
107 int result = 0; | 110 int result = 0; |
108 for (int i = 0; i < node->child_count(); ++i) { | 111 for (int i = 0; i < node->child_count(); ++i) { |
109 const BookmarkNode* child = node->GetChild(i); | 112 const BookmarkNode* child = node->GetChild(i); |
110 if (child->is_url()) | 113 if (child->is_url()) |
111 result++; | 114 result++; |
112 else | |
113 result += DescendantURLCount(child); | |
114 } | 115 } |
115 return result; | 116 return result; |
116 } | 117 } |
117 | 118 |
118 // Implementation of OpenAll. Opens all nodes of type URL and recurses for | 119 // Implementation of OpenAll. Opens all nodes of type URL and any children of |
119 // folders. |navigator| is the PageNavigator used to open URLs. After the first | 120 // |node| that are of type URL. |navigator| is the PageNavigator used to open |
120 // url is opened |opened_url| is set to true and |navigator| is set to the | 121 // URLs. After the first url is opened |opened_url| is set to true and |
121 // PageNavigator of the last active tab. This is done to handle a window | 122 // |navigator| is set to the PageNavigator of the last active tab. This is done |
122 // disposition of new window, in which case we want subsequent tabs to open in | 123 // to handle a window disposition of new window, in which case we want |
123 // that window. | 124 // subsequent tabs to open in that window. |
124 void OpenAllImpl(const BookmarkNode* node, | 125 void OpenAllImpl(const BookmarkNode* node, |
125 WindowOpenDisposition initial_disposition, | 126 WindowOpenDisposition initial_disposition, |
126 PageNavigator** navigator, | 127 PageNavigator** navigator, |
127 bool* opened_url) { | 128 bool* opened_url) { |
128 if (node->is_url()) { | 129 if (node->is_url()) { |
129 WindowOpenDisposition disposition; | 130 WindowOpenDisposition disposition; |
130 if (*opened_url) | 131 if (*opened_url) |
131 disposition = NEW_BACKGROUND_TAB; | 132 disposition = NEW_BACKGROUND_TAB; |
132 else | 133 else |
133 disposition = initial_disposition; | 134 disposition = initial_disposition; |
134 (*navigator)->OpenURL(node->GetURL(), GURL(), disposition, | 135 (*navigator)->OpenURL(node->GetURL(), GURL(), disposition, |
135 PageTransition::AUTO_BOOKMARK); | 136 PageTransition::AUTO_BOOKMARK); |
136 if (!*opened_url) { | 137 if (!*opened_url) { |
137 *opened_url = true; | 138 *opened_url = true; |
138 // We opened the first URL which may have opened a new window or clobbered | 139 // We opened the first URL which may have opened a new window or clobbered |
139 // the current page, reset the navigator just to be sure. | 140 // the current page, reset the navigator just to be sure. |
140 Browser* new_browser = BrowserList::GetLastActive(); | 141 Browser* new_browser = BrowserList::GetLastActive(); |
141 if (new_browser) { | 142 if (new_browser) { |
142 TabContents* current_tab = new_browser->GetSelectedTabContents(); | 143 TabContents* current_tab = new_browser->GetSelectedTabContents(); |
143 DCHECK(new_browser && current_tab); | 144 DCHECK(new_browser && current_tab); |
144 if (new_browser && current_tab) | 145 if (new_browser && current_tab) |
145 *navigator = current_tab; | 146 *navigator = current_tab; |
146 } // else, new_browser == NULL, which happens during testing. | 147 } // else, new_browser == NULL, which happens during testing. |
147 } | 148 } |
148 } else { | 149 } else { |
149 // Folder, recurse through children. | 150 // For folders only open direct children. |
150 for (int i = 0; i < node->child_count(); ++i) { | 151 for (int i = 0; i < node->child_count(); ++i) { |
151 OpenAllImpl(node->GetChild(i), initial_disposition, navigator, | 152 const BookmarkNode* child_node = node->GetChild(i); |
152 opened_url); | 153 if (child_node->is_url()) |
| 154 OpenAllImpl(child_node, initial_disposition, navigator, opened_url); |
153 } | 155 } |
154 } | 156 } |
155 } | 157 } |
156 | 158 |
157 bool ShouldOpenAll(gfx::NativeWindow parent, | 159 bool ShouldOpenAll(gfx::NativeWindow parent, |
158 const std::vector<const BookmarkNode*>& nodes) { | 160 const std::vector<const BookmarkNode*>& nodes) { |
159 int descendant_count = 0; | 161 int child_count = 0; |
160 for (size_t i = 0; i < nodes.size(); ++i) | 162 for (size_t i = 0; i < nodes.size(); ++i) |
161 descendant_count += DescendantURLCount(nodes[i]); | 163 child_count += ChildURLCount(nodes[i]); |
162 if (descendant_count < bookmark_utils::num_urls_before_prompting) | 164 if (child_count < bookmark_utils::num_urls_before_prompting) |
163 return true; | 165 return true; |
164 | 166 |
165 string16 message = l10n_util::GetStringFUTF16( | 167 string16 message = l10n_util::GetStringFUTF16( |
166 IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL, | 168 IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL, |
167 base::IntToString16(descendant_count)); | 169 base::IntToString16(child_count)); |
168 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | 170 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
169 return platform_util::SimpleYesNoBox(parent, title, message); | 171 return platform_util::SimpleYesNoBox(parent, title, message); |
170 } | 172 } |
171 | 173 |
172 // Comparison function that compares based on date modified of the two nodes. | 174 // Comparison function that compares based on date modified of the two nodes. |
173 bool MoreRecentlyModified(const BookmarkNode* n1, const BookmarkNode* n2) { | 175 bool MoreRecentlyModified(const BookmarkNode* n1, const BookmarkNode* n2) { |
174 return n1->date_folder_modified() > n2->date_folder_modified(); | 176 return n1->date_folder_modified() > n2->date_folder_modified(); |
175 } | 177 } |
176 | 178 |
177 // Returns true if |text| contains each string in |words|. This is used when | 179 // Returns true if |text| contains each string in |words|. This is used when |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 return true; | 673 return true; |
672 | 674 |
673 for (int i = 0; i < node->child_count(); ++i) { | 675 for (int i = 0; i < node->child_count(); ++i) { |
674 if (NodeHasURLs(node->GetChild(i))) | 676 if (NodeHasURLs(node->GetChild(i))) |
675 return true; | 677 return true; |
676 } | 678 } |
677 return false; | 679 return false; |
678 } | 680 } |
679 | 681 |
680 } // namespace bookmark_utils | 682 } // namespace bookmark_utils |
OLD | NEW |