1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/dom_ui/foreign_session_handler.h"
#include "base/scoped_vector.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/dom_ui/value_helper.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/tab_restore_service.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
namespace browser_sync {
static const int kMaxSessionsToShow = 10;
ForeignSessionHandler::ForeignSessionHandler() {
Init();
}
void ForeignSessionHandler::RegisterMessages() {
dom_ui_->RegisterMessageCallback("getForeignSessions",
NewCallback(this,
&ForeignSessionHandler::HandleGetForeignSessions));
dom_ui_->RegisterMessageCallback("reopenForeignSession",
NewCallback(this,
&ForeignSessionHandler::HandleReopenForeignSession));
}
void ForeignSessionHandler::Init() {
registrar_.Add(this, NotificationType::SYNC_CONFIGURE_DONE,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::FOREIGN_SESSION_UPDATED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::FOREIGN_SESSION_DELETED,
NotificationService::AllSources());
}
void ForeignSessionHandler::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type != NotificationType::SYNC_CONFIGURE_DONE &&
type != NotificationType::FOREIGN_SESSION_UPDATED &&
type != NotificationType::FOREIGN_SESSION_DELETED) {
NOTREACHED();
return;
}
ListValue list_value;
HandleGetForeignSessions(&list_value);
}
SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() {
ProfileSyncService* service = dom_ui_->GetProfile()->GetProfileSyncService();
if (service == NULL)
return NULL;
// We only want to set the model associator if there is one, and it is done
// syncing sessions.
SessionModelAssociator* model_associator = service->
GetSessionModelAssociator();
if (model_associator == NULL ||
!service->ShouldPushChanges()) {
return NULL;
}
return dom_ui_->GetProfile()->GetProfileSyncService()->
GetSessionModelAssociator();
}
void ForeignSessionHandler::HandleGetForeignSessions(const Value* content) {
SessionModelAssociator* associator = GetModelAssociator();
if (associator)
GetForeignSessions(associator);
}
void ForeignSessionHandler::HandleReopenForeignSession(
const Value* content) {
// Extract the machine tag and use it to obtain the id for the node we are
// looking for. Send it along with a valid associator to OpenForeignSessions.
if (content->GetType() == Value::TYPE_LIST) {
const ListValue* list_value = static_cast<const ListValue*>(content);
Value* list_member;
if (list_value->Get(0, &list_member) &&
list_member->GetType() == Value::TYPE_STRING) {
const StringValue* string_value =
static_cast<const StringValue*>(list_member);
std::string session_string_value;
if (string_value->GetAsString(&session_string_value)) {
SessionModelAssociator* associator = GetModelAssociator();
if (associator) {
int64 id = associator->GetSyncIdFromChromeId(session_string_value);
OpenForeignSession(associator, id);
}
}
}
}
}
void ForeignSessionHandler::OpenForeignSession(
SessionModelAssociator* associator, int64 id) {
// Obtain the session windows for the foreign session.
// We don't have a ForeignSessionHandler in off the record mode, so we
// expect the ProfileSyncService to exist.
sync_api::ReadTransaction trans(dom_ui_->GetProfile()->
GetProfileSyncService()->backend()->GetUserShareHandle());
ScopedVector<ForeignSession> session;
associator->AppendForeignSessionWithID(id, &session.get(), &trans);
DCHECK(session.size() == 1);
std::vector<SessionWindow*> windows = (*session.begin())->windows;
SessionRestore::RestoreForeignSessionWindows(dom_ui_->GetProfile(), &windows);
}
void ForeignSessionHandler::GetForeignSessions(
SessionModelAssociator* associator) {
ScopedVector<ForeignSession> windows;
associator->GetSessionDataFromSyncModel(&windows.get());
int added_count = 0;
ListValue list_value;
for (std::vector<ForeignSession*>::const_iterator i =
windows.begin(); i != windows.end() &&
added_count < kMaxSessionsToShow; ++i) {
ForeignSession* foreign_session = *i;
std::vector<TabRestoreService::Entry*> entries;
dom_ui_->GetProfile()->GetTabRestoreService()->CreateEntriesFromWindows(
&foreign_session->windows, &entries);
for (std::vector<TabRestoreService::Entry*>::const_iterator it =
entries.begin(); it != entries.end(); ++it) {
TabRestoreService::Entry* entry = *it;
scoped_ptr<DictionaryValue> value(new DictionaryValue());
if (entry->type == TabRestoreService::WINDOW &&
ValueHelper::WindowToValue(
*static_cast<TabRestoreService::Window*>(entry), value.get())) {
// The javascript checks if the session id is a valid session id,
// when rendering session information to the new tab page, and it
// sends the sessionTag back when we need to restore a session.
value->SetString("sessionTag", foreign_session->foreign_tession_tag);
value->SetInteger("sessionId", entry->id);
list_value.Append(value.release()); // Give ownership to |list_value|.
}
}
added_count++;
}
dom_ui_->CallJavascriptFunction(L"foreignSessions", list_value);
}
} // namespace browser_sync
|