Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / sessions / session_data_type_controller.cc
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 "chrome/browser/sync/sessions/synced_window_delegates_getter.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/notification_source.h"
16
17 using content::BrowserThread;
18
19 namespace browser_sync {
20
21 SessionDataTypeController::SessionDataTypeController(
22     sync_driver::SyncApiComponentFactory* sync_factory,
23     Profile* profile,
24     SyncedWindowDelegatesGetter* synced_window_getter,
25     LocalDeviceInfoProvider* local_device)
26     : UIDataTypeController(
27           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
28           base::Bind(&ChromeReportUnrecoverableError),
29           syncer::SESSIONS,
30           sync_factory),
31       profile_(profile),
32       synced_window_getter_(synced_window_getter),
33       local_device_(local_device),
34       waiting_on_session_restore_(false),
35       waiting_on_local_device_info_(false) {
36   DCHECK(local_device_);
37 }
38
39 SessionDataTypeController::~SessionDataTypeController() {}
40
41 bool SessionDataTypeController::StartModels() {
42   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
43   std::set<browser_sync::SyncedWindowDelegate*> window =
44       synced_window_getter_->GetSyncedWindowDelegates();
45   for (std::set<browser_sync::SyncedWindowDelegate*>::const_iterator i =
46       window.begin(); i != window.end(); ++i) {
47     if ((*i)->IsSessionRestoreInProgress()) {
48       notification_registrar_.Add(
49           this,
50           chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE,
51           content::Source<Profile>(profile_));
52       waiting_on_session_restore_ = true;
53       break;
54     }
55   }
56
57   if (!local_device_->GetLocalDeviceInfo()) {
58     subscription_ = local_device_->RegisterOnInitializedCallback(
59         base::Bind(&SessionDataTypeController::OnLocalDeviceInfoInitialized,
60                    this));
61     waiting_on_local_device_info_ = true;
62   }
63
64   return !IsWaiting();
65 }
66
67 void SessionDataTypeController::StopModels() {
68   notification_registrar_.RemoveAll();
69 }
70
71 bool SessionDataTypeController::IsWaiting() {
72   return waiting_on_session_restore_ || waiting_on_local_device_info_;
73 }
74
75 void SessionDataTypeController::MaybeCompleteLoading() {
76   if (state_ == MODEL_STARTING && !IsWaiting()) {
77     OnModelLoaded();
78   }
79 }
80
81 void SessionDataTypeController::Observe(
82     int type,
83     const content::NotificationSource& source,
84     const content::NotificationDetails& details) {
85   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
86   DCHECK_EQ(chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE, type);
87   DCHECK_EQ(profile_, content::Source<Profile>(source).ptr());
88   notification_registrar_.RemoveAll();
89
90   waiting_on_session_restore_ = false;
91   MaybeCompleteLoading();
92 }
93
94 void SessionDataTypeController::OnLocalDeviceInfoInitialized() {
95   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96   subscription_.reset();
97
98   waiting_on_local_device_info_ = false;
99   MaybeCompleteLoading();
100 }
101
102 }  // namespace browser_sync