1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sessions/chrome_tab_restore_service_client.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_common_utils.h"
#include "chrome/common/url_constants.h"
#include "components/sessions/content/content_live_tab.h"
#include "components/tab_groups/tab_group_id.h"
#include "extensions/buildflags/buildflags.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
#include "chrome/browser/sessions/exit_type_service.h"
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_service_factory.h"
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/apps/app_service/web_contents_app_id_utils.h"
#include "chrome/browser/apps/platform_apps/platform_app_launch.h"
#endif
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser_live_tab_context.h"
#else
#include "chrome/browser/ui/android/tab_model/android_live_tab_context.h"
#endif
ChromeTabRestoreServiceClient::ChromeTabRestoreServiceClient(Profile* profile)
: profile_(profile) {}
ChromeTabRestoreServiceClient::~ChromeTabRestoreServiceClient() = default;
sessions::LiveTabContext* ChromeTabRestoreServiceClient::CreateLiveTabContext(
sessions::LiveTabContext* existing_context,
sessions::SessionWindow::WindowType type,
const std::string& app_name,
const gfx::Rect& bounds,
ui::mojom::WindowShowState show_state,
const std::string& workspace,
const std::string& user_title,
const std::map<std::string, std::string>& extra_data) {
#if BUILDFLAG(IS_ANDROID)
// Android does not support creating a LiveTabContext here. Return the
// existing context instead.
DCHECK(existing_context);
return existing_context;
#else
return BrowserLiveTabContext::Create(profile_, type, app_name, bounds,
show_state, workspace, user_title,
extra_data);
#endif
}
sessions::LiveTabContext*
ChromeTabRestoreServiceClient::FindLiveTabContextForTab(
const sessions::LiveTab* tab) {
#if BUILDFLAG(IS_ANDROID)
return AndroidLiveTabContext::FindContextForWebContents(
static_cast<const sessions::ContentLiveTab*>(tab)->web_contents());
#else
return BrowserLiveTabContext::FindContextForWebContents(
static_cast<const sessions::ContentLiveTab*>(tab)->web_contents());
#endif
}
sessions::LiveTabContext*
ChromeTabRestoreServiceClient::FindLiveTabContextWithID(SessionID desired_id) {
#if BUILDFLAG(IS_ANDROID)
return AndroidLiveTabContext::FindContextWithID(desired_id);
#else
return BrowserLiveTabContext::FindContextWithID(desired_id);
#endif
}
sessions::LiveTabContext*
ChromeTabRestoreServiceClient::FindLiveTabContextWithGroup(
tab_groups::TabGroupId group) {
#if BUILDFLAG(IS_ANDROID)
return nullptr;
#else
return BrowserLiveTabContext::FindContextWithGroup(group, profile_);
#endif
}
bool ChromeTabRestoreServiceClient::ShouldTrackURLForRestore(const GURL& url) {
return ::ShouldTrackURLForRestore(url);
}
std::string ChromeTabRestoreServiceClient::GetExtensionAppIDForTab(
sessions::LiveTab* tab) {
std::string app_id;
#if BUILDFLAG(ENABLE_EXTENSIONS)
app_id = apps::GetAppIdForWebContents(
static_cast<sessions::ContentLiveTab*>(tab)->web_contents());
#endif
return app_id;
}
base::FilePath ChromeTabRestoreServiceClient::GetPathToSaveTo() {
return profile_->GetPath();
}
GURL ChromeTabRestoreServiceClient::GetNewTabURL() {
return GURL(chrome::kChromeUINewTabURL);
}
bool ChromeTabRestoreServiceClient::HasLastSession() {
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
SessionService* session_service =
SessionServiceFactory::GetForProfile(profile_);
ExitType exit_type = ExitTypeService::GetLastSessionExitType(profile_);
// The previous session crashed and wasn't restored, or was a forced
// shutdown. Both of which won't have notified us of the browser close so
// that we need to load the windows from session service (which will have
// saved them).
return (!profile_->restored_last_session() && session_service &&
(exit_type == ExitType::kCrashed ||
exit_type == ExitType::kForcedShutdown));
#else
return false;
#endif
}
void ChromeTabRestoreServiceClient::GetLastSession(
sessions::GetLastSessionCallback callback) {
DCHECK(HasLastSession());
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
SessionServiceFactory::GetForProfile(profile_)->GetLastSession(
std::move(callback));
#endif
}
void ChromeTabRestoreServiceClient::OnTabRestored(const GURL& url) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
apps::RecordExtensionAppLaunchOnTabRestored(profile_, url);
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
}
|