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 #ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
      6 #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
      7 
      8 #include "base/memory/singleton.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/sessions/session_service.h"
     11 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
     12 
     13 // Singleton that owns all SessionServices and associates them with
     14 // Profiles. Listens for the Profile's destruction notification and cleans up
     15 // the associated SessionService.
     16 class SessionServiceFactory : public BrowserContextKeyedServiceFactory {
     17  public:
     18   // Returns the session service for |profile|. This may return NULL. If this
     19   // profile supports a session service (it isn't incognito), and the session
     20   // service hasn't yet been created, this forces creation of the session
     21   // service. This returns NULL if ShutdownForProfile has been called for
     22   // |profile|.
     23   //
     24   // This returns NULL if the profile is incognito. Callers should always check
     25   // the return value for NULL.
     26   static SessionService* GetForProfile(Profile* profile);
     27 
     28   // Returns the session service for |profile|, but do not create it if it
     29   // doesn't exist. This returns NULL if the profile is incognito or if session
     30   // service has been explicitly shutdown (browser is exiting). Callers should
     31   // always check the return value for NULL.
     32   static SessionService* GetForProfileIfExisting(Profile* profile);
     33 
     34   // Returns the session service for |profile|. This is the same as
     35   // GetForProfile, but will force creation of the session service even if
     36   // ShutdownForProfile has been called for |profile|.
     37   static SessionService* GetForProfileForSessionRestore(Profile* profile);
     38 
     39   // If |profile| has a session service, it is shut down. To properly record the
     40   // current state this forces creation of the session service, then shuts it
     41   // down.
     42   static void ShutdownForProfile(Profile* profile);
     43 
     44 #if defined(UNIT_TEST)
     45   // For test use: force setting of the session service for a given profile.
     46   // This will delete a previous session service for this profile if it exists.
     47   static void SetForTestProfile(Profile* profile, SessionService* service) {
     48     GetInstance()->BrowserContextShutdown(profile);
     49     GetInstance()->BrowserContextDestroyed(profile);
     50     GetInstance()->Associate(profile, service);
     51   }
     52 #endif
     53 
     54   static SessionServiceFactory* GetInstance();
     55 
     56  private:
     57   friend struct DefaultSingletonTraits<SessionServiceFactory>;
     58   FRIEND_TEST_ALL_PREFIXES(SessionCrashedInfoBarDelegateUnitTest,
     59                            DetachingTabWithCrashedInfoBar);
     60 
     61   SessionServiceFactory();
     62   virtual ~SessionServiceFactory();
     63 
     64   // BrowserContextKeyedServiceFactory:
     65   virtual KeyedService* BuildServiceInstanceFor(
     66       content::BrowserContext* profile) const OVERRIDE;
     67   virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
     68   virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
     69 };
     70 
     71 #endif  // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
     72