Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sessions / chrome_tab_restore_service_client.cc
blob8cd6fd6e9ffa2ee29a38127966c2fc044b52399b
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"
21 #endif
23 #if !defined(OS_ANDROID)
24 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
25 #endif
27 namespace {
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()
34 .GetAppByURL(url);
35 if (!extension)
36 return;
38 extensions::RecordAppLaunchType(
39 extension_misc::APP_LAUNCH_NTP_RECENTLY_CLOSED, extension->GetType());
40 #endif // defined(ENABLE_EXTENSIONS)
43 } // namespace
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.
57 return nullptr;
58 #else
59 return BrowserTabRestoreServiceDelegate::Create(
60 profile_, static_cast<chrome::HostDesktopType>(host_desktop_type),
61 app_name);
62 #endif
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.
71 return nullptr;
72 #else
73 return BrowserTabRestoreServiceDelegate::FindDelegateForWebContents(
74 static_cast<const sessions::ContentLiveTab*>(tab)->web_contents());
75 #endif
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.
85 return nullptr;
86 #else
87 return BrowserTabRestoreServiceDelegate::FindDelegateWithID(
88 desired_id, static_cast<chrome::HostDesktopType>(host_desktop_type));
89 #endif
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();
108 if (extension)
109 extension_app_id = extension->id();
111 #endif
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
137 // saved them).
138 return (!profile_->restored_last_session() && session_service &&
139 (exit_type == Profile::EXIT_CRASHED ||
140 exit_type == Profile::EXIT_SESSION_ENDED));
141 #else
142 return false;
143 #endif
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);
153 #endif
156 void ChromeTabRestoreServiceClient::OnTabRestored(const GURL& url) {
157 RecordAppLaunch(profile_, url);