OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/search/other_device_menu.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "base/string16.h" | |
9 #include "chrome/browser/sync/glue/session_model_associator.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_finder.h" | |
12 #include "chrome/browser/ui/browser_window.h" | |
13 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "content/public/browser/web_ui.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/base/text/text_elider.h" | |
19 #include "ui/views/controls/menu/menu_model_adapter.h" | |
20 #include "ui/views/controls/menu/menu_runner.h" | |
21 #include "ui/views/widget/widget.h" | |
22 #include "webkit/glue/window_open_disposition.h" | |
23 | |
24 namespace { | |
25 | |
26 // The max number of tabs that will be added to the menu. | |
27 const size_t kMaxTabsToShow = 18; | |
28 | |
29 // The max width of a menu. Menu text exceeding this will be elided. | |
30 const int kMaxWidth = 375; | |
31 | |
32 // Enumerates the different menu item types. | |
33 enum ItemType { | |
34 // An item for restoring a single tab. | |
35 TAB, | |
36 // An item for restoring all tabs in this session. | |
37 OPEN_ALL, | |
38 // Number of enum entries, used for UMA histogram reporting macros. | |
39 ITEM_TYPE_ENUM_COUNT, | |
40 }; | |
41 | |
42 // Helper function that returns the largest tab timestamp for the window. | |
43 double GetMostRecentTabTimestamp(const SessionWindow* window) { | |
44 double max_timestamp = 0; | |
45 for (size_t i = 0, num_tabs = window->tabs.size(); i < num_tabs; ++i) { | |
46 linked_ptr<DictionaryValue> tab_value = | |
47 linked_ptr<DictionaryValue>(new DictionaryValue()); | |
48 if (browser_sync::ForeignSessionHandler::SessionTabToValue( | |
49 *window->tabs[i], tab_value.get())) { | |
50 double timestamp; | |
51 tab_value->GetDouble("timestamp", ×tamp); | |
52 if (timestamp > max_timestamp) | |
53 max_timestamp = timestamp; | |
54 } | |
55 } | |
56 return max_timestamp; | |
57 } | |
58 | |
59 // Comparator function to sort windows by last-modified time. Windows in a | |
60 // session share the same timestamp, so we need to use tab timestamps instead. | |
61 // instead. | |
62 bool SortWindowsByRecency(const SessionWindow* w1, const SessionWindow* w2) { | |
63 return GetMostRecentTabTimestamp(w1) > GetMostRecentTabTimestamp(w2); | |
64 } | |
65 | |
66 } // namespace | |
Dan Beam
2012/10/19 04:18:47
nit: 2 \s before ending namespace comments
jeremycho
2012/10/20 23:38:15
Done.
| |
67 | |
68 OtherDeviceMenu::OtherDeviceMenu(content::WebUI* web_ui, | |
69 const std::string& session_id, | |
70 const gfx::Point& location) : | |
71 web_ui_(web_ui), | |
72 session_id_(session_id), | |
73 location_(location), | |
74 ALLOW_THIS_IN_INITIALIZER_LIST(menu_model_(this)) { | |
75 AddDeviceTabs(); | |
76 | |
77 // Add a "Open all" menu item if there is more than one tab. | |
78 if (!tab_data_.empty()) { | |
79 linked_ptr<DictionaryValue> open_all_tab_value = | |
80 linked_ptr<DictionaryValue>(new DictionaryValue()); | |
81 // kInvalidId signifies that the entire session should be opened. | |
82 open_all_tab_value->SetInteger( | |
83 "sessionId", | |
84 browser_sync::ForeignSessionHandler::kInvalidId); | |
85 open_all_tab_value->SetInteger( | |
86 "windowId", | |
87 browser_sync::ForeignSessionHandler::kInvalidId); | |
88 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
89 menu_model_.AddItem( | |
90 tab_data_.size(), | |
91 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL)); | |
92 tab_data_.push_back(open_all_tab_value); | |
93 } | |
94 } | |
95 | |
96 OtherDeviceMenu::~OtherDeviceMenu() { | |
97 } | |
98 | |
99 void OtherDeviceMenu::ShowMenu() { | |
100 content::WebContents* web_contents = web_ui_->GetWebContents(); | |
101 Browser* browser = browser::FindBrowserWithWebContents(web_contents); | |
Dan Beam
2012/10/19 04:18:47
nit: s/ = / = /
jeremycho
2012/10/20 23:38:15
Done.
| |
102 if (!browser) | |
103 return; | |
104 | |
105 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | |
106 browser->window()->GetNativeWindow()); | |
107 if (!widget) | |
108 return; | |
109 | |
110 views::MenuModelAdapter menu_model_adapter(&menu_model_); | |
111 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); | |
112 | |
113 if (menu_runner_->RunMenuAt(widget, NULL, gfx::Rect(location_, gfx::Size()), | |
114 views::MenuItemView::TOPLEFT, 0) == | |
115 views::MenuRunner::MENU_DELETED) { | |
116 return; | |
117 } | |
118 } | |
119 | |
120 bool OtherDeviceMenu::IsCommandIdChecked(int command_id) const { | |
121 return false; | |
122 } | |
123 | |
124 bool OtherDeviceMenu::IsCommandIdEnabled(int command_id) const { | |
125 return true; | |
126 } | |
127 | |
128 void OtherDeviceMenu::ExecuteCommand(int command_id) { | |
129 ExecuteCommand(command_id, 0); | |
130 } | |
131 | |
132 // TODO(jeremycho): Figure out why mouse wheel clicks don't trigger this. | |
133 void OtherDeviceMenu::ExecuteCommand(int command_id, int event_flags) { | |
134 DCHECK_GT(tab_data_.size(), static_cast<size_t>(command_id)) << | |
135 "Invalid command_id from other device menu."; | |
136 | |
137 linked_ptr<DictionaryValue> tab_data = tab_data_[command_id]; | |
138 // This is not a mistake - sessionId actually refers to the tab id. | |
139 // See http://crbug.com/154865. | |
140 int tab_id = browser_sync::ForeignSessionHandler::kInvalidId; | |
141 tab_data->GetInteger("sessionId", &tab_id); | |
142 | |
143 int window_id = browser_sync::ForeignSessionHandler::kInvalidId; | |
144 tab_data->GetInteger("windowId", &window_id); | |
145 | |
146 WindowOpenDisposition disposition = | |
147 chrome::DispositionFromEventFlags(event_flags); | |
148 browser_sync::ForeignSessionHandler::OpenForeignSession( | |
149 web_ui_, session_id_, window_id, tab_id, disposition); | |
150 | |
151 ItemType itemType = tab_id == | |
152 browser_sync::ForeignSessionHandler::kInvalidId ? OPEN_ALL : TAB; | |
153 UMA_HISTOGRAM_ENUMERATION("NewTabPage.OtherDeviceMenu", | |
154 itemType, ITEM_TYPE_ENUM_COUNT); | |
155 } | |
156 | |
157 bool OtherDeviceMenu::GetAcceleratorForCommandId( | |
158 int command_id, | |
159 ui::Accelerator* accelerator) { | |
160 return false; | |
161 } | |
162 | |
163 void OtherDeviceMenu::AddDeviceTabs() { | |
164 browser_sync::SessionModelAssociator* associator = | |
165 browser_sync::ForeignSessionHandler::GetModelAssociator(web_ui_); | |
166 std::vector<const SessionWindow*> windows; | |
167 | |
168 // Populate the menu with the device's tabs, using separators between windows. | |
169 if (associator && associator->GetForeignSession(session_id_, &windows)) { | |
170 // Show windows by descending last-modified time. | |
171 std::sort(windows.begin(), windows.end(), SortWindowsByRecency); | |
172 bool last_window_has_tabs = false; | |
173 for (std::vector<const SessionWindow*>::const_iterator it = | |
174 windows.begin(); it != windows.end(); ++it) { | |
175 if (last_window_has_tabs) | |
176 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
177 last_window_has_tabs = false; | |
178 | |
179 const SessionWindow* window = *it; | |
180 for (size_t i = 0, num_tabs = window->tabs.size(); i < num_tabs; ++i) { | |
181 linked_ptr<DictionaryValue> tab_value = | |
182 linked_ptr<DictionaryValue>(new DictionaryValue()); | |
183 if (browser_sync::ForeignSessionHandler::SessionTabToValue( | |
184 *window->tabs[i], tab_value.get())) { | |
Dan Beam
2012/10/19 04:18:47
nit: prefer
if (!browser_sync::ForeignSessionHa
jeremycho
2012/10/20 23:38:15
Done.
| |
185 last_window_has_tabs = true; | |
186 tab_value->SetInteger("windowId", window->window_id.id()); | |
187 string16 title; | |
188 tab_value->GetString("title", &title); | |
189 title = ui::ElideText( | |
190 title, gfx::Font(), kMaxWidth, ui::ELIDE_AT_END); | |
191 menu_model_.AddItem(tab_data_.size(), title); | |
192 // TODO(jeremycho): Use tab_value.GetString("url", &url) to | |
193 // request favicons. http://crbug.com/153410. | |
Dan Beam
2012/10/19 04:18:47
nit: 1 \s between sentences, you can probably make
jeremycho
2012/10/20 23:38:15
Done.
| |
194 tab_data_.push_back(tab_value); | |
195 if (tab_data_.size() >= kMaxTabsToShow) | |
196 return; | |
197 } | |
198 } | |
199 } | |
200 } | |
201 } | |
OLD | NEW |