Home | History | Annotate | Download | only in sessions
      1 // Copyright (c) 2012 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/sessions/session_service_factory.h"
      6 
      7 #include "chrome/browser/profiles/profile.h"
      8 #include "chrome/browser/sessions/session_data_deleter.h"
      9 #include "chrome/browser/sessions/session_service.h"
     10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
     11 
     12 // static
     13 SessionService* SessionServiceFactory::GetForProfile(Profile* profile) {
     14 #if defined(OS_ANDROID)
     15   // For Android we do not store sessions in the SessionService.
     16   return NULL;
     17 #else
     18   return static_cast<SessionService*>(
     19       GetInstance()->GetServiceForBrowserContext(profile, true));
     20 #endif
     21 }
     22 
     23 // static
     24 SessionService* SessionServiceFactory::GetForProfileIfExisting(
     25     Profile* profile) {
     26 #if defined(OS_ANDROID)
     27   // For Android we do not store sessions in the SessionService.
     28   return NULL;
     29 #else
     30   return static_cast<SessionService*>(
     31       GetInstance()->GetServiceForBrowserContext(profile, false));
     32 #endif
     33 }
     34 
     35 // static
     36 SessionService* SessionServiceFactory::GetForProfileForSessionRestore(
     37     Profile* profile) {
     38   SessionService* service = GetForProfile(profile);
     39   if (!service) {
     40     // If the service has been shutdown, remove the reference to NULL for
     41     // |profile| so GetForProfile will recreate it.
     42     GetInstance()->Disassociate(profile);
     43     service = GetForProfile(profile);
     44   }
     45   return service;
     46 }
     47 
     48 // static
     49 void SessionServiceFactory::ShutdownForProfile(Profile* profile) {
     50   DeleteSessionOnlyData(profile);
     51 
     52   // We're about to exit, force creation of the session service if it hasn't
     53   // been created yet. We do this to ensure session state matches the point in
     54   // time the user exited.
     55   SessionServiceFactory* factory = GetInstance();
     56   factory->GetServiceForBrowserContext(profile, true);
     57 
     58   // Shut down and remove the reference to the session service, and replace it
     59   // with an explicit NULL to prevent it being recreated on the next access.
     60   factory->BrowserContextShutdown(profile);
     61   factory->BrowserContextDestroyed(profile);
     62   factory->Associate(profile, NULL);
     63 }
     64 
     65 SessionServiceFactory* SessionServiceFactory::GetInstance() {
     66   return Singleton<SessionServiceFactory>::get();
     67 }
     68 
     69 SessionServiceFactory::SessionServiceFactory()
     70     : BrowserContextKeyedServiceFactory(
     71         "SessionService",
     72         BrowserContextDependencyManager::GetInstance()) {
     73 }
     74 
     75 SessionServiceFactory::~SessionServiceFactory() {
     76 }
     77 
     78 KeyedService* SessionServiceFactory::BuildServiceInstanceFor(
     79     content::BrowserContext* profile) const {
     80   SessionService* service = NULL;
     81   service = new SessionService(static_cast<Profile*>(profile));
     82   service->ResetFromCurrentBrowsers();
     83   return service;
     84 }
     85 
     86 bool SessionServiceFactory::ServiceIsCreatedWithBrowserContext() const {
     87   return true;
     88 }
     89 
     90 bool SessionServiceFactory::ServiceIsNULLWhileTesting() const {
     91   return true;
     92 }
     93