15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_service.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Singleton that owns all SessionServices and associates them with
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Profiles. Listens for the Profile's destruction notification and cleans up
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the associated SessionService.
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class SessionServiceFactory : public BrowserContextKeyedServiceFactory {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the session service for |profile|. This may return NULL. If this
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profile supports a session service (it isn't incognito), and the session
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // service hasn't yet been created, this forces creation of the session
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // service. This returns NULL if ShutdownForProfile has been called for
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // |profile|.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This returns NULL if the profile is incognito. Callers should always check
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the return value for NULL.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static SessionService* GetForProfile(Profile* profile);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the session service for |profile|, but do not create it if it
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // doesn't exist. This returns NULL if the profile is incognito or if session
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // service has been explicitly shutdown (browser is exiting). Callers should
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // always check the return value for NULL.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static SessionService* GetForProfileIfExisting(Profile* profile);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns the session service for |profile|. This is the same as
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // GetForProfile, but will force creation of the session service even if
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // ShutdownForProfile has been called for |profile|.
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  static SessionService* GetForProfileForSessionRestore(Profile* profile);
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |profile| has a session service, it is shut down. To properly record the
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // current state this forces creation of the session service, then shuts it
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // down.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void ShutdownForProfile(Profile* profile);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(UNIT_TEST)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For test use: force setting of the session service for a given profile.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This will delete a previous session service for this profile if it exists.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void SetForTestProfile(Profile* profile, SessionService* service) {
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetInstance()->BrowserContextShutdown(profile);
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetInstance()->BrowserContextDestroyed(profile);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetInstance()->Associate(profile, service);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static SessionServiceFactory* GetInstance();
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct DefaultSingletonTraits<SessionServiceFactory>;
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SessionCrashedInfoBarDelegateUnitTest,
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           DetachingTabWithCrashedInfoBar);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionServiceFactory();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~SessionServiceFactory();
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // BrowserContextKeyedServiceFactory:
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual KeyedService* BuildServiceInstanceFor(
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::BrowserContext* profile) const OVERRIDE;
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_FACTORY_H_
72