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/ui/webui/ntp/foreign_session_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/i18n/time_formatting.h" |
12 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/sessions/session_restore.h" | 18 #include "chrome/browser/sessions/session_restore.h" |
18 #include "chrome/browser/sync/profile_sync_service.h" | 19 #include "chrome/browser/sync/profile_sync_service.h" |
19 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 21 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
21 #include "chrome/browser/ui/webui/web_ui_util.h" | 22 #include "chrome/browser/ui/webui/web_ui_util.h" |
22 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/time_format.h" |
23 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
24 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
26 #include "content/public/browser/web_ui.h" | 28 #include "content/public/browser/web_ui.h" |
27 | 29 |
28 namespace browser_sync { | 30 namespace browser_sync { |
29 | 31 |
30 // Maximum number of session we're going to display on the NTP | 32 // Maximum number of session we're going to display on the NTP |
31 static const size_t kMaxSessionsToShow = 10; | 33 static const size_t kMaxSessionsToShow = 10; |
32 | 34 |
(...skipping 14 matching lines...) Expand all Loading... |
47 } | 49 } |
48 | 50 |
49 void ForeignSessionHandler::RegisterMessages() { | 51 void ForeignSessionHandler::RegisterMessages() { |
50 Init(); | 52 Init(); |
51 web_ui()->RegisterMessageCallback("getForeignSessions", | 53 web_ui()->RegisterMessageCallback("getForeignSessions", |
52 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, | 54 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, |
53 base::Unretained(this))); | 55 base::Unretained(this))); |
54 web_ui()->RegisterMessageCallback("openForeignSession", | 56 web_ui()->RegisterMessageCallback("openForeignSession", |
55 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, | 57 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, |
56 base::Unretained(this))); | 58 base::Unretained(this))); |
| 59 web_ui()->RegisterMessageCallback("deleteForeignSession", |
| 60 base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession, |
| 61 base::Unretained(this))); |
57 } | 62 } |
58 | 63 |
59 void ForeignSessionHandler::Init() { | 64 void ForeignSessionHandler::Init() { |
60 Profile* profile = Profile::FromWebUI(web_ui()); | 65 Profile* profile = Profile::FromWebUI(web_ui()); |
61 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 66 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
62 content::Source<Profile>(profile)); | 67 content::Source<Profile>(profile)); |
63 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, | 68 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, |
64 content::Source<Profile>(profile)); | 69 content::Source<Profile>(profile)); |
65 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 70 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
66 content::Source<Profile>(profile)); | 71 content::Source<Profile>(profile)); |
(...skipping 30 matching lines...) Expand all Loading... |
97 return model_associator; | 102 return model_associator; |
98 } | 103 } |
99 | 104 |
100 bool ForeignSessionHandler::IsTabSyncEnabled() { | 105 bool ForeignSessionHandler::IsTabSyncEnabled() { |
101 Profile* profile = Profile::FromWebUI(web_ui()); | 106 Profile* profile = Profile::FromWebUI(web_ui()); |
102 ProfileSyncService* service = | 107 ProfileSyncService* service = |
103 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 108 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
104 return service && service->GetSessionModelAssociator(); | 109 return service && service->GetSessionModelAssociator(); |
105 } | 110 } |
106 | 111 |
| 112 string16 ForeignSessionHandler::FormatSessionTime(const base::Time& time) { |
| 113 // Return a time like "1 hour ago", "2 days ago", etc. |
| 114 return TimeFormat::TimeElapsed(base::Time::Now() - time); |
| 115 } |
| 116 |
107 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { | 117 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
108 SessionModelAssociator* associator = GetModelAssociator(); | 118 SessionModelAssociator* associator = GetModelAssociator(); |
109 std::vector<const SyncedSession*> sessions; | 119 std::vector<const SyncedSession*> sessions; |
110 | 120 |
111 ListValue session_list; | 121 ListValue session_list; |
112 if (associator && associator->GetAllForeignSessions(&sessions)) { | 122 if (associator && associator->GetAllForeignSessions(&sessions)) { |
113 // Sort sessions from most recent to least recent. | 123 // Sort sessions from most recent to least recent. |
114 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | 124 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
115 | 125 |
116 // Note: we don't own the SyncedSessions themselves. | 126 // Note: we don't own the SyncedSessions themselves. |
117 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { | 127 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { |
118 const SyncedSession* session = sessions[i]; | 128 const SyncedSession* session = sessions[i]; |
119 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | 129 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
120 session_data->SetString("tag", session->session_tag); | 130 session_data->SetString("tag", session->session_tag); |
121 session_data->SetString("name", session->session_name); | 131 session_data->SetString("name", session->session_name); |
| 132 session_data->SetString("modifiedTime", |
| 133 FormatSessionTime(session->modified_time)); |
122 scoped_ptr<ListValue> window_list(new ListValue()); | 134 scoped_ptr<ListValue> window_list(new ListValue()); |
123 for (SyncedSession::SyncedWindowMap::const_iterator it = | 135 for (SyncedSession::SyncedWindowMap::const_iterator it = |
124 session->windows.begin(); it != session->windows.end(); ++it) { | 136 session->windows.begin(); it != session->windows.end(); ++it) { |
125 SessionWindow* window = it->second; | 137 SessionWindow* window = it->second; |
126 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | 138 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); |
127 if (SessionWindowToValue(*window, window_data.get())) | 139 if (SessionWindowToValue(*window, window_data.get())) |
128 window_list->Append(window_data.release()); | 140 window_list->Append(window_data.release()); |
129 } | 141 } |
130 | 142 |
131 session_data->Set("windows", window_list.release()); | 143 session_data->Set("windows", window_list.release()); |
132 session_list.Append(session_data.release()); | 144 session_list.Append(session_data.release()); |
133 } | 145 } |
134 } | 146 } |
135 base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled()); | 147 base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled()); |
136 web_ui()->CallJavascriptFunction("ntp.setForeignSessions", | 148 web_ui()->CallJavascriptFunction("ntp.setForeignSessions", |
137 session_list, | 149 session_list, |
138 tab_sync_enabled); | 150 tab_sync_enabled); |
139 } | 151 } |
140 | 152 |
141 void ForeignSessionHandler::HandleOpenForeignSession( | 153 void ForeignSessionHandler::HandleOpenForeignSession(const ListValue* args) { |
142 const ListValue* args) { | |
143 size_t num_args = args->GetSize(); | 154 size_t num_args = args->GetSize(); |
144 // Expect either 2 or 8 args. For restoring an entire window, only | 155 // Expect either 2 or 8 args. For restoring an entire window, only |
145 // two arguments are required -- the session tag and the window id. | 156 // two arguments are required -- the session tag and the window id. |
146 // To restore a tab, the additional args required are the tab id, | 157 // To restore a tab, the additional args required are the tab id, |
147 // and 4 properties of the event object (button, altKey, ctrlKey, | 158 // and 4 properties of the event object (button, altKey, ctrlKey, |
148 // metaKey, shiftKey) for determining how to open the tab. | 159 // metaKey, shiftKey) for determining how to open the tab. |
149 if (num_args != 8U && num_args != 2U) { | 160 if (num_args != 8U && num_args != 2U) { |
150 LOG(ERROR) << "openForeignSession called with " << args->GetSize() | 161 LOG(ERROR) << "openForeignSession called with " << args->GetSize() |
151 << " arguments."; | 162 << " arguments."; |
152 return; | 163 return; |
153 } | 164 } |
154 | 165 |
155 // Extract the machine tag (always provided). | 166 // Extract the session tag (always provided). |
156 std::string session_string_value; | 167 std::string session_string_value; |
157 if (!args->GetString(0, &session_string_value)) { | 168 if (!args->GetString(0, &session_string_value)) { |
158 LOG(ERROR) << "Failed to extract session tag."; | 169 LOG(ERROR) << "Failed to extract session tag."; |
159 return; | 170 return; |
160 } | 171 } |
161 | 172 |
162 // Extract window number (always provided). | 173 // Extract window number (always provided). |
163 std::string window_num_str; | 174 std::string window_num_str; |
164 int window_num = kInvalidId; | 175 int window_num = kInvalidId; |
165 if (num_args >= 2 && (!args->GetString(1, &window_num_str) || | 176 if (num_args >= 2 && (!args->GetString(1, &window_num_str) || |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 std::vector<const SessionWindow*>::const_iterator iter_begin = | 213 std::vector<const SessionWindow*>::const_iterator iter_begin = |
203 windows.begin() + ((window_num == kInvalidId) ? 0 : window_num); | 214 windows.begin() + ((window_num == kInvalidId) ? 0 : window_num); |
204 std::vector<const SessionWindow*>::const_iterator iter_end = | 215 std::vector<const SessionWindow*>::const_iterator iter_end = |
205 ((window_num == kInvalidId) ? | 216 ((window_num == kInvalidId) ? |
206 std::vector<const SessionWindow*>::const_iterator(windows.end()) : | 217 std::vector<const SessionWindow*>::const_iterator(windows.end()) : |
207 iter_begin + 1); | 218 iter_begin + 1); |
208 SessionRestore::RestoreForeignSessionWindows(profile, iter_begin, iter_end); | 219 SessionRestore::RestoreForeignSessionWindows(profile, iter_begin, iter_end); |
209 } | 220 } |
210 } | 221 } |
211 | 222 |
| 223 void ForeignSessionHandler::HandleDeleteForeignSession(const ListValue* args) { |
| 224 if (args->GetSize() != 1U) { |
| 225 LOG(ERROR) << "Wrong number of args to deleteForeignSession"; |
| 226 return; |
| 227 } |
| 228 |
| 229 // Get the session tag argument (required). |
| 230 std::string session_tag = UTF16ToUTF8(ExtractStringValue(args)); |
| 231 |
| 232 SessionModelAssociator* associator = GetModelAssociator(); |
| 233 if (associator) |
| 234 associator->DeleteForeignSession(session_tag); |
| 235 } |
| 236 |
212 bool ForeignSessionHandler::SessionTabToValue( | 237 bool ForeignSessionHandler::SessionTabToValue( |
213 const SessionTab& tab, | 238 const SessionTab& tab, |
214 DictionaryValue* dictionary) { | 239 DictionaryValue* dictionary) { |
215 if (tab.navigations.empty()) | 240 if (tab.navigations.empty()) |
216 return false; | 241 return false; |
217 int selected_index = tab.current_navigation_index; | 242 int selected_index = tab.current_navigation_index; |
218 selected_index = std::max( | 243 selected_index = std::max( |
219 0, | 244 0, |
220 std::min(selected_index, | 245 std::min(selected_index, |
221 static_cast<int>(tab.navigations.size() - 1))); | 246 static_cast<int>(tab.navigations.size() - 1))); |
(...skipping 27 matching lines...) Expand all Loading... |
249 return false; | 274 return false; |
250 dictionary->SetString("type", "window"); | 275 dictionary->SetString("type", "window"); |
251 dictionary->SetDouble("timestamp", | 276 dictionary->SetDouble("timestamp", |
252 static_cast<double>(window.timestamp.ToInternalValue())); | 277 static_cast<double>(window.timestamp.ToInternalValue())); |
253 dictionary->SetInteger("sessionId", window.window_id.id()); | 278 dictionary->SetInteger("sessionId", window.window_id.id()); |
254 dictionary->Set("tabs", tab_values.release()); | 279 dictionary->Set("tabs", tab_values.release()); |
255 return true; | 280 return true; |
256 } | 281 } |
257 | 282 |
258 } // namespace browser_sync | 283 } // namespace browser_sync |
OLD | NEW |