BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / sessions / chrome_tab_restore_service_client.cc
bloba063b3ffbe6654df3532ed2f824dc52fb498af70
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"
22 #endif
24 #if !defined(OS_ANDROID)
25 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
26 #endif
28 namespace {
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()
35 .GetAppByURL(url);
36 if (!extension)
37 return;
39 extensions::RecordAppLaunchType(
40 extension_misc::APP_LAUNCH_NTP_RECENTLY_CLOSED, extension->GetType());
41 #endif // defined(ENABLE_EXTENSIONS)
44 } // namespace
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.
58 return nullptr;
59 #else
60 return BrowserTabRestoreServiceDelegate::Create(
61 profile_, static_cast<chrome::HostDesktopType>(host_desktop_type),
62 app_name);
63 #endif
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.
72 return nullptr;
73 #else
74 return BrowserTabRestoreServiceDelegate::FindDelegateForWebContents(
75 static_cast<const sessions::ContentLiveTab*>(tab)->web_contents());
76 #endif
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.
86 return nullptr;
87 #else
88 return BrowserTabRestoreServiceDelegate::FindDelegateWithID(
89 desired_id, static_cast<chrome::HostDesktopType>(host_desktop_type));
90 #endif
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();
109 if (extension)
110 extension_app_id = extension->id();
112 #endif
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
144 // saved them).
145 return (!profile_->restored_last_session() && session_service &&
146 (exit_type == Profile::EXIT_CRASHED ||
147 exit_type == Profile::EXIT_SESSION_ENDED));
148 #else
149 return false;
150 #endif
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);
160 #endif
163 void ChromeTabRestoreServiceClient::OnTabRestored(const GURL& url) {
164 RecordAppLaunch(profile_, url);