Skip to content

Commit

Permalink
Remove ScopedVector from components/sessions.
Browse files Browse the repository at this point in the history
BUG=554289, 561329

Review-Url: https://codereview.chromium.org/2600583002
Cr-Commit-Position: refs/heads/master@{#441162}
  • Loading branch information
avi authored and Commit bot committed Jan 3, 2017
1 parent 00e8e63 commit 51f1cd9
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 132 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/sessions/session_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ void SessionService::OnBrowserSetLastActive(Browser* browser) {

void SessionService::OnGotSessionCommands(
const sessions::GetLastSessionCallback& callback,
ScopedVector<sessions::SessionCommand> commands) {
std::vector<std::unique_ptr<sessions::SessionCommand>> commands) {
std::vector<std::unique_ptr<sessions::SessionWindow>> valid_windows;
SessionID::id_type active_window_id = 0;

Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/sessions/session_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h"
Expand Down Expand Up @@ -260,8 +259,9 @@ class SessionService : public sessions::BaseSessionServiceDelegate,
void OnBrowserSetLastActive(Browser* browser) override;

// Converts |commands| to SessionWindows and notifies the callback.
void OnGotSessionCommands(const sessions::GetLastSessionCallback& callback,
ScopedVector<sessions::SessionCommand> commands);
void OnGotSessionCommands(
const sessions::GetLastSessionCallback& callback,
std::vector<std::unique_ptr<sessions::SessionCommand>> commands);

// Adds commands to commands that will recreate the state of the specified
// tab. This adds at most kMaxNavigationCountToPersist navigations (in each
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/sessions/session_service_test_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "chrome/browser/sessions/session_service_test_helper.h"

#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/sessions/session_service.h"
#include "components/sessions/core/base_session_service_test_helper.h"
Expand Down Expand Up @@ -60,7 +59,7 @@ void SessionServiceTestHelper::ReadWindows(
std::vector<std::unique_ptr<sessions::SessionWindow>>* windows,
SessionID::id_type* active_window_id) {
Time last_time;
ScopedVector<sessions::SessionCommand> read_commands;
std::vector<std::unique_ptr<sessions::SessionCommand>> read_commands;
sessions::BaseSessionServiceTestHelper test_helper(
service_->GetBaseSessionServiceForTest());
test_helper.ReadLastSessionCommands(&read_commands);
Expand Down
31 changes: 16 additions & 15 deletions components/sessions/core/base_session_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace {
void RunIfNotCanceled(
const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
const BaseSessionService::GetCommandsCallback& callback,
ScopedVector<SessionCommand> commands) {
std::vector<std::unique_ptr<SessionCommand>> commands) {
if (is_canceled.Run())
return;
callback.Run(std::move(commands));
Expand All @@ -33,7 +33,7 @@ void RunIfNotCanceled(
void PostOrRunInternalGetCommandsCallback(
base::TaskRunner* task_runner,
const BaseSessionService::GetCommandsCallback& callback,
ScopedVector<SessionCommand> commands) {
std::vector<std::unique_ptr<SessionCommand>> commands) {
if (task_runner->RunsTasksOnCurrentThread()) {
callback.Run(std::move(commands));
} else {
Expand Down Expand Up @@ -80,35 +80,36 @@ void BaseSessionService::ScheduleCommand(
std::unique_ptr<SessionCommand> command) {
DCHECK(command);
commands_since_reset_++;
pending_commands_.push_back(command.release());
pending_commands_.push_back(std::move(command));
StartSaveTimer();
}

void BaseSessionService::AppendRebuildCommand(
std::unique_ptr<SessionCommand> command) {
DCHECK(command);
pending_commands_.push_back(command.release());
pending_commands_.push_back(std::move(command));
}

void BaseSessionService::EraseCommand(SessionCommand* old_command) {
ScopedVector<SessionCommand>::iterator it =
std::find(pending_commands_.begin(),
pending_commands_.end(),
old_command);
auto it = std::find_if(
pending_commands_.begin(), pending_commands_.end(),
[old_command](const std::unique_ptr<SessionCommand>& command_ptr) {
return command_ptr.get() == old_command;
});
CHECK(it != pending_commands_.end());
pending_commands_.erase(it);
}

void BaseSessionService::SwapCommand(
SessionCommand* old_command,
std::unique_ptr<SessionCommand> new_command) {
ScopedVector<SessionCommand>::iterator it =
std::find(pending_commands_.begin(),
pending_commands_.end(),
old_command);
auto it = std::find_if(
pending_commands_.begin(), pending_commands_.end(),
[old_command](const std::unique_ptr<SessionCommand>& command_ptr) {
return command_ptr.get() == old_command;
});
CHECK(it != pending_commands_.end());
*it = new_command.release();
delete old_command;
*it = std::move(new_command);
}

void BaseSessionService::ClearPendingCommands() {
Expand All @@ -134,7 +135,7 @@ void BaseSessionService::Save() {
if (pending_commands_.empty())
return;

// We create a new ScopedVector which will receive all elements from the
// We create a new vector which will receive all elements from the
// current commands. This will also clear the current list.
RunTaskOnBackendThread(
FROM_HERE,
Expand Down
7 changes: 3 additions & 4 deletions components/sessions/core/base_session_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/sessions/core/sessions_export.h"
Expand All @@ -35,7 +34,7 @@ class SESSIONS_EXPORT BaseSessionService {
TAB_RESTORE
};

typedef base::Callback<void(ScopedVector<SessionCommand>)>
typedef base::Callback<void(std::vector<std::unique_ptr<SessionCommand>>)>
GetCommandsCallback;

// Creates a new BaseSessionService. After creation you need to invoke
Expand All @@ -55,7 +54,7 @@ class SESSIONS_EXPORT BaseSessionService {

// Returns the set of commands which were scheduled to be written. Once
// committed to the backend, the commands are removed from here.
const ScopedVector<SessionCommand>& pending_commands() {
const std::vector<std::unique_ptr<SessionCommand>>& pending_commands() {
return pending_commands_;
}

Expand Down Expand Up @@ -112,7 +111,7 @@ class SESSIONS_EXPORT BaseSessionService {
scoped_refptr<SessionBackend> backend_;

// Commands we need to send over to the backend.
ScopedVector<SessionCommand> pending_commands_;
std::vector<std::unique_ptr<SessionCommand>> pending_commands_;

// Whether the backend file should be recreated the next time we send
// over the commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool BaseSessionServiceTestHelper::ProcessedAnyCommands() {
}

bool BaseSessionServiceTestHelper::ReadLastSessionCommands(
ScopedVector<SessionCommand>* commands) {
std::vector<std::unique_ptr<SessionCommand>>* commands) {
return base_session_service_->backend_->ReadLastSessionCommandsImpl(commands);
}

Expand Down
7 changes: 5 additions & 2 deletions components/sessions/core/base_session_service_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#ifndef COMPONENTS_SESSIONS_CORE_BASE_SESSION_SERVICE_TEST_HELPER_H_
#define COMPONENTS_SESSIONS_CORE_BASE_SESSION_SERVICE_TEST_HELPER_H_

#include <memory>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/task/cancelable_task_tracker.h"

namespace sessions {
Expand All @@ -29,7 +31,8 @@ class BaseSessionServiceTestHelper {
bool ProcessedAnyCommands();

// Read the last session commands directly from file.
bool ReadLastSessionCommands(ScopedVector<SessionCommand>* commands);
bool ReadLastSessionCommands(
std::vector<std::unique_ptr<SessionCommand>>* commands);

private:
BaseSessionService* base_session_service_;
Expand Down
14 changes: 7 additions & 7 deletions components/sessions/core/persistent_tab_restore_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ class PersistentTabRestoreService::Delegate
// Invoked when we've loaded the session commands that identify the previously
// closed tabs. This creates entries, adds them to staging_entries_, and
// invokes LoadState.
void OnGotLastSessionCommands(ScopedVector<SessionCommand> commands);
void OnGotLastSessionCommands(
std::vector<std::unique_ptr<SessionCommand>> commands);

// Populates |loaded_entries| with Entries from |commands|.
void CreateEntriesFromCommands(
const std::vector<SessionCommand*>& commands,
const std::vector<std::unique_ptr<SessionCommand>>& commands,
std::vector<std::unique_ptr<Entry>>* loaded_entries);

// Validates all entries in |entries|, deleting any with no navigations. This
Expand Down Expand Up @@ -578,9 +579,9 @@ int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist(
}

void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands(
ScopedVector<SessionCommand> commands) {
std::vector<std::unique_ptr<SessionCommand>> commands) {
std::vector<std::unique_ptr<TabRestoreService::Entry>> entries;
CreateEntriesFromCommands(commands.get(), &entries);
CreateEntriesFromCommands(commands, &entries);
// Closed tabs always go to the end.
staging_entries_.insert(staging_entries_.end(),
make_move_iterator(entries.begin()),
Expand All @@ -590,7 +591,7 @@ void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands(
}

void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
const std::vector<SessionCommand*>& commands,
const std::vector<std::unique_ptr<SessionCommand>>& commands,
std::vector<std::unique_ptr<Entry>>* loaded_entries) {
if (tab_restore_service_helper_->entries().size() == kMaxEntries)
return;
Expand All @@ -603,8 +604,7 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
Window* current_window = nullptr;
// If > 0, we've gotten a window command but not all the tabs yet.
int pending_window_tabs = 0;
for (std::vector<SessionCommand*>::const_iterator i = commands.begin();
i != commands.end(); ++i) {
for (auto i = commands.begin(); i != commands.end(); ++i) {
const SessionCommand& command = *(*i);
switch (command.id()) {
case kCommandRestoredEntry: {
Expand Down
46 changes: 23 additions & 23 deletions components/sessions/core/session_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -55,17 +55,16 @@ class SessionFileReader {
path, base::File::FLAG_OPEN | base::File::FLAG_READ));
}
// Reads the contents of the file specified in the constructor, returning
// true on success. It is up to the caller to free all SessionCommands
// added to commands.
// true on success, and filling up |commands| with commands.
bool Read(sessions::BaseSessionService::SessionType type,
ScopedVector<sessions::SessionCommand>* commands);
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands);

private:
// Reads a single command, returning it. A return value of NULL indicates
// either there are no commands, or there was an error. Use errored_ to
// distinguish the two. If NULL is returned, and there is no error, it means
// the end of file was successfully reached.
sessions::SessionCommand* ReadCommand();
std::unique_ptr<sessions::SessionCommand> ReadCommand();

// Shifts the unused portion of buffer_ to the beginning and fills the
// remaining portion with data from the file. Returns false if the buffer
Expand All @@ -91,8 +90,9 @@ class SessionFileReader {
DISALLOW_COPY_AND_ASSIGN(SessionFileReader);
};

bool SessionFileReader::Read(sessions::BaseSessionService::SessionType type,
ScopedVector<sessions::SessionCommand>* commands) {
bool SessionFileReader::Read(
sessions::BaseSessionService::SessionType type,
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands) {
if (!file_->IsValid())
return false;
FileHeader header;
Expand All @@ -104,10 +104,10 @@ bool SessionFileReader::Read(sessions::BaseSessionService::SessionType type,
header.version != kFileCurrentVersion)
return false;

ScopedVector<sessions::SessionCommand> read_commands;
for (sessions::SessionCommand* command = ReadCommand(); command && !errored_;
command = ReadCommand())
read_commands.push_back(command);
std::vector<std::unique_ptr<sessions::SessionCommand>> read_commands;
for (std::unique_ptr<sessions::SessionCommand> command = ReadCommand();
command && !errored_; command = ReadCommand())
read_commands.push_back(std::move(command));
if (!errored_)
read_commands.swap(*commands);
if (type == sessions::BaseSessionService::TAB_RESTORE) {
Expand All @@ -120,7 +120,7 @@ bool SessionFileReader::Read(sessions::BaseSessionService::SessionType type,
return !errored_;
}

sessions::SessionCommand* SessionFileReader::ReadCommand() {
std::unique_ptr<sessions::SessionCommand> SessionFileReader::ReadCommand() {
// Make sure there is enough in the buffer for the size of the next command.
if (available_count_ < sizeof(size_type)) {
if (!FillBuffer())
Expand Down Expand Up @@ -157,8 +157,9 @@ sessions::SessionCommand* SessionFileReader::ReadCommand() {
const id_type command_id = buffer_[buffer_position_];
// NOTE: command_size includes the size of the id, which is not part of
// the contents of the SessionCommand.
sessions::SessionCommand* command =
new sessions::SessionCommand(command_id, command_size - sizeof(id_type));
std::unique_ptr<sessions::SessionCommand> command =
base::MakeUnique<sessions::SessionCommand>(
command_id, command_size - sizeof(id_type));
if (command_size > sizeof(id_type)) {
memcpy(command->contents(),
&(buffer_[buffer_position_ + sizeof(id_type)]),
Expand Down Expand Up @@ -227,7 +228,7 @@ void SessionBackend::Init() {
}

void SessionBackend::AppendCommands(
ScopedVector<sessions::SessionCommand> commands,
std::vector<std::unique_ptr<sessions::SessionCommand>> commands,
bool reset_first) {
Init();
// Make sure and check current_session_file_, if opening the file failed
Expand All @@ -252,13 +253,13 @@ void SessionBackend::ReadLastSessionCommands(

Init();

ScopedVector<sessions::SessionCommand> commands;
std::vector<std::unique_ptr<sessions::SessionCommand>> commands;
ReadLastSessionCommandsImpl(&commands);
callback.Run(std::move(commands));
}

bool SessionBackend::ReadLastSessionCommandsImpl(
ScopedVector<sessions::SessionCommand>* commands) {
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands) {
Init();
SessionFileReader file_reader(GetLastSessionPath());
return file_reader.Read(type_, commands);
Expand Down Expand Up @@ -299,17 +300,16 @@ void SessionBackend::MoveCurrentSessionToLastSession() {
}

bool SessionBackend::ReadCurrentSessionCommandsImpl(
ScopedVector<sessions::SessionCommand>* commands) {
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands) {
Init();
SessionFileReader file_reader(GetCurrentSessionPath());
return file_reader.Read(type_, commands);
}

bool SessionBackend::AppendCommandsToFile(base::File* file,
const ScopedVector<sessions::SessionCommand>& commands) {
for (ScopedVector<sessions::SessionCommand>::const_iterator i =
commands.begin();
i != commands.end(); ++i) {
bool SessionBackend::AppendCommandsToFile(
base::File* file,
const std::vector<std::unique_ptr<sessions::SessionCommand>>& commands) {
for (auto i = commands.begin(); i != commands.end(); ++i) {
int wrote;
const size_type content_size = static_cast<size_type>((*i)->size());
const size_type total_size = content_size + sizeof(id_type);
Expand Down
11 changes: 6 additions & 5 deletions components/sessions/core/session_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class SESSIONS_EXPORT SessionBackend

// Appends the specified commands to the current file. If reset_first is
// true the the current file is recreated.
void AppendCommands(ScopedVector<sessions::SessionCommand> commands,
bool reset_first);
void AppendCommands(
std::vector<std::unique_ptr<sessions::SessionCommand>> commands,
bool reset_first);

// Invoked from the service to read the commands that make up the last
// session, invokes ReadLastSessionCommandsImpl to do the work.
Expand All @@ -77,7 +78,7 @@ class SESSIONS_EXPORT SessionBackend
//
// On success, the read commands are added to commands.
bool ReadLastSessionCommandsImpl(
ScopedVector<sessions::SessionCommand>* commands);
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands);

// Deletes the file containing the commands for the last session.
void DeleteLastSession();
Expand All @@ -92,7 +93,7 @@ class SESSIONS_EXPORT SessionBackend
// On success, the read commands are added to commands. It is up to the
// caller to delete the commands.
bool ReadCurrentSessionCommandsImpl(
ScopedVector<sessions::SessionCommand>* commands);
std::vector<std::unique_ptr<sessions::SessionCommand>>* commands);

private:
friend class base::RefCountedThreadSafe<SessionBackend>;
Expand All @@ -114,7 +115,7 @@ class SESSIONS_EXPORT SessionBackend
// Appends the specified commands to the specified file.
bool AppendCommandsToFile(
base::File* file,
const ScopedVector<sessions::SessionCommand>& commands);
const std::vector<std::unique_ptr<sessions::SessionCommand>>& commands);

const sessions::BaseSessionService::SessionType type_;

Expand Down
Loading

0 comments on commit 51f1cd9

Please sign in to comment.