1 // Copyright 2014 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/sync/sessions/session_data_type_controller.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
10 #include "chrome/browser/sync/glue/synced_window_delegate.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_source.h"
15
16 using content::BrowserThread;
17
18 namespace browser_sync {
19
SessionDataTypeController(SyncApiComponentFactory * sync_factory,Profile * profile,const DisableTypeCallback & disable_callback)20 SessionDataTypeController::SessionDataTypeController(
21 SyncApiComponentFactory* sync_factory,
22 Profile* profile,
23 const DisableTypeCallback& disable_callback)
24 : UIDataTypeController(
25 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
26 base::Bind(&ChromeReportUnrecoverableError),
27 disable_callback,
28 syncer::SESSIONS,
29 sync_factory),
30 profile_(profile) {
31 }
32
~SessionDataTypeController()33 SessionDataTypeController::~SessionDataTypeController() {}
34
StartModels()35 bool SessionDataTypeController::StartModels() {
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
37 std::set<browser_sync::SyncedWindowDelegate*> window =
38 browser_sync::SyncedWindowDelegate::GetSyncedWindowDelegates();
39 for (std::set<browser_sync::SyncedWindowDelegate*>::const_iterator i =
40 window.begin(); i != window.end(); ++i) {
41 if ((*i)->IsSessionRestoreInProgress()) {
42 notification_registrar_.Add(
43 this,
44 chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE,
45 content::Source<Profile>(profile_));
46 return false;
47 }
48 }
49 return true;
50 }
51
StopModels()52 void SessionDataTypeController::StopModels() {
53 notification_registrar_.RemoveAll();
54 }
55
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)56 void SessionDataTypeController::Observe(
57 int type,
58 const content::NotificationSource& source,
59 const content::NotificationDetails& details) {
60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
61 DCHECK_EQ(chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE, type);
62 DCHECK_EQ(profile_, content::Source<Profile>(source).ptr());
63 notification_registrar_.RemoveAll();
64 OnModelLoaded();
65 }
66
67 } // namespace browser_sync
68