1 // Copyright 2015 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.
5 #include "chrome/browser/sessions/chrome_tab_restore_service_client.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/sessions/session_service.h"
9 #include "chrome/browser/sessions/session_service_factory.h"
10 #include "chrome/common/url_constants.h"
11 #include "components/sessions/content/content_live_tab.h"
12 #include "content/public/browser/browser_thread.h"
14 #if defined(ENABLE_EXTENSIONS)
15 #include "chrome/browser/extensions/tab_helper.h"
16 #include "chrome/common/extensions/extension_constants.h"
17 #include "chrome/common/extensions/extension_metrics.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h"
23 #if !defined(OS_ANDROID)
24 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
29 void RecordAppLaunch(Profile
* profile
, const GURL
& url
) {
30 #if defined(ENABLE_EXTENSIONS)
31 const extensions::Extension
* extension
=
32 extensions::ExtensionRegistry::Get(profile
)
33 ->enabled_extensions()
38 extensions::RecordAppLaunchType(
39 extension_misc::APP_LAUNCH_NTP_RECENTLY_CLOSED
, extension
->GetType());
40 #endif // defined(ENABLE_EXTENSIONS)
45 ChromeTabRestoreServiceClient::ChromeTabRestoreServiceClient(Profile
* profile
)
46 : profile_(profile
) {}
48 ChromeTabRestoreServiceClient::~ChromeTabRestoreServiceClient() {}
50 sessions::TabRestoreServiceDelegate
*
51 ChromeTabRestoreServiceClient::CreateTabRestoreServiceDelegate(
52 int host_desktop_type
,
53 const std::string
& app_name
) {
54 #if defined(OS_ANDROID)
55 // Android does not support TabRestoreServiceDelegate, as tab persistence
56 // is implemented on the Java side.
59 return BrowserTabRestoreServiceDelegate::Create(
60 profile_
, static_cast<chrome::HostDesktopType
>(host_desktop_type
),
65 sessions::TabRestoreServiceDelegate
*
66 ChromeTabRestoreServiceClient::FindTabRestoreServiceDelegateForTab(
67 const sessions::LiveTab
* tab
) {
68 #if defined(OS_ANDROID)
69 // Android does not support TabRestoreServiceDelegate, as tab persistence
70 // is implemented on the Java side.
73 return BrowserTabRestoreServiceDelegate::FindDelegateForWebContents(
74 static_cast<const sessions::ContentLiveTab
*>(tab
)->web_contents());
78 sessions::TabRestoreServiceDelegate
*
79 ChromeTabRestoreServiceClient::FindTabRestoreServiceDelegateWithID(
80 SessionID::id_type desired_id
,
81 int host_desktop_type
) {
82 #if defined(OS_ANDROID)
83 // Android does not support TabRestoreServiceDelegate, as tab persistence
84 // is implemented on the Java side.
87 return BrowserTabRestoreServiceDelegate::FindDelegateWithID(
88 desired_id
, static_cast<chrome::HostDesktopType
>(host_desktop_type
));
92 bool ChromeTabRestoreServiceClient::ShouldTrackURLForRestore(const GURL
& url
) {
93 return ::ShouldTrackURLForRestore(url
);
96 std::string
ChromeTabRestoreServiceClient::GetExtensionAppIDForTab(
97 sessions::LiveTab
* tab
) {
98 std::string extension_app_id
;
100 #if defined(ENABLE_EXTENSIONS)
101 extensions::TabHelper
* extensions_tab_helper
=
102 extensions::TabHelper::FromWebContents(
103 static_cast<sessions::ContentLiveTab
*>(tab
)->web_contents());
104 // extensions_tab_helper is NULL in some browser tests.
105 if (extensions_tab_helper
) {
106 const extensions::Extension
* extension
=
107 extensions_tab_helper
->extension_app();
109 extension_app_id
= extension
->id();
113 return extension_app_id
;
116 base::SequencedWorkerPool
* ChromeTabRestoreServiceClient::GetBlockingPool() {
117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
118 return content::BrowserThread::GetBlockingPool();
121 base::FilePath
ChromeTabRestoreServiceClient::GetPathToSaveTo() {
122 return profile_
->GetPath();
125 GURL
ChromeTabRestoreServiceClient::GetNewTabURL() {
126 return GURL(chrome::kChromeUINewTabURL
);
129 bool ChromeTabRestoreServiceClient::HasLastSession() {
130 #if defined(ENABLE_SESSION_SERVICE)
131 SessionService
* session_service
=
132 SessionServiceFactory::GetForProfile(profile_
);
133 Profile::ExitType exit_type
= profile_
->GetLastSessionExitType();
134 // The previous session crashed and wasn't restored, or was a forced
135 // shutdown. Both of which won't have notified us of the browser close so
136 // that we need to load the windows from session service (which will have
138 return (!profile_
->restored_last_session() && session_service
&&
139 (exit_type
== Profile::EXIT_CRASHED
||
140 exit_type
== Profile::EXIT_SESSION_ENDED
));
146 void ChromeTabRestoreServiceClient::GetLastSession(
147 const sessions::GetLastSessionCallback
& callback
,
148 base::CancelableTaskTracker
* tracker
) {
149 DCHECK(HasLastSession());
150 #if defined(ENABLE_SESSION_SERVICE)
151 SessionServiceFactory::GetForProfile(profile_
)
152 ->GetLastSession(callback
, tracker
);
156 void ChromeTabRestoreServiceClient::OnTabRestored(const GURL
& url
) {
157 RecordAppLaunch(profile_
, url
);