1 // Copyright (c) 2012 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 "chrome/browser/sessions/session_restore.h"
6
7 #include <vector>
8
9 #include "chrome/browser/android/tab_android.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/android/tab_model/tab_model.h"
12 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
13 #include "components/sessions/content/content_serialized_navigation_builder.h"
14 #include "components/sessions/core/session_types.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/restore_type.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_delegate.h"
19
20 // The android implementation does not do anything "foreign session" specific.
21 // We use it to restore tabs from "recently closed" too.
22 // static
RestoreForeignSessionTab(content::WebContents * web_contents,const sessions::SessionTab & session_tab,WindowOpenDisposition disposition)23 content::WebContents* SessionRestore::RestoreForeignSessionTab(
24 content::WebContents* web_contents,
25 const sessions::SessionTab& session_tab,
26 WindowOpenDisposition disposition) {
27 DCHECK(session_tab.navigations.size() > 0);
28 content::BrowserContext* context = web_contents->GetBrowserContext();
29 Profile* profile = Profile::FromBrowserContext(context);
30 TabModel* tab_model = TabModelList::GetTabModelForWebContents(web_contents);
31 DCHECK(tab_model);
32 std::vector<std::unique_ptr<content::NavigationEntry>> entries =
33 sessions::ContentSerializedNavigationBuilder::ToNavigationEntries(
34 session_tab.navigations, profile);
35 std::unique_ptr<content::WebContents> new_web_contents =
36 content::WebContents::Create(content::WebContents::CreateParams(context));
37 content::WebContents* raw_new_web_contents = new_web_contents.get();
38 int selected_index = session_tab.normalized_navigation_index();
39 new_web_contents->GetController().Restore(
40 selected_index, content::RestoreType::LAST_SESSION_EXITED_CLEANLY,
41 &entries);
42
43 TabAndroid* current_tab = TabAndroid::FromWebContents(web_contents);
44 DCHECK(current_tab);
45 if (disposition == WindowOpenDisposition::CURRENT_TAB) {
46 current_tab->SwapWebContents(std::move(new_web_contents), false, false);
47 } else {
48 DCHECK(disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB ||
49 disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB);
50 tab_model->CreateTab(current_tab, new_web_contents.release());
51 }
52 return raw_new_web_contents;
53 }
54
55 // static
RestoreForeignSessionWindows(Profile * profile,std::vector<const sessions::SessionWindow * >::const_iterator begin,std::vector<const sessions::SessionWindow * >::const_iterator end)56 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows(
57 Profile* profile,
58 std::vector<const sessions::SessionWindow*>::const_iterator begin,
59 std::vector<const sessions::SessionWindow*>::const_iterator end) {
60 NOTREACHED();
61 return std::vector<Browser*>();
62 }
63