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

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

Issue 7554008: Removal of Profile from content part 6. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sessions/session_restore.h" 15 #include "chrome/browser/sessions/session_restore.h"
16 #include "chrome/browser/sync/engine/syncapi.h" 16 #include "chrome/browser/sync/engine/syncapi.h"
17 #include "chrome/browser/sync/profile_sync_service.h" 17 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 18 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
19 #include "chrome/common/chrome_notification_types.h" 19 #include "chrome/common/chrome_notification_types.h"
20 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/notification_details.h" 21 #include "content/common/notification_details.h"
21 #include "content/common/notification_service.h" 22 #include "content/common/notification_service.h"
22 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
23 24
24 namespace browser_sync { 25 namespace browser_sync {
25 26
26 // Maximum number of session we're going to display on the NTP 27 // Maximum number of session we're going to display on the NTP
27 static const int kMaxSessionsToShow = 10; 28 static const int kMaxSessionsToShow = 10;
28 29
29 // Invalid value, used to note that we don't have a tab or window number. 30 // Invalid value, used to note that we don't have a tab or window number.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // Calling foreignSessions with empty list will automatically hide 65 // Calling foreignSessions with empty list will automatically hide
65 // foreign session section. 66 // foreign session section.
66 web_ui_->CallJavascriptFunction("foreignSessions", list_value); 67 web_ui_->CallJavascriptFunction("foreignSessions", list_value);
67 break; 68 break;
68 default: 69 default:
69 NOTREACHED(); 70 NOTREACHED();
70 } 71 }
71 } 72 }
72 73
73 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { 74 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() {
74 ProfileSyncService* service = web_ui_->GetProfile()->GetProfileSyncService(); 75 Profile* profile =
76 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context());
77 ProfileSyncService* service = profile->GetProfileSyncService();
75 if (service == NULL) 78 if (service == NULL)
76 return NULL; 79 return NULL;
80
77 // 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
78 // syncing sessions. 82 // syncing sessions.
79 SessionModelAssociator* model_associator = service-> 83 SessionModelAssociator* model_associator =
80 GetSessionModelAssociator(); 84 service->GetSessionModelAssociator();
81 if (model_associator == NULL || 85 if (model_associator == NULL ||
82 !service->ShouldPushChanges()) { 86 !service->ShouldPushChanges()) {
83 return NULL; 87 return NULL;
84 } 88 }
85 return web_ui_->GetProfile()->GetProfileSyncService()-> 89 return model_associator;
86 GetSessionModelAssociator();
87 } 90 }
88 91
89 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { 92 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) {
90 SessionModelAssociator* associator = GetModelAssociator(); 93 SessionModelAssociator* associator = GetModelAssociator();
91 std::vector<const SyncedSession*> sessions; 94 std::vector<const SyncedSession*> sessions;
92 95
93 if (associator == NULL) { 96 if (associator == NULL) {
94 // Called before associator created, exit. 97 // Called before associator created, exit.
95 return; 98 return;
96 } 99 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 std::string tab_id_str; 160 std::string tab_id_str;
158 SessionID::id_type tab_id = kInvalidId; 161 SessionID::id_type tab_id = kInvalidId;
159 if (num_args == 3 && (!args->GetString(2, &tab_id_str) || 162 if (num_args == 3 && (!args->GetString(2, &tab_id_str) ||
160 !base::StringToInt(tab_id_str, &tab_id))) { 163 !base::StringToInt(tab_id_str, &tab_id))) {
161 LOG(ERROR) << "Failed to extract tab SessionID."; 164 LOG(ERROR) << "Failed to extract tab SessionID.";
162 return; 165 return;
163 } 166 }
164 167
165 SessionModelAssociator* associator = GetModelAssociator(); 168 SessionModelAssociator* associator = GetModelAssociator();
166 169
170 Profile* profile =
171 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context());
167 if (tab_id != kInvalidId) { 172 if (tab_id != kInvalidId) {
168 // We don't actually care about |window_num|, this is just a sanity check. 173 // We don't actually care about |window_num|, this is just a sanity check.
169 DCHECK_LT(kInvalidId, window_num); 174 DCHECK_LT(kInvalidId, window_num);
170 const SessionTab* tab; 175 const SessionTab* tab;
171 if (!associator->GetForeignTab(session_string_value, tab_id, &tab)) { 176 if (!associator->GetForeignTab(session_string_value, tab_id, &tab)) {
172 LOG(ERROR) << "Failed to load foreign tab."; 177 LOG(ERROR) << "Failed to load foreign tab.";
173 return; 178 return;
174 } 179 }
175 SessionRestore::RestoreForeignSessionTab(web_ui_->GetProfile(), *tab); 180 SessionRestore::RestoreForeignSessionTab(profile, *tab);
176 } else { 181 } else {
177 std::vector<SessionWindow*> windows; 182 std::vector<SessionWindow*> windows;
178 // Note: we don't own the ForeignSessions themselves. 183 // Note: we don't own the ForeignSessions themselves.
179 if (!associator->GetForeignSession(session_string_value, &windows)) { 184 if (!associator->GetForeignSession(session_string_value, &windows)) {
180 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" 185 LOG(ERROR) << "ForeignSessionHandler failed to get session data from"
181 "SessionModelAssociator."; 186 "SessionModelAssociator.";
182 return; 187 return;
183 } 188 }
184 std::vector<SessionWindow*>::const_iterator iter_begin = windows.begin() + 189 std::vector<SessionWindow*>::const_iterator iter_begin = windows.begin() +
185 ((window_num == kInvalidId) ? 0 : window_num); 190 ((window_num == kInvalidId) ? 0 : window_num);
186 std::vector<SessionWindow*>::const_iterator iter_end = 191 std::vector<SessionWindow*>::const_iterator iter_end =
187 ((window_num == kInvalidId) ? 192 ((window_num == kInvalidId) ?
188 std::vector<SessionWindow*>::const_iterator(windows.end()) : 193 std::vector<SessionWindow*>::const_iterator(windows.end()) :
189 iter_begin+1); 194 iter_begin + 1);
190 SessionRestore::RestoreForeignSessionWindows(web_ui_->GetProfile(), 195 SessionRestore::RestoreForeignSessionWindows(profile, iter_begin, iter_end);
191 iter_begin,
192 iter_end);
193 } 196 }
194 } 197 }
195 198
196 bool ForeignSessionHandler::SessionTabToValue( 199 bool ForeignSessionHandler::SessionTabToValue(
197 const SessionTab& tab, 200 const SessionTab& tab,
198 DictionaryValue* dictionary) { 201 DictionaryValue* dictionary) {
199 if (tab.navigations.empty()) 202 if (tab.navigations.empty())
200 return false; 203 return false;
201 int selected_index = tab.current_navigation_index; 204 int selected_index = tab.current_navigation_index;
202 selected_index = std::max( 205 selected_index = std::max(
(...skipping 30 matching lines...) Expand all
233 return false; 236 return false;
234 dictionary->SetString("type", "window"); 237 dictionary->SetString("type", "window");
235 dictionary->SetDouble("timestamp", 238 dictionary->SetDouble("timestamp",
236 static_cast<double>(window.timestamp.ToInternalValue())); 239 static_cast<double>(window.timestamp.ToInternalValue()));
237 dictionary->SetInteger("sessionId", window.window_id.id()); 240 dictionary->SetInteger("sessionId", window.window_id.id());
238 dictionary->Set("tabs", tab_values.release()); 241 dictionary->Set("tabs", tab_values.release());
239 return true; 242 return true;
240 } 243 }
241 244
242 } // namespace browser_sync 245 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698