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 size_t 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; |
34 | 35 |
35 ForeignSessionHandler::ForeignSessionHandler() { | 36 ForeignSessionHandler::ForeignSessionHandler() { |
36 } | 37 } |
37 | 38 |
38 void ForeignSessionHandler::RegisterMessages() { | 39 void ForeignSessionHandler::RegisterMessages() { |
39 Init(); | 40 Init(); |
40 web_ui()->RegisterMessageCallback("getForeignSessions", | 41 web_ui()->RegisterMessageCallback("getForeignSessions", |
(...skipping 15 matching lines...) Expand all Loading... | |
56 } | 57 } |
57 | 58 |
58 void ForeignSessionHandler::Observe( | 59 void ForeignSessionHandler::Observe( |
59 int type, | 60 int type, |
60 const content::NotificationSource& source, | 61 const content::NotificationSource& source, |
61 const content::NotificationDetails& details) { | 62 const content::NotificationDetails& details) { |
62 ListValue list_value; | 63 ListValue list_value; |
63 switch (type) { | 64 switch (type) { |
64 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: | 65 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: |
65 case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: | 66 case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: |
67 case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED: | |
66 HandleGetForeignSessions(&list_value); | 68 HandleGetForeignSessions(&list_value); |
67 break; | 69 break; |
68 case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED: | |
69 // Calling foreignSessions with empty list will automatically hide | |
70 // foreign session section. | |
71 web_ui()->CallJavascriptFunction("ntp.foreignSessions", list_value); | |
72 break; | |
73 default: | 70 default: |
74 NOTREACHED(); | 71 NOTREACHED(); |
75 } | 72 } |
76 } | 73 } |
77 | 74 |
78 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { | 75 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { |
79 ProfileSyncService* service(ProfileSyncServiceFactory:: | 76 ProfileSyncService* service(ProfileSyncServiceFactory:: |
80 GetInstance()->GetForProfile(Profile::FromWebUI(web_ui()))); | 77 GetInstance()->GetForProfile(Profile::FromWebUI(web_ui()))); |
81 if (service == NULL) | 78 if (service == NULL) |
82 return NULL; | 79 return NULL; |
83 | 80 |
84 // We only want to set the model associator if there is one, and it is done | 81 // We only want to set the model associator if there is one, and it is done |
85 // syncing sessions. | 82 // syncing sessions. |
86 SessionModelAssociator* model_associator = | 83 SessionModelAssociator* model_associator = |
87 service->GetSessionModelAssociator(); | 84 service->GetSessionModelAssociator(); |
88 if (!service->ShouldPushChanges()) | 85 if (!service->ShouldPushChanges()) |
89 return NULL; | 86 return NULL; |
90 return model_associator; | 87 return model_associator; |
91 } | 88 } |
92 | 89 |
90 bool ForeignSessionHandler::IsTabSyncEnabled() { | |
91 ProfileSyncService* service(ProfileSyncServiceFactory:: | |
92 GetInstance()->GetForProfile(Profile::FromWebUI(web_ui()))); | |
93 return service && service->GetSessionModelAssociator() != NULL; | |
Dan Beam
2012/03/26 23:18:54
nit: remove "!= NULL" if possible, IMO
Patrick Dubroy
2012/03/27 00:36:51
Done.
| |
94 } | |
95 | |
93 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { | 96 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
94 SessionModelAssociator* associator = GetModelAssociator(); | 97 SessionModelAssociator* associator = GetModelAssociator(); |
95 std::vector<const SyncedSession*> sessions; | 98 std::vector<const SyncedSession*> sessions; |
96 | 99 |
97 if (associator == NULL) { | 100 ListValue session_list; |
98 // Called before associator created, exit. | 101 if (associator && associator->GetAllForeignSessions(&sessions)) { |
99 return; | 102 // Note: we don't own the SyncedSessions themselves. |
103 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { | |
104 const SyncedSession* session = sessions[i]; | |
105 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | |
106 session_data->SetString("tag", session->session_tag); | |
107 session_data->SetString("name", session->session_name); | |
108 scoped_ptr<ListValue> window_list(new ListValue()); | |
109 for (SyncedSession::SyncedWindowMap::const_iterator it = | |
110 session->windows.begin(); it != session->windows.end(); ++it) { | |
Dan Beam
2012/03/26 23:18:54
nit: +1 \s
Patrick Dubroy
2012/03/27 00:36:51
Done.
| |
111 SessionWindow* window = it->second; | |
112 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | |
113 if (SessionWindowToValue(*window, window_data.get())) | |
114 window_list->Append(window_data.release()); | |
115 } | |
116 session_data->Set("windows", window_list.release()); | |
117 session_list.Append(session_data.release()); | |
118 } | |
100 } | 119 } |
101 | 120 base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled()); |
102 // Note: we don't own the SyncedSessions themselves. | 121 web_ui()->CallJavascriptFunction("ntp.foreignSessions", |
103 if (!associator->GetAllForeignSessions(&sessions)) { | 122 session_list, |
104 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" | 123 tab_sync_enabled); |
105 "SessionModelAssociator."; | |
106 return; | |
107 } | |
108 int added_count = 0; | |
109 ListValue session_list; | |
110 for (std::vector<const SyncedSession*>::const_iterator i = | |
111 sessions.begin(); i != sessions.end() && | |
112 added_count < kMaxSessionsToShow; ++i) { | |
113 const SyncedSession* session = *i; | |
114 scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); | |
115 session_data->SetString("tag", session->session_tag); | |
116 session_data->SetString("name", session->session_name); | |
117 scoped_ptr<ListValue> window_list(new ListValue()); | |
118 for (SyncedSession::SyncedWindowMap::const_iterator it = | |
119 session->windows.begin(); it != session->windows.end(); ++it) { | |
120 SessionWindow* window = it->second; | |
121 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); | |
122 if (SessionWindowToValue(*window, window_data.get())) { | |
123 window_list->Append(window_data.release()); | |
124 } | |
125 } | |
126 session_data->Set("windows", window_list.release()); | |
127 session_list.Append(session_data.release()); | |
128 added_count++; | |
129 } | |
130 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, |
139 // and 4 properties of the event object (button, altKey, ctrlKey, | 132 // and 4 properties of the event object (button, altKey, ctrlKey, |
140 // metaKey, shiftKey) for determining how to open the tab. | 133 // metaKey, shiftKey) for determining how to open the tab. |
(...skipping 100 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 |