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

Side by Side Diff: chrome/browser/ui/webui/ntp/foreign_session_handler.cc

Issue 9838064: Add a sign-in promo message to the Other Devices menu. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix copyright header. Created 8 years, 9 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
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
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 Profile* profile = Profile::FromWebUI(web_ui());
80 GetInstance()->GetForProfile(Profile::FromWebUI(web_ui()))); 77 ProfileSyncService* service =
78 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
81 if (service == NULL) 79 if (service == NULL)
82 return NULL; 80 return NULL;
83 81
84 // We only want to set the model associator if there is one, and it is done 82 // Only return the associator if it exists and it is done syncing sessions.
85 // 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 Profile* profile = Profile::FromWebUI(web_ui());
92 ProfileSyncService* service =
93 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
94 return service && service->GetSessionModelAssociator();
95 }
96
93 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { 97 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) {
94 SessionModelAssociator* associator = GetModelAssociator(); 98 SessionModelAssociator* associator = GetModelAssociator();
95 std::vector<const SyncedSession*> sessions; 99 std::vector<const SyncedSession*> sessions;
96 100
97 if (associator == NULL) { 101 ListValue session_list;
98 // Called before associator created, exit. 102 if (associator && associator->GetAllForeignSessions(&sessions)) {
99 return; 103 // Note: we don't own the SyncedSessions themselves.
104 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) {
105 const SyncedSession* session = sessions[i];
106 scoped_ptr<DictionaryValue> session_data(new DictionaryValue());
107 session_data->SetString("tag", session->session_tag);
108 session_data->SetString("name", session->session_name);
109 scoped_ptr<ListValue> window_list(new ListValue());
110 for (SyncedSession::SyncedWindowMap::const_iterator it =
111 session->windows.begin(); it != session->windows.end(); ++it) {
112 SessionWindow* window = it->second;
113 scoped_ptr<DictionaryValue> window_data(new DictionaryValue());
114 if (SessionWindowToValue(*window, window_data.get()))
115 window_list->Append(window_data.release());
116 }
117
118 session_data->Set("windows", window_list.release());
119 session_list.Append(session_data.release());
120 }
100 } 121 }
101 122 base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled());
102 // Note: we don't own the SyncedSessions themselves. 123 web_ui()->CallJavascriptFunction("ntp.setForeignSessions",
103 if (!associator->GetAllForeignSessions(&sessions)) { 124 session_list,
104 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" 125 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 } 126 }
132 127
133 void ForeignSessionHandler::HandleOpenForeignSession( 128 void ForeignSessionHandler::HandleOpenForeignSession(
134 const ListValue* args) { 129 const ListValue* args) {
135 size_t num_args = args->GetSize(); 130 size_t num_args = args->GetSize();
136 // Expect either 2 or 8 args. For restoring an entire window, only 131 // Expect either 2 or 8 args. For restoring an entire window, only
137 // two arguments are required -- the session tag and the window id. 132 // two arguments are required -- the session tag and the window id.
138 // To restore a tab, the additional args required are the tab id, 133 // To restore a tab, the additional args required are the tab id,
139 // and 4 properties of the event object (button, altKey, ctrlKey, 134 // and 4 properties of the event object (button, altKey, ctrlKey,
140 // metaKey, shiftKey) for determining how to open the tab. 135 // metaKey, shiftKey) for determining how to open the tab.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 return false; 236 return false;
242 dictionary->SetString("type", "window"); 237 dictionary->SetString("type", "window");
243 dictionary->SetDouble("timestamp", 238 dictionary->SetDouble("timestamp",
244 static_cast<double>(window.timestamp.ToInternalValue())); 239 static_cast<double>(window.timestamp.ToInternalValue()));
245 dictionary->SetInteger("sessionId", window.window_id.id()); 240 dictionary->SetInteger("sessionId", window.window_id.id());
246 dictionary->Set("tabs", tab_values.release()); 241 dictionary->Set("tabs", tab_values.release());
247 return true; 242 return true;
248 } 243 }
249 244
250 } // namespace browser_sync 245 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/foreign_session_handler.h ('k') | chrome/browser/ui/webui/ntp/ntp_login_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698