| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h" | 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 return s1->modified_time > s2->modified_time; | 66 return s1->modified_time > s2->modified_time; |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Comparator function for use with std::sort that will sort tabs in a window | 69 // Comparator function for use with std::sort that will sort tabs in a window |
| 70 // by descending timestamp (i.e., most recent first). | 70 // by descending timestamp (i.e., most recent first). |
| 71 bool SortTabsByRecency(const sessions::SessionTab* t1, | 71 bool SortTabsByRecency(const sessions::SessionTab* t1, |
| 72 const sessions::SessionTab* t2) { | 72 const sessions::SessionTab* t2) { |
| 73 return t1->timestamp > t2->timestamp; | 73 return t1->timestamp > t2->timestamp; |
| 74 } | 74 } |
| 75 | 75 |
| 76 scoped_ptr<tabs::Tab> CreateTabModelHelper( | 76 tabs::Tab CreateTabModelHelper( |
| 77 Profile* profile, | 77 Profile* profile, |
| 78 const sessions::SerializedNavigationEntry& current_navigation, | 78 const sessions::SerializedNavigationEntry& current_navigation, |
| 79 const std::string& session_id, | 79 const std::string& session_id, |
| 80 int index, | 80 int index, |
| 81 bool pinned, | 81 bool pinned, |
| 82 int selected_index, | 82 int selected_index, |
| 83 const Extension* extension) { | 83 const Extension* extension) { |
| 84 scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); | 84 tabs::Tab tab_struct; |
| 85 | 85 |
| 86 const GURL& url = current_navigation.virtual_url(); | 86 const GURL& url = current_navigation.virtual_url(); |
| 87 std::string title = base::UTF16ToUTF8(current_navigation.title()); | 87 std::string title = base::UTF16ToUTF8(current_navigation.title()); |
| 88 | 88 |
| 89 tab_struct->session_id.reset(new std::string(session_id)); | 89 tab_struct.session_id.reset(new std::string(session_id)); |
| 90 tab_struct->url.reset(new std::string(url.spec())); | 90 tab_struct.url.reset(new std::string(url.spec())); |
| 91 tab_struct->fav_icon_url.reset( | 91 tab_struct.fav_icon_url.reset( |
| 92 new std::string(current_navigation.favicon_url().spec())); | 92 new std::string(current_navigation.favicon_url().spec())); |
| 93 if (!title.empty()) { | 93 if (!title.empty()) { |
| 94 tab_struct->title.reset(new std::string(title)); | 94 tab_struct.title.reset(new std::string(title)); |
| 95 } else { | 95 } else { |
| 96 const std::string languages = | 96 const std::string languages = |
| 97 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 97 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 98 tab_struct->title.reset(new std::string( | 98 tab_struct.title.reset(new std::string( |
| 99 base::UTF16ToUTF8(url_formatter::FormatUrl(url, languages)))); | 99 base::UTF16ToUTF8(url_formatter::FormatUrl(url, languages)))); |
| 100 } | 100 } |
| 101 tab_struct->index = index; | 101 tab_struct.index = index; |
| 102 tab_struct->pinned = pinned; | 102 tab_struct.pinned = pinned; |
| 103 // Note: |selected_index| from the sync sessions model is what we call | 103 // Note: |selected_index| from the sync sessions model is what we call |
| 104 // "active" in extensions terminology. "selected" is deprecated because it's | 104 // "active" in extensions terminology. "selected" is deprecated because it's |
| 105 // not clear whether it means "active" (user can see) or "highlighted" (user | 105 // not clear whether it means "active" (user can see) or "highlighted" (user |
| 106 // has highlighted, since you can select tabs without bringing them into the | 106 // has highlighted, since you can select tabs without bringing them into the |
| 107 // foreground). | 107 // foreground). |
| 108 tab_struct->active = index == selected_index; | 108 tab_struct.active = index == selected_index; |
| 109 ExtensionTabUtil::ScrubTabForExtension(extension, nullptr, tab_struct.get()); | 109 ExtensionTabUtil::ScrubTabForExtension(extension, nullptr, &tab_struct); |
| 110 return tab_struct; | 110 return tab_struct; |
| 111 } | 111 } |
| 112 | 112 |
| 113 scoped_ptr<windows::Window> CreateWindowModelHelper( | 113 scoped_ptr<windows::Window> CreateWindowModelHelper( |
| 114 scoped_ptr<std::vector<linked_ptr<tabs::Tab>>> tabs, | 114 scoped_ptr<std::vector<tabs::Tab>> tabs, |
| 115 const std::string& session_id, | 115 const std::string& session_id, |
| 116 const windows::WindowType& type, | 116 const windows::WindowType& type, |
| 117 const windows::WindowState& state) { | 117 const windows::WindowState& state) { |
| 118 scoped_ptr<windows::Window> window_struct(new windows::Window); | 118 scoped_ptr<windows::Window> window_struct(new windows::Window); |
| 119 window_struct->tabs = std::move(tabs); | 119 window_struct->tabs = std::move(tabs); |
| 120 window_struct->session_id.reset(new std::string(session_id)); | 120 window_struct->session_id.reset(new std::string(session_id)); |
| 121 window_struct->incognito = false; | 121 window_struct->incognito = false; |
| 122 window_struct->always_on_top = false; | 122 window_struct->always_on_top = false; |
| 123 window_struct->focused = false; | 123 window_struct->focused = false; |
| 124 window_struct->type = type; | 124 window_struct->type = type; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 143 } | 143 } |
| 144 | 144 |
| 145 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { | 145 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { |
| 146 return entry->type == sessions::TabRestoreService::TAB; | 146 return entry->type == sessions::TabRestoreService::TAB; |
| 147 } | 147 } |
| 148 | 148 |
| 149 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { | 149 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { |
| 150 return entry->type == sessions::TabRestoreService::WINDOW; | 150 return entry->type == sessions::TabRestoreService::WINDOW; |
| 151 } | 151 } |
| 152 | 152 |
| 153 scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( | 153 tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel( |
| 154 const sessions::TabRestoreService::Tab& tab, | 154 const sessions::TabRestoreService::Tab& tab, |
| 155 int session_id, | 155 int session_id, |
| 156 int selected_index) { | 156 int selected_index) { |
| 157 return CreateTabModelHelper(GetProfile(), | 157 return CreateTabModelHelper(GetProfile(), |
| 158 tab.navigations[tab.current_navigation_index], | 158 tab.navigations[tab.current_navigation_index], |
| 159 base::IntToString(session_id), | 159 base::IntToString(session_id), |
| 160 tab.tabstrip_index, | 160 tab.tabstrip_index, |
| 161 tab.pinned, | 161 tab.pinned, |
| 162 selected_index, | 162 selected_index, |
| 163 extension()); | 163 extension()); |
| 164 } | 164 } |
| 165 | 165 |
| 166 scoped_ptr<windows::Window> | 166 scoped_ptr<windows::Window> |
| 167 SessionsGetRecentlyClosedFunction::CreateWindowModel( | 167 SessionsGetRecentlyClosedFunction::CreateWindowModel( |
| 168 const sessions::TabRestoreService::Window& window, | 168 const sessions::TabRestoreService::Window& window, |
| 169 int session_id) { | 169 int session_id) { |
| 170 DCHECK(!window.tabs.empty()); | 170 DCHECK(!window.tabs.empty()); |
| 171 | 171 |
| 172 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | 172 scoped_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); |
| 173 new std::vector<linked_ptr<tabs::Tab> >); | |
| 174 for (size_t i = 0; i < window.tabs.size(); ++i) { | 173 for (size_t i = 0; i < window.tabs.size(); ++i) { |
| 175 tabs->push_back(make_linked_ptr( | 174 tabs->push_back(CreateTabModel(window.tabs[i], window.tabs[i].id, |
| 176 CreateTabModel(window.tabs[i], window.tabs[i].id, | 175 window.selected_tab_index)); |
| 177 window.selected_tab_index).release())); | |
| 178 } | 176 } |
| 179 | 177 |
| 180 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), | 178 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), |
| 181 windows::WINDOW_TYPE_NORMAL, | 179 windows::WINDOW_TYPE_NORMAL, |
| 182 windows::WINDOW_STATE_NORMAL); | 180 windows::WINDOW_STATE_NORMAL); |
| 183 } | 181 } |
| 184 | 182 |
| 185 scoped_ptr<api::sessions::Session> | 183 scoped_ptr<api::sessions::Session> |
| 186 SessionsGetRecentlyClosedFunction::CreateSessionModel( | 184 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
| 187 const sessions::TabRestoreService::Entry* entry) { | 185 const sessions::TabRestoreService::Entry* entry) { |
| 188 scoped_ptr<tabs::Tab> tab; | 186 scoped_ptr<tabs::Tab> tab; |
| 189 scoped_ptr<windows::Window> window; | 187 scoped_ptr<windows::Window> window; |
| 190 switch (entry->type) { | 188 switch (entry->type) { |
| 191 case sessions::TabRestoreService::TAB: | 189 case sessions::TabRestoreService::TAB: |
| 192 tab = CreateTabModel( | 190 tab.reset(new tabs::Tab(CreateTabModel( |
| 193 *static_cast<const sessions::TabRestoreService::Tab*>(entry), | 191 *static_cast<const sessions::TabRestoreService::Tab*>(entry), |
| 194 entry->id, -1); | 192 entry->id, -1))); |
| 195 break; | 193 break; |
| 196 case sessions::TabRestoreService::WINDOW: | 194 case sessions::TabRestoreService::WINDOW: |
| 197 window = CreateWindowModel( | 195 window = CreateWindowModel( |
| 198 *static_cast<const sessions::TabRestoreService::Window*>(entry), | 196 *static_cast<const sessions::TabRestoreService::Window*>(entry), |
| 199 entry->id); | 197 entry->id); |
| 200 break; | 198 break; |
| 201 default: | 199 default: |
| 202 NOTREACHED(); | 200 NOTREACHED(); |
| 203 } | 201 } |
| 204 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab), | 202 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 233 // uninteresting entries. | 231 // uninteresting entries. |
| 234 for (const sessions::TabRestoreService::Entry* entry : | 232 for (const sessions::TabRestoreService::Entry* entry : |
| 235 tab_restore_service->entries()) { | 233 tab_restore_service->entries()) { |
| 236 result.push_back(std::move(*CreateSessionModel(entry))); | 234 result.push_back(std::move(*CreateSessionModel(entry))); |
| 237 } | 235 } |
| 238 | 236 |
| 239 results_ = GetRecentlyClosed::Results::Create(result); | 237 results_ = GetRecentlyClosed::Results::Create(result); |
| 240 return true; | 238 return true; |
| 241 } | 239 } |
| 242 | 240 |
| 243 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( | 241 tabs::Tab SessionsGetDevicesFunction::CreateTabModel( |
| 244 const std::string& session_tag, | 242 const std::string& session_tag, |
| 245 const sessions::SessionTab& tab, | 243 const sessions::SessionTab& tab, |
| 246 int tab_index, | 244 int tab_index, |
| 247 int selected_index) { | 245 int selected_index) { |
| 248 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString(); | 246 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString(); |
| 249 return CreateTabModelHelper( | 247 return CreateTabModelHelper( |
| 250 GetProfile(), | 248 GetProfile(), |
| 251 tab.navigations[tab.normalized_navigation_index()], | 249 tab.navigations[tab.normalized_navigation_index()], |
| 252 session_id, | 250 session_id, |
| 253 tab_index, | 251 tab_index, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 271 tab->navigations.at(tab->normalized_navigation_index()); | 269 tab->navigations.at(tab->normalized_navigation_index()); |
| 272 if (search::IsNTPURL(current_navigation.virtual_url(), GetProfile())) { | 270 if (search::IsNTPURL(current_navigation.virtual_url(), GetProfile())) { |
| 273 continue; | 271 continue; |
| 274 } | 272 } |
| 275 tabs_in_window.push_back(tab); | 273 tabs_in_window.push_back(tab); |
| 276 } | 274 } |
| 277 if (tabs_in_window.empty()) | 275 if (tabs_in_window.empty()) |
| 278 return scoped_ptr<windows::Window>(); | 276 return scoped_ptr<windows::Window>(); |
| 279 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency); | 277 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency); |
| 280 | 278 |
| 281 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | 279 scoped_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); |
| 282 new std::vector<linked_ptr<tabs::Tab> >); | |
| 283 for (size_t i = 0; i < tabs_in_window.size(); ++i) { | 280 for (size_t i = 0; i < tabs_in_window.size(); ++i) { |
| 284 tabs->push_back(make_linked_ptr( | 281 tabs->push_back(CreateTabModel(session_tag, *tabs_in_window[i], i, |
| 285 CreateTabModel(session_tag, *tabs_in_window[i], i, | 282 window.selected_tab_index)); |
| 286 window.selected_tab_index).release())); | |
| 287 } | 283 } |
| 288 | 284 |
| 289 std::string session_id = | 285 std::string session_id = |
| 290 SessionId(session_tag, window.window_id.id()).ToString(); | 286 SessionId(session_tag, window.window_id.id()).ToString(); |
| 291 | 287 |
| 292 windows::WindowType type = windows::WINDOW_TYPE_NONE; | 288 windows::WindowType type = windows::WINDOW_TYPE_NONE; |
| 293 switch (window.type) { | 289 switch (window.type) { |
| 294 case sessions::SessionWindow::TYPE_TABBED: | 290 case sessions::SessionWindow::TYPE_TABBED: |
| 295 type = windows::WINDOW_TYPE_NORMAL; | 291 type = windows::WINDOW_TYPE_NORMAL; |
| 296 break; | 292 break; |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 return g_factory.Pointer(); | 640 return g_factory.Pointer(); |
| 645 } | 641 } |
| 646 | 642 |
| 647 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 643 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 648 sessions_event_router_.reset( | 644 sessions_event_router_.reset( |
| 649 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 645 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 650 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 646 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 651 } | 647 } |
| 652 | 648 |
| 653 } // namespace extensions | 649 } // namespace extensions |
| OLD | NEW |