1 // Copyright 2020 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.
4 
5 #include "base/android/jni_android.h"
6 #include "base/logging.h"
7 #include "chrome/android/chrome_jni_headers/HistoricalTabSaver_jni.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sessions/tab_restore_service_factory.h"
10 #include "chrome/common/url_constants.h"
11 #include "components/sessions/content/content_live_tab.h"
12 #include "components/sessions/core/tab_restore_service.h"
13 #include "content/public/browser/web_contents.h"
14 
15 using base::android::JavaParamRef;
16 
17 namespace {
18 
CreateHistoricalTab(content::WebContents * web_contents)19 void CreateHistoricalTab(content::WebContents* web_contents) {
20   DCHECK(web_contents);
21 
22   sessions::TabRestoreService* service =
23       TabRestoreServiceFactory::GetForProfile(
24           Profile::FromBrowserContext(web_contents->GetBrowserContext()));
25   if (!service)
26     return;
27 
28   // Exclude internal pages from being marked as recent when they are closed.
29   const GURL& tab_url = web_contents->GetURL();
30   if (tab_url.SchemeIs(content::kChromeUIScheme) ||
31       tab_url.SchemeIs(chrome::kChromeNativeScheme) ||
32       tab_url.SchemeIs(url::kAboutScheme)) {
33     return;
34   }
35 
36   // TODO(jcivelli): is the index important?
37   service->CreateHistoricalTab(
38       sessions::ContentLiveTab::GetForWebContents(web_contents), -1);
39 }
40 }  // anonymous namespace
41 
42 // Static JNI methods.
43 
44 // static
JNI_HistoricalTabSaver_CreateHistoricalTabFromContents(JNIEnv * env,const JavaParamRef<jobject> & jweb_contents)45 static void JNI_HistoricalTabSaver_CreateHistoricalTabFromContents(
46     JNIEnv* env,
47     const JavaParamRef<jobject>& jweb_contents) {
48   auto* web_contents = content::WebContents::FromJavaWebContents(jweb_contents);
49   if (web_contents)
50     CreateHistoricalTab(web_contents);
51 }
52