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 "components/sessions/content/content_tab_client_data.h"
13 #include "content/public/browser/browser_thread.h"
15 #if defined(ENABLE_EXTENSIONS)
16 #include "chrome/browser/extensions/tab_helper.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/common/extensions/extension_metrics.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_set.h"
24 #if !defined(OS_ANDROID)
25 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
30 void RecordAppLaunch(Profile
* profile
, const GURL
& url
) {
31 #if defined(ENABLE_EXTENSIONS)
32 const extensions::Extension
* extension
=
33 extensions::ExtensionRegistry::Get(profile
)
34 ->enabled_extensions()
39 extensions::RecordAppLaunchType(
40 extension_misc::APP_LAUNCH_NTP_RECENTLY_CLOSED
, extension
->GetType());
41 #endif // defined(ENABLE_EXTENSIONS)
46 ChromeTabRestoreServiceClient::ChromeTabRestoreServiceClient(Profile
* profile
)
47 : profile_(profile
) {}
49 ChromeTabRestoreServiceClient::~ChromeTabRestoreServiceClient() {}
51 sessions::TabRestoreServiceDelegate
*
52 ChromeTabRestoreServiceClient::CreateTabRestoreServiceDelegate(
53 int host_desktop_type
,
54 const std::string
& app_name
) {
55 #if defined(OS_ANDROID)
56 // Android does not support TabRestoreServiceDelegate, as tab persistence
57 // is implemented on the Java side.
60 return BrowserTabRestoreServiceDelegate::Create(
61 profile_
, static_cast<chrome::HostDesktopType
>(host_desktop_type
),
66 sessions::TabRestoreServiceDelegate
*
67 ChromeTabRestoreServiceClient::FindTabRestoreServiceDelegateForTab(
68 const sessions::LiveTab
* tab
) {
69 #if defined(OS_ANDROID)
70 // Android does not support TabRestoreServiceDelegate, as tab persistence
71 // is implemented on the Java side.
74 return BrowserTabRestoreServiceDelegate::FindDelegateForWebContents(
75 static_cast<const sessions::ContentLiveTab
*>(tab
)->web_contents());
79 sessions::TabRestoreServiceDelegate
*
80 ChromeTabRestoreServiceClient::FindTabRestoreServiceDelegateWithID(
81 SessionID::id_type desired_id
,
82 int host_desktop_type
) {
83 #if defined(OS_ANDROID)
84 // Android does not support TabRestoreServiceDelegate, as tab persistence
85 // is implemented on the Java side.
88 return BrowserTabRestoreServiceDelegate::FindDelegateWithID(
89 desired_id
, static_cast<chrome::HostDesktopType
>(host_desktop_type
));
93 bool ChromeTabRestoreServiceClient::ShouldTrackURLForRestore(const GURL
& url
) {
94 return ::ShouldTrackURLForRestore(url
);
97 std::string
ChromeTabRestoreServiceClient::GetExtensionAppIDForTab(
98 sessions::LiveTab
* tab
) {
99 std::string extension_app_id
;
101 #if defined(ENABLE_EXTENSIONS)
102 extensions::TabHelper
* extensions_tab_helper
=
103 extensions::TabHelper::FromWebContents(
104 static_cast<sessions::ContentLiveTab
*>(tab
)->web_contents());
105 // extensions_tab_helper is NULL in some browser tests.
106 if (extensions_tab_helper
) {
107 const extensions::Extension
* extension
=
108 extensions_tab_helper
->extension_app();
110 extension_app_id
= extension
->id();
114 return extension_app_id
;
117 scoped_ptr
<sessions::TabClientData
>
118 ChromeTabRestoreServiceClient::GetTabClientDataForTab(sessions::LiveTab
* tab
) {
119 return make_scoped_ptr(new sessions::ContentTabClientData(
120 static_cast<sessions::ContentLiveTab
*>(tab
)->web_contents()));
123 base::SequencedWorkerPool
* ChromeTabRestoreServiceClient::GetBlockingPool() {
124 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
125 return content::BrowserThread::GetBlockingPool();
128 base::FilePath
ChromeTabRestoreServiceClient::GetPathToSaveTo() {
129 return profile_
->GetPath();
132 GURL
ChromeTabRestoreServiceClient::GetNewTabURL() {
133 return GURL(chrome::kChromeUINewTabURL
);
136 bool ChromeTabRestoreServiceClient::HasLastSession() {
137 #if defined(ENABLE_SESSION_SERVICE)
138 SessionService
* session_service
=
139 SessionServiceFactory::GetForProfile(profile_
);
140 Profile::ExitType exit_type
= profile_
->GetLastSessionExitType();
141 // The previous session crashed and wasn't restored, or was a forced
142 // shutdown. Both of which won't have notified us of the browser close so
143 // that we need to load the windows from session service (which will have
145 return (!profile_
->restored_last_session() && session_service
&&
146 (exit_type
== Profile::EXIT_CRASHED
||
147 exit_type
== Profile::EXIT_SESSION_ENDED
));
153 void ChromeTabRestoreServiceClient::GetLastSession(
154 const sessions::GetLastSessionCallback
& callback
,
155 base::CancelableTaskTracker
* tracker
) {
156 DCHECK(HasLastSession());
157 #if defined(ENABLE_SESSION_SERVICE)
158 SessionServiceFactory::GetForProfile(profile_
)
159 ->GetLastSession(callback
, tracker
);
163 void ChromeTabRestoreServiceClient::OnTabRestored(const GURL
& url
) {
164 RecordAppLaunch(profile_
, url
);