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/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/sessions/session_restore.h" | 17 #include "chrome/browser/sessions/session_restore.h" |
18 #include "chrome/browser/sync/profile_sync_service.h" | 18 #include "chrome/browser/sync/profile_sync_service.h" |
19 #include "chrome/browser/sync/profile_sync_service_factory.h" | 19 #include "chrome/browser/sync/profile_sync_service_factory.h" |
20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
21 #include "chrome/browser/ui/webui/web_ui_util.h" | 21 #include "chrome/browser/ui/webui/web_ui_util.h" |
22 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
24 #include "content/public/browser/notification_service.h" | |
24 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
25 #include "content/public/browser/web_ui.h" | 26 #include "content/public/browser/web_ui.h" |
26 | 27 |
27 namespace browser_sync { | 28 namespace browser_sync { |
28 | 29 |
29 // Maximum number of session we're going to display on the NTP | 30 // Maximum number of session we're going to display on the NTP |
30 static const int kMaxSessionsToShow = 10; | 31 static const int kMaxSessionsToShow = 10; |
31 | 32 |
32 // Invalid value, used to note that we don't have a tab or window number. | 33 // Invalid value, used to note that we don't have a tab or window number. |
33 static const int kInvalidId = -1; | 34 static const int kInvalidId = -1; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 service->GetSessionModelAssociator(); | 88 service->GetSessionModelAssociator(); |
88 if (!service->ShouldPushChanges()) | 89 if (!service->ShouldPushChanges()) |
89 return NULL; | 90 return NULL; |
90 return model_associator; | 91 return model_associator; |
91 } | 92 } |
92 | 93 |
93 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { | 94 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
94 SessionModelAssociator* associator = GetModelAssociator(); | 95 SessionModelAssociator* associator = GetModelAssociator(); |
95 std::vector<const SyncedSession*> sessions; | 96 std::vector<const SyncedSession*> sessions; |
96 | 97 |
97 if (associator == NULL) { | |
98 // Called before associator created, exit. | |
99 return; | |
100 } | |
101 | |
102 // Note: we don't own the SyncedSessions themselves. | |
103 if (!associator->GetAllForeignSessions(&sessions)) { | |
104 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" | |
105 "SessionModelAssociator."; | |
106 return; | |
107 } | |
108 int added_count = 0; | |
109 ListValue session_list; | 98 ListValue session_list; |
110 for (std::vector<const SyncedSession*>::const_iterator i = | 99 if (associator && associator->GetAllForeignSessions(&sessions)) { |
111 sessions.begin(); i != sessions.end() && | 100 // Note: we don't own the SyncedSessions themselves. |
112 added_count < kMaxSessionsToShow; ++i) { | 101 int added_count = 0; |
113 const SyncedSession* session = *i; | 102 for (std::vector<const SyncedSession*>::const_iterator i = |
Dan Beam
2012/03/26 20:42:39
change from iterator to index
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
114 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | 103 sessions.begin(); i != sessions.end() && |
115 session_data->SetString("tag", session->session_tag); | 104 added_count < kMaxSessionsToShow; ++i) { |
116 session_data->SetString("name", session->session_name); | 105 const SyncedSession* session = *i; |
117 scoped_ptr<ListValue> window_list(new ListValue()); | 106 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
118 for (SyncedSession::SyncedWindowMap::const_iterator it = | 107 session_data->SetString("tag", session->session_tag); |
119 session->windows.begin(); it != session->windows.end(); ++it) { | 108 session_data->SetString("name", session->session_name); |
120 SessionWindow* window = it->second; | 109 scoped_ptr<ListValue> window_list(new ListValue()); |
121 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | 110 for (SyncedSession::SyncedWindowMap::const_iterator it = |
122 if (SessionWindowToValue(*window, window_data.get())) { | 111 session->windows.begin(); it != session->windows.end(); ++it) { |
123 window_list->Append(window_data.release()); | 112 SessionWindow* window = it->second; |
113 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | |
114 if (SessionWindowToValue(*window, window_data.get())) { | |
Dan Beam
2012/03/26 20:57:38
nit: no curlies
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
115 window_list->Append(window_data.release()); | |
116 } | |
124 } | 117 } |
118 session_data->Set("windows", window_list.release()); | |
119 session_list.Append(session_data.release()); | |
120 added_count++; | |
125 } | 121 } |
126 session_data->Set("windows", window_list.release()); | |
127 session_list.Append(session_data.release()); | |
128 added_count++; | |
129 } | 122 } |
130 web_ui()->CallJavascriptFunction("ntp.foreignSessions", session_list); | 123 web_ui()->CallJavascriptFunction("ntp.foreignSessions", session_list); |
131 } | 124 } |
132 | 125 |
133 void ForeignSessionHandler::HandleOpenForeignSession( | 126 void ForeignSessionHandler::HandleOpenForeignSession( |
134 const ListValue* args) { | 127 const ListValue* args) { |
135 size_t num_args = args->GetSize(); | 128 size_t num_args = args->GetSize(); |
136 // Expect either 2 or 8 args. For restoring an entire window, only | 129 // Expect either 2 or 8 args. For restoring an entire window, only |
137 // two arguments are required -- the session tag and the window id. | 130 // two arguments are required -- the session tag and the window id. |
138 // To restore a tab, the additional args required are the tab id, | 131 // To restore a tab, the additional args required are the tab id, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 return false; | 234 return false; |
242 dictionary->SetString("type", "window"); | 235 dictionary->SetString("type", "window"); |
243 dictionary->SetDouble("timestamp", | 236 dictionary->SetDouble("timestamp", |
244 static_cast<double>(window.timestamp.ToInternalValue())); | 237 static_cast<double>(window.timestamp.ToInternalValue())); |
245 dictionary->SetInteger("sessionId", window.window_id.id()); | 238 dictionary->SetInteger("sessionId", window.window_id.id()); |
246 dictionary->Set("tabs", tab_values.release()); | 239 dictionary->Set("tabs", tab_values.release()); |
247 return true; | 240 return true; |
248 } | 241 } |
249 | 242 |
250 } // namespace browser_sync | 243 } // namespace browser_sync |
OLD | NEW |