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