OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/android/tab_android.h" |
| 6 #include "chrome/browser/profiles/profile.h" |
| 7 #include "chrome/browser/ui/android/tab_model/android_live_tab_context.h" |
| 8 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 9 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 10 #include "components/sessions/content/content_live_tab.h" |
| 11 #include "components/sessions/content/content_serialized_navigation_builder.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/navigation_entry.h" |
| 14 |
| 15 AndroidLiveTabContext::AndroidLiveTabContext(TabModel* tab_model) |
| 16 : tab_model_(tab_model) {} |
| 17 |
| 18 // Called in tab restore service, but expected to do nothing on Android. |
| 19 void AndroidLiveTabContext::ShowBrowserWindow() { |
| 20 } |
| 21 |
| 22 const SessionID& AndroidLiveTabContext::GetSessionID() const { |
| 23 return tab_model_->SessionId(); |
| 24 } |
| 25 |
| 26 int AndroidLiveTabContext::GetTabCount() const { |
| 27 return tab_model_->GetTabCount(); |
| 28 } |
| 29 |
| 30 int AndroidLiveTabContext::GetSelectedIndex() const { |
| 31 return tab_model_->GetActiveIndex(); |
| 32 } |
| 33 |
| 34 // Not supported by android. |
| 35 std::string AndroidLiveTabContext::GetAppName() const { |
| 36 return std::string(); |
| 37 } |
| 38 |
| 39 sessions::LiveTab* AndroidLiveTabContext::GetLiveTabAt(int index) const { |
| 40 TabAndroid* tab_android = tab_model_->GetTabAt(index); |
| 41 if (!tab_android || !tab_android->web_contents()) |
| 42 return nullptr; |
| 43 |
| 44 return sessions::ContentLiveTab::GetForWebContents( |
| 45 tab_android->web_contents()); |
| 46 } |
| 47 |
| 48 sessions::LiveTab* AndroidLiveTabContext::GetActiveLiveTab() const { |
| 49 content::WebContents* web_contents = tab_model_->GetActiveWebContents(); |
| 50 if (!web_contents) |
| 51 return nullptr; |
| 52 |
| 53 return sessions::ContentLiveTab::GetForWebContents(web_contents); |
| 54 } |
| 55 |
| 56 // Not supported by android. |
| 57 bool AndroidLiveTabContext::IsTabPinned(int index) const { |
| 58 return false; |
| 59 } |
| 60 |
| 61 sessions::LiveTab* AndroidLiveTabContext::AddRestoredTab( |
| 62 const std::vector<sessions::SerializedNavigationEntry>& navigations, |
| 63 int tab_index, |
| 64 int selected_navigation, |
| 65 const std::string& extension_app_id, |
| 66 bool select, |
| 67 bool pin, |
| 68 bool from_last_session, |
| 69 const sessions::PlatformSpecificTabData* tab_platform_data, |
| 70 const std::string& user_agent_override) { |
| 71 Profile* profile = tab_model_->GetProfile(); |
| 72 |
| 73 // Prepare navigation history. |
| 74 std::vector<std::unique_ptr<content::NavigationEntry>> nav_entries = |
| 75 sessions::ContentSerializedNavigationBuilder::ToNavigationEntries( |
| 76 navigations, profile); |
| 77 |
| 78 // Restore web contents with navigation history. |
| 79 content::WebContents* web_contents = content::WebContents::Create( |
| 80 content::WebContents::CreateParams(profile)); |
| 81 web_contents->GetController().Restore( |
| 82 selected_navigation, |
| 83 content::NavigationController::RESTORE_CURRENT_SESSION, |
| 84 &nav_entries); |
| 85 |
| 86 // Create new tab. |
| 87 tab_model_->CreateTab(nullptr, web_contents, -1); |
| 88 return sessions::ContentLiveTab::GetForWebContents(web_contents); |
| 89 } |
| 90 |
| 91 // Currently does nothing. |
| 92 sessions::LiveTab* AndroidLiveTabContext::ReplaceRestoredTab( |
| 93 const std::vector<sessions::SerializedNavigationEntry>& navigations, |
| 94 int selected_navigation, |
| 95 bool from_last_session, |
| 96 const std::string& extension_app_id, |
| 97 const sessions::PlatformSpecificTabData* tab_platform_data, |
| 98 const std::string& user_agent_override) { |
| 99 NOTIMPLEMENTED(); |
| 100 return nullptr; |
| 101 } |
| 102 |
| 103 // Currently does nothing. |
| 104 void AndroidLiveTabContext::CloseTab() { |
| 105 NOTIMPLEMENTED(); |
| 106 } |
| 107 |
| 108 // static. |
| 109 sessions::LiveTabContext* AndroidLiveTabContext::FindContextForWebContents( |
| 110 const content::WebContents* contents) { |
| 111 TabAndroid* tab_android = TabAndroid::FromWebContents(contents); |
| 112 if (!tab_android) |
| 113 return nullptr; |
| 114 |
| 115 TabModel* model = TabModelList::FindTabModelWithId( |
| 116 tab_android->window_id().id()); |
| 117 |
| 118 return model ? model->GetLiveTabContext() : nullptr; |
| 119 } |
| 120 |
| 121 // static. |
| 122 sessions::LiveTabContext* AndroidLiveTabContext::FindContextWithID( |
| 123 SessionID::id_type desired_id) { |
| 124 // Find the model with desired id. |
| 125 TabModel* tab_model = TabModelList::FindTabModelWithId(desired_id); |
| 126 |
| 127 // If we can't find the correct model, fall back to first non-incognito model. |
| 128 if (!tab_model || tab_model->IsOffTheRecord()) { |
| 129 for (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) { |
| 130 TabModel* model = *it; |
| 131 if (!model->IsOffTheRecord()) { |
| 132 return model->GetLiveTabContext(); |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 return tab_model ? tab_model->GetLiveTabContext() : nullptr; |
| 138 } |
OLD | NEW |