Commit 9db24af9 authored by rbpotter's avatar rbpotter Committed by Commit Bot

History ForeignSessionHandler: Use non-deprecated base::Value APIs

Splitting these changes off of
https://chromium-review.googlesource.com/c/chromium/src/+/1949773

Bug: 1022212
Change-Id: Ibf62966995ff4c16eb8cd1edf65110e469ad28e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1951156
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721684}
parent fa5ebf20
...@@ -70,10 +70,9 @@ std::string DeviceTypeToString(sync_pb::SyncEnums::DeviceType device_type) { ...@@ -70,10 +70,9 @@ std::string DeviceTypeToString(sync_pb::SyncEnums::DeviceType device_type) {
} }
// Helper method to create JSON compatible objects from Session objects. // Helper method to create JSON compatible objects from Session objects.
std::unique_ptr<base::DictionaryValue> SessionTabToValue( base::Value SessionTabToValue(const ::sessions::SessionTab& tab) {
const ::sessions::SessionTab& tab) {
if (tab.navigations.empty()) if (tab.navigations.empty())
return nullptr; return base::Value();
int selected_index = std::min(tab.current_navigation_index, int selected_index = std::min(tab.current_navigation_index,
static_cast<int>(tab.navigations.size() - 1)); static_cast<int>(tab.navigations.size() - 1));
...@@ -81,61 +80,56 @@ std::unique_ptr<base::DictionaryValue> SessionTabToValue( ...@@ -81,61 +80,56 @@ std::unique_ptr<base::DictionaryValue> SessionTabToValue(
tab.navigations.at(selected_index); tab.navigations.at(selected_index);
GURL tab_url = current_navigation.virtual_url(); GURL tab_url = current_navigation.virtual_url();
if (!tab_url.is_valid() || tab_url.spec() == chrome::kChromeUINewTabURL) if (!tab_url.is_valid() || tab_url.spec() == chrome::kChromeUINewTabURL)
return nullptr; return base::Value();
std::unique_ptr<base::DictionaryValue> dictionary( base::Value dictionary(base::Value::Type::DICTIONARY);
new base::DictionaryValue()); NewTabUI::SetUrlTitleAndDirection(&dictionary, current_navigation.title(),
NewTabUI::SetUrlTitleAndDirection(dictionary.get(), tab_url);
current_navigation.title(), tab_url); dictionary.SetStringKey("remoteIconUrlForUma",
dictionary->SetString("remoteIconUrlForUma",
current_navigation.favicon_url().spec()); current_navigation.favicon_url().spec());
dictionary->SetString("type", "tab"); dictionary.SetStringKey("type", "tab");
dictionary->SetDouble("timestamp", dictionary.SetDoubleKey("timestamp",
static_cast<double>(tab.timestamp.ToInternalValue())); static_cast<double>(tab.timestamp.ToInternalValue()));
// TODO(jeremycho): This should probably be renamed to tabId to avoid // TODO(jeremycho): This should probably be renamed to tabId to avoid
// confusion with the ID corresponding to a session. Investigate all the // confusion with the ID corresponding to a session. Investigate all the
// places (C++ and JS) where this is being used. (http://crbug.com/154865). // places (C++ and JS) where this is being used. (http://crbug.com/154865).
dictionary->SetInteger("sessionId", tab.tab_id.id()); dictionary.SetIntKey("sessionId", tab.tab_id.id());
return dictionary; return dictionary;
} }
// Helper for initializing a boilerplate SessionWindow JSON compatible object. // Helper for initializing a boilerplate SessionWindow JSON compatible object.
std::unique_ptr<base::DictionaryValue> BuildWindowData( base::Value BuildWindowData(base::Time modification_time, SessionID window_id) {
base::Time modification_time, base::Value dictionary(base::Value::Type::DICTIONARY);
SessionID window_id) {
std::unique_ptr<base::DictionaryValue> dictionary(
new base::DictionaryValue());
// The items which are to be written into |dictionary| are also described in // The items which are to be written into |dictionary| are also described in
// chrome/browser/resources/ntp4/other_sessions.js in @typedef for WindowData. // chrome/browser/resources/ntp4/other_sessions.js in @typedef for WindowData.
// Please update it whenever you add or remove any keys here. // Please update it whenever you add or remove any keys here.
dictionary->SetString("type", "window"); dictionary.SetStringKey("type", "window");
dictionary->SetDouble("timestamp", modification_time.ToInternalValue()); dictionary.SetDoubleKey("timestamp", modification_time.ToInternalValue());
dictionary->SetInteger("sessionId", window_id.id()); dictionary.SetIntKey("sessionId", window_id.id());
return dictionary; return dictionary;
} }
// Helper method to create JSON compatible objects from SessionWindow objects. // Helper method to create JSON compatible objects from SessionWindow objects.
std::unique_ptr<base::DictionaryValue> SessionWindowToValue( base::Value SessionWindowToValue(const ::sessions::SessionWindow& window) {
const ::sessions::SessionWindow& window) {
if (window.tabs.empty()) if (window.tabs.empty())
return nullptr; return base::Value();
std::unique_ptr<base::ListValue> tab_values(new base::ListValue());
base::Value tab_values(base::Value::Type::LIST);
// Calculate the last |modification_time| for all entries within a window. // Calculate the last |modification_time| for all entries within a window.
base::Time modification_time = window.timestamp; base::Time modification_time = window.timestamp;
for (const std::unique_ptr<sessions::SessionTab>& tab : window.tabs) { for (const std::unique_ptr<sessions::SessionTab>& tab : window.tabs) {
std::unique_ptr<base::DictionaryValue> tab_value( base::Value tab_value = SessionTabToValue(*tab.get());
SessionTabToValue(*tab.get())); if (!tab_value.is_none()) {
if (tab_value.get()) {
modification_time = std::max(modification_time, tab->timestamp); modification_time = std::max(modification_time, tab->timestamp);
tab_values->Append(std::move(tab_value)); tab_values.GetList().push_back(std::move(tab_value));
} }
} }
if (tab_values->GetSize() == 0) if (tab_values.GetList().empty())
return nullptr; return base::Value();
std::unique_ptr<base::DictionaryValue> dictionary(
BuildWindowData(window.timestamp, window.window_id)); base::Value dictionary = BuildWindowData(window.timestamp, window.window_id);
dictionary->Set("tabs", std::move(tab_values)); dictionary.SetKey("tabs", std::move(tab_values));
return dictionary; return dictionary;
} }
...@@ -283,7 +277,7 @@ void ForeignSessionHandler::HandleGetForeignSessions( ...@@ -283,7 +277,7 @@ void ForeignSessionHandler::HandleGetForeignSessions(
GetOpenTabsUIDelegate(web_ui()); GetOpenTabsUIDelegate(web_ui());
std::vector<const sync_sessions::SyncedSession*> sessions; std::vector<const sync_sessions::SyncedSession*> sessions;
base::ListValue session_list; base::Value session_list(base::Value::Type::LIST);
if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) { if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) {
if (!load_attempt_time_.is_null()) { if (!load_attempt_time_.is_null()) {
UMA_HISTOGRAM_TIMES("Sync.SessionsRefreshDelay", UMA_HISTOGRAM_TIMES("Sync.SessionsRefreshDelay",
...@@ -305,40 +299,41 @@ void ForeignSessionHandler::HandleGetForeignSessions( ...@@ -305,40 +299,41 @@ void ForeignSessionHandler::HandleGetForeignSessions(
for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) {
const sync_sessions::SyncedSession* session = sessions[i]; const sync_sessions::SyncedSession* session = sessions[i];
const std::string& session_tag = session->session_tag; const std::string& session_tag = session->session_tag;
std::unique_ptr<base::DictionaryValue> session_data( base::Value session_data(base::Value::Type::DICTIONARY);
new base::DictionaryValue());
// The items which are to be written into |session_data| are also // The items which are to be written into |session_data| are also
// described in chrome/browser/resources/history/externs.js // described in chrome/browser/resources/history/externs.js
// @typedef for ForeignSession. Please update it whenever you add or // @typedef for ForeignSession. Please update it whenever you add or
// remove any keys here. // remove any keys here.
session_data->SetString("tag", session_tag); session_data.SetStringKey("tag", session_tag);
session_data->SetString("name", session->session_name); session_data.SetStringKey("name", session->session_name);
session_data->SetString("deviceType", session_data.SetStringKey("deviceType",
DeviceTypeToString(session->device_type)); DeviceTypeToString(session->device_type));
session_data->SetString("modifiedTime", session_data.SetStringKey("modifiedTime",
FormatSessionTime(session->modified_time)); FormatSessionTime(session->modified_time));
session_data->SetDouble("timestamp", session->modified_time.ToJsTime()); session_data.SetDoubleKey("timestamp", session->modified_time.ToJsTime());
bool is_collapsed = collapsed_sessions->HasKey(session_tag); bool is_collapsed = collapsed_sessions->HasKey(session_tag);
session_data->SetBoolean("collapsed", is_collapsed); session_data.SetBoolKey("collapsed", is_collapsed);
if (is_collapsed) if (is_collapsed)
current_collapsed_sessions->SetBoolean(session_tag, true); current_collapsed_sessions->SetBoolean(session_tag, true);
std::unique_ptr<base::ListValue> window_list(new base::ListValue()); base::Value window_list(base::Value::Type::LIST);
// Order tabs by visual order within window. // Order tabs by visual order within window.
for (const auto& window_pair : session->windows) { for (const auto& window_pair : session->windows) {
std::unique_ptr<base::DictionaryValue> window_data( base::Value window_data =
SessionWindowToValue(window_pair.second->wrapped_window)); SessionWindowToValue(window_pair.second->wrapped_window);
if (window_data) if (!window_data.is_none()) {
window_list->Append(std::move(window_data)); window_list.GetList().push_back(std::move(window_data));
}
} }
session_data->Set("windows", std::move(window_list)); session_data.SetKey("windows", std::move(window_list));
session_list.Append(std::move(session_data)); session_list.GetList().push_back(std::move(session_data));
} }
} }
web_ui()->CallJavascriptFunctionUnsafe("setForeignSessions", session_list); web_ui()->CallJavascriptFunctionUnsafe("setForeignSessions",
std::move(session_list));
} }
void ForeignSessionHandler::HandleOpenForeignSession( void ForeignSessionHandler::HandleOpenForeignSession(
......
...@@ -96,10 +96,10 @@ bool NewTabUI::IsNewTab(const GURL& url) { ...@@ -96,10 +96,10 @@ bool NewTabUI::IsNewTab(const GURL& url) {
} }
// static // static
void NewTabUI::SetUrlTitleAndDirection(base::DictionaryValue* dictionary, void NewTabUI::SetUrlTitleAndDirection(base::Value* dictionary,
const base::string16& title, const base::string16& title,
const GURL& gurl) { const GURL& gurl) {
dictionary->SetString("url", gurl.spec()); dictionary->SetStringKey("url", gurl.spec());
bool using_url_as_the_title = false; bool using_url_as_the_title = false;
base::string16 title_to_set(title); base::string16 title_to_set(title);
...@@ -125,8 +125,8 @@ void NewTabUI::SetUrlTitleAndDirection(base::DictionaryValue* dictionary, ...@@ -125,8 +125,8 @@ void NewTabUI::SetUrlTitleAndDirection(base::DictionaryValue* dictionary,
else else
direction = GetHtmlTextDirection(title); direction = GetHtmlTextDirection(title);
dictionary->SetString("title", title_to_set); dictionary->SetStringKey("title", title_to_set);
dictionary->SetString("direction", direction); dictionary->SetStringKey("direction", direction);
} }
// static // static
......
...@@ -16,6 +16,7 @@ class Profile; ...@@ -16,6 +16,7 @@ class Profile;
namespace base { namespace base {
class DictionaryValue; class DictionaryValue;
class Value;
} }
namespace user_prefs { namespace user_prefs {
...@@ -39,7 +40,7 @@ class NewTabUI : public content::WebUIController { ...@@ -39,7 +40,7 @@ class NewTabUI : public content::WebUIController {
// Adds "url", "title", and "direction" keys on incoming dictionary, setting // Adds "url", "title", and "direction" keys on incoming dictionary, setting
// title as the url as a fallback on empty title. // title as the url as a fallback on empty title.
static void SetUrlTitleAndDirection(base::DictionaryValue* dictionary, static void SetUrlTitleAndDirection(base::Value* dictionary,
const base::string16& title, const base::string16& title,
const GURL& gurl); const GURL& gurl);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment