Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/ui/search/other_device_menu.cc

Issue 11009013: NTP5: Starting implementation of a native menu for showing other device sessions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to comments. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // The max number of tabs that will be added to the menu.
25 static const size_t kMaxTabsToShow = 18;
26
27 // The max width of a menu. Menu text exceeding this will be elided.
28 static const int kMaxWidth = 375;
29
30 namespace {
31
32 // Helper function that returns the largest tab timestamp for the window.
33 double getMaxTabTimestamp(const SessionWindow* window) {
34 double max_timestamp = 0;
35 for (size_t i = 0; i < window->tabs.size(); ++i) {
36 linked_ptr<DictionaryValue> tab_value =
37 linked_ptr<DictionaryValue>(new DictionaryValue());
38 if (browser_sync::ForeignSessionHandler::SessionTabToValue(
39 *window->tabs[i], tab_value.get())) {
40 double timestamp;
41 tab_value->GetDouble("timestamp", &timestamp);
42 if (timestamp > max_timestamp)
43 max_timestamp = timestamp;
44 }
45 }
46 return max_timestamp;
47 }
48
49 // Comparator function to sort windows by last-modified time. Windows in a
50 // session share the same timestamp, so we need to use tab timestamps instead.
51 // instead.
52 bool SortWindowsByRecency(const SessionWindow* w1, const SessionWindow* w2) {
53 return getMaxTabTimestamp(w1) > getMaxTabTimestamp(w2);
54 }
55
56 } // namespace
57
58 OtherDeviceMenu::OtherDeviceMenu(content::WebUI* web_ui,
59 std::string session_id,
60 gfx::Point location) :
61 web_ui_(web_ui), session_id_(session_id), location_(location),
62 ALLOW_THIS_IN_INITIALIZER_LIST(menu_model_(this)) {
63 AddDeviceTabs();
64
65 // Add a "Open all" menu item if there is more than one tab.
66 if (tab_data_.size() > 1) {
67 linked_ptr<DictionaryValue> open_all_tab_value =
68 linked_ptr<DictionaryValue>(new DictionaryValue());
69 // kInvalidId signifies that the entire session should be opened.
70 open_all_tab_value->SetInteger(
71 "sessionId",
72 browser_sync::ForeignSessionHandler::kInvalidId);
73 open_all_tab_value->SetInteger(
74 "windowId",
75 browser_sync::ForeignSessionHandler::kInvalidId);
76 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
77 menu_model_.AddItem(
78 tab_data_.size(),
79 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL));
80 tab_data_.push_back(open_all_tab_value);
81 }
82 }
83
84 OtherDeviceMenu::~OtherDeviceMenu() {
85 }
86
87 void OtherDeviceMenu::ShowMenu() {
88 content::WebContents* web_contents = web_ui_->GetWebContents();
89 Browser* browser = browser::FindBrowserWithWebContents(web_contents);
90 if (!browser)
91 return;
92
93 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(
94 browser->window()->GetNativeWindow());
95 if (!widget)
96 return;
97
98 views::MenuModelAdapter menu_model_adapter(&menu_model_);
99 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
100
101 if (menu_runner_->RunMenuAt(widget, NULL, gfx::Rect(location_, gfx::Size()),
102 views::MenuItemView::TOPLEFT, 0) ==
103 views::MenuRunner::MENU_DELETED)
104 return;
105 }
106
107 bool OtherDeviceMenu::IsCommandIdChecked(int command_id) const {
108 return false;
109 }
110
111 bool OtherDeviceMenu::IsCommandIdEnabled(int command_id) const {
112 return true;
113 }
114
115 void OtherDeviceMenu::ExecuteCommand(int command_id) {
116 ExecuteCommand(command_id, 0);
117 }
118
119 // TODO(jeremycho): Figure out why mouse wheel clicks don't trigger this.
120 void OtherDeviceMenu::ExecuteCommand(int command_id, int event_flags) {
121 DCHECK_GT(tab_data_.size(), static_cast<size_t>(command_id)) <<
122 "Invalid command_id from other device menu.";
123
124 linked_ptr<DictionaryValue> tab_data = tab_data_[command_id];
125 // This is not a mistake - sessionId actually refers to the tab id.
126 // See ForeignSessionHandler::SessionTabToValue.
127 int tab_id = browser_sync::ForeignSessionHandler::kInvalidId;
128 tab_data->GetInteger("sessionId", &tab_id);
129
130 int window_id = browser_sync::ForeignSessionHandler::kInvalidId;
131 tab_data->GetInteger("windowId", &window_id);
132
133 WindowOpenDisposition disposition =
134 chrome::DispositionFromEventFlags(event_flags);
135 browser_sync::ForeignSessionHandler::OpenForeignSession(
136 web_ui_, session_id_, window_id, tab_id, disposition);
137
138 ItemType itemType =
139 tab_id == browser_sync::ForeignSessionHandler::kInvalidId ?
140 OtherDeviceMenu::OPEN_ALL : OtherDeviceMenu::TAB;
141 UMA_HISTOGRAM_ENUMERATION("NewTabPage.OtherDeviceMenu",
142 itemType, OtherDeviceMenu::ITEM_TYPE_ENUM_COUNT);
143 }
144
145 bool OtherDeviceMenu::GetAcceleratorForCommandId(
146 int command_id,
147 ui::Accelerator* accelerator) {
148 return false;
149 }
150
151 void OtherDeviceMenu::AddDeviceTabs() {
152 browser_sync::SessionModelAssociator* associator =
153 browser_sync::ForeignSessionHandler::GetModelAssociator(web_ui_);
154 std::vector<const SessionWindow*> windows;
155
156 // Populate the menu with the device's tabs, using separators between windows.
157 if (associator && associator->GetForeignSession(session_id_, &windows)) {
158 // Show windows by descending last-modified time.
159 std::sort(windows.begin(), windows.end(), SortWindowsByRecency);
160 bool last_window_has_tabs = false;
161 for (std::vector<const SessionWindow*>::const_iterator it =
162 windows.begin(); it != windows.end(); ++it) {
163 if (last_window_has_tabs)
164 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
165 last_window_has_tabs = false;
166
167 const SessionWindow* window = *it;
168 for (size_t i = 0; i < window->tabs.size(); ++i) {
169 linked_ptr<DictionaryValue> tab_value =
170 linked_ptr<DictionaryValue>(new DictionaryValue());
171 if (browser_sync::ForeignSessionHandler::SessionTabToValue(
172 *window->tabs[i], tab_value.get())) {
173 last_window_has_tabs = true;
174 tab_value->SetInteger("windowId", window->window_id.id());
175 string16 title;
176 tab_value->GetString("title", &title);
177 title = ui::ElideText(
178 title, gfx::Font(), kMaxWidth, ui::ELIDE_AT_END);
179 menu_model_.AddItem(tab_data_.size(), title);
180 // TODO(jeremycho): Use tab_value.GetString("url", &url) to
181 // request favicons. http://crbug.com/153410.
182 tab_data_.push_back(tab_value);
183 if (tab_data_.size() >= kMaxTabsToShow)
184 return;
185 }
186 }
187 }
188 }
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698