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/string16.h" | |
8 #include "base/utf_string_conversions.h" | |
dhollowa
2012/10/01 17:37:38
This should be removed. We'll put the string in r
jeremycho
2012/10/01 22:53:40
Done.
| |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/browser_finder.h" | |
11 #include "chrome/browser/ui/browser_window.h" | |
12 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/browser/web_ui.h" | |
15 #include "chrome/browser/sync/glue/session_model_associator.h" | |
16 #include "ui/base/text/text_elider.h" | |
17 #include "ui/views/controls/menu/menu_model_adapter.h" | |
18 #include "ui/views/controls/menu/menu_runner.h" | |
19 #include "ui/views/widget/widget.h" | |
20 #include "webkit/glue/window_open_disposition.h" | |
21 | |
22 using browser_sync::ForeignSessionHandler; | |
23 using browser_sync::SessionModelAssociator; | |
24 | |
25 // The max width of a menu. Menu text exceeding this will be elided. | |
26 static const int kMaxWidth = 375; | |
27 | |
28 OtherDeviceMenu::OtherDeviceMenu(content::WebUI* web_ui, | |
29 const std::string& session_id, | |
30 const gfx::Point& location) : | |
31 web_ui_(web_ui), session_id_(session_id), location_(location), | |
32 ALLOW_THIS_IN_INITIALIZER_LIST(menu_model_(this)) { | |
33 SessionModelAssociator* associator = | |
34 ForeignSessionHandler::GetModelAssociator(web_ui); | |
35 std::vector<const SessionWindow*> windows; | |
36 | |
37 // Populate the menu with the device's tabs, using separators between windows. | |
38 // TODO(jeremycho): Limit the number of menu items? | |
dhollowa
2012/10/01 17:37:38
I see browser_sync::kMaxSessionsToShow. Use that?
jeremycho
2012/10/01 22:53:40
A menu should only contain a single session. Adde
| |
39 if (associator && associator->GetForeignSession(session_id, &windows)) { | |
dhollowa
2012/10/01 17:37:38
Should |associator| ever be NULL here? If not pre
jeremycho
2012/10/01 22:53:40
Yes it can be, e.g. if it hasn't finished syncing.
| |
40 int command_id = 0; | |
41 bool lastWindowHasTabs = false; | |
dhollowa
2012/10/01 17:37:38
C++ style is: |last_window_has_tabs|.
jeremycho
2012/10/01 22:53:40
Done.
| |
42 for (std::vector<const SessionWindow*>::const_iterator it = | |
43 windows.begin(); it != windows.end(); ++it) { | |
44 if (lastWindowHasTabs) | |
45 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
46 lastWindowHasTabs = false; | |
47 | |
48 const SessionWindow* window = *it; | |
49 for (size_t i = 0; i < window->tabs.size(); ++i) { | |
50 linked_ptr<DictionaryValue> tab_value = | |
51 linked_ptr<DictionaryValue>(new DictionaryValue()); | |
52 if (ForeignSessionHandler::SessionTabToValue( | |
53 *window->tabs[i], tab_value.get())) { | |
54 lastWindowHasTabs = true; | |
55 tab_value->SetInteger("windowId", window->window_id.id()); | |
56 string16 title; | |
57 tab_value->GetString("title", &title); | |
58 title = ui::ElideText( | |
59 title, gfx::Font(), kMaxWidth, ui::ELIDE_AT_END); | |
60 menu_model_.AddItem(command_id, title); | |
61 session_data_[++command_id] = tab_value; | |
62 // TODO(jeremycho): Use tab_value.GetString("url", &url) to | |
dhollowa
2012/10/01 17:37:38
Please add a link to associated bug in this TODO.
jeremycho
2012/10/01 22:53:40
Done.
| |
63 // request favicons. | |
64 } | |
65 } | |
66 } | |
67 | |
68 // Add a "Show all" menu item if there is more than one tab. | |
69 if (session_data_.size() > 1) { | |
70 linked_ptr<DictionaryValue> show_all_tab_value = | |
71 linked_ptr<DictionaryValue>(new DictionaryValue()); | |
72 // kInvalidId signifies that the entire session should be opened. | |
73 show_all_tab_value->SetInteger( | |
74 "sessionId", | |
75 ForeignSessionHandler::kInvalidId); | |
76 show_all_tab_value->SetInteger( | |
77 "windowId", | |
78 ForeignSessionHandler::kInvalidId); | |
79 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
80 // TODO(jeremycho): i18n. | |
81 menu_model_.AddItem(command_id, UTF8ToUTF16("Show all")); | |
dhollowa
2012/10/01 17:37:38
Let's do this now. We shouldn't be hard coding st
jeremycho
2012/10/01 22:53:40
Reused the existing string from NTP4's other devic
| |
82 session_data_[++command_id] = show_all_tab_value; | |
83 } | |
84 } | |
85 } | |
86 | |
87 OtherDeviceMenu::~OtherDeviceMenu() { | |
88 } | |
89 | |
90 void OtherDeviceMenu::ShowMenu() { | |
91 content::WebContents* web_contents = web_ui_->GetWebContents(); | |
dhollowa
2012/10/01 17:37:38
Is it possible to get a NULL result back here? I'
jeremycho
2012/10/01 22:53:40
WebContents* shouldn't be NULL, but the others cou
| |
92 Browser* browser = browser::FindBrowserWithWebContents(web_contents); | |
93 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | |
94 browser->window()->GetNativeWindow()); | |
95 views::MenuModelAdapter menu_model_adapter(&menu_model_); | |
96 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); | |
97 | |
98 if (menu_runner_->RunMenuAt(widget, NULL, gfx::Rect(location_, gfx::Size()), | |
dhollowa
2012/10/01 17:37:38
Prefer: RunResult result = menu_runner_->RunMenuAt
jeremycho
2012/10/01 22:53:40
We don't expect that necessarily - only if the Men
| |
99 views::MenuItemView::TOPLEFT, 0) == | |
100 views::MenuRunner::MENU_DELETED) | |
101 return; | |
102 } | |
103 | |
104 bool OtherDeviceMenu::IsCommandIdChecked(int command_id) const { | |
105 return false; | |
106 } | |
107 | |
108 bool OtherDeviceMenu::IsCommandIdEnabled(int command_id) const { | |
109 return true; | |
110 } | |
111 | |
112 void OtherDeviceMenu::ExecuteCommand(int command_id) { | |
113 ExecuteCommand(command_id, 0); | |
114 } | |
115 | |
116 // TODO(jeremycho): Figure out why mouse wheel clicks don't trigger this. | |
117 void OtherDeviceMenu::ExecuteCommand(int command_id, int event_flags) { | |
118 if (session_data_.count(command_id) == 0) { | |
dhollowa
2012/10/01 17:37:38
Chrome style typically does not mask logic errors
jeremycho
2012/10/01 22:53:40
Done.
| |
119 NOTREACHED() << "Invalid command_id from other device menu."; | |
120 return; | |
121 } | |
122 | |
123 linked_ptr<DictionaryValue> tab_data = session_data_[command_id]; | |
124 // This is not a mistake - sessionId actually refers to the tab id. | |
125 // See ForeignSessionHandler::SessionTabToValue. | |
126 int tab_id = ForeignSessionHandler::kInvalidId; | |
127 tab_data->GetInteger("sessionId", &tab_id); | |
128 | |
129 int window_id = ForeignSessionHandler::kInvalidId; | |
130 tab_data->GetInteger("windowId", &window_id); | |
131 | |
132 WindowOpenDisposition disposition = | |
133 chrome::DispositionFromEventFlags(event_flags); | |
134 ForeignSessionHandler::OpenForeignSession( | |
135 web_ui_, session_id_, window_id, tab_id, disposition); | |
136 // TODO(jeremycho): Figure out what we want to log. | |
dhollowa
2012/10/01 17:37:38
If in doubt, log nothing.
jeremycho
2012/10/01 22:53:40
other_sessions.js logs session clicks, so most lik
| |
137 } | |
138 | |
139 bool OtherDeviceMenu::GetAcceleratorForCommandId( | |
140 int command_id, | |
141 ui::Accelerator* accelerator) { | |
142 return false; | |
143 } | |
OLD | NEW |