Skip to content

Commit

Permalink
Convert SessionService to use new CancelableTaskTracker
Browse files Browse the repository at this point in the history
BUG=155883


Review URL: https://chromiumcodereview.appspot.com/11446033

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172418 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kaiwang@chromium.org committed Dec 11, 2012
1 parent 5e150c2 commit 4c7f9c5
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 160 deletions.
49 changes: 31 additions & 18 deletions chrome/browser/sessions/base_session_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@
using content::BrowserThread;
using content::NavigationEntry;

// InternalGetCommandsRequest -------------------------------------------------

BaseSessionService::InternalGetCommandsRequest::InternalGetCommandsRequest(
const CallbackType& callback)
: CancelableRequest<InternalGetCommandsCallback>(callback) {
}

BaseSessionService::InternalGetCommandsRequest::~InternalGetCommandsRequest() {
STLDeleteElements(&commands);
}

// BaseSessionService ---------------------------------------------------------

namespace {
Expand All @@ -51,6 +40,24 @@ void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes,
}
}

// Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner
// thread if it's not canceled.
void RunIfNotCanceled(
const CancelableTaskTracker::IsCanceledCallback& is_canceled,
base::TaskRunner* task_runner,
const BaseSessionService::InternalGetCommandsCallback& callback,
ScopedVector<SessionCommand> commands) {
if (is_canceled.Run())
return;

if (task_runner->RunsTasksOnCurrentThread()) {
callback.Run(commands.Pass());
} else {
task_runner->PostTask(FROM_HERE,
base::Bind(callback, base::Passed(&commands)));
}
}

} // namespace

// Delay between when a command is received, and when we save it to the
Expand Down Expand Up @@ -259,16 +266,22 @@ bool BaseSessionService::ShouldTrackEntry(const GURL& url) {
return url.is_valid();
}

BaseSessionService::Handle BaseSessionService::ScheduleGetLastSessionCommands(
InternalGetCommandsRequest* request,
CancelableRequestConsumerBase* consumer) {
scoped_refptr<InternalGetCommandsRequest> request_wrapper(request);
AddRequest(request, consumer);
CancelableTaskTracker::TaskId
BaseSessionService::ScheduleGetLastSessionCommands(
const InternalGetCommandsCallback& callback,
CancelableTaskTracker* tracker) {
CancelableTaskTracker::IsCanceledCallback is_canceled;
CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled);

InternalGetCommandsCallback callback_runner =
base::Bind(&RunIfNotCanceled,
is_canceled, base::MessageLoopProxy::current(), callback);

RunTaskOnBackendThread(
FROM_HERE,
base::Bind(&SessionBackend::ReadLastSessionCommands, backend(),
request_wrapper));
return request->handle();
is_canceled, callback_runner));
return id;
}

bool BaseSessionService::RunTaskOnBackendThread(
Expand Down
35 changes: 9 additions & 26 deletions chrome/browser/sessions/base_session_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
#include "base/gtest_prod_util.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/common/cancelable_request.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/common/cancelable_task_tracker.h"
#include "googleurl/src/gurl.h"

class Profile;
Expand Down Expand Up @@ -48,29 +50,9 @@ class BaseSessionService : public CancelableRequestProvider {
// Deletes the last session.
void DeleteLastSession();

class InternalGetCommandsRequest;

typedef base::Callback<void(Handle,
scoped_refptr<InternalGetCommandsRequest>)>
typedef base::Callback<void(ScopedVector<SessionCommand>)>
InternalGetCommandsCallback;

// Callback used when fetching the last session. The last session consists
// of a vector of SessionCommands.
class InternalGetCommandsRequest :
public CancelableRequest<InternalGetCommandsCallback> {
public:
explicit InternalGetCommandsRequest(const CallbackType& callback);

// The commands. The backend fills this in for us.
std::vector<SessionCommand*> commands;

protected:
virtual ~InternalGetCommandsRequest();

private:
DISALLOW_COPY_AND_ASSIGN(InternalGetCommandsRequest);
};

protected:
virtual ~BaseSessionService();

Expand Down Expand Up @@ -160,11 +142,12 @@ class BaseSessionService : public CancelableRequestProvider {
// Returns true if the entry at specified |url| should be written to disk.
bool ShouldTrackEntry(const GURL& url);

// Invokes ReadLastSessionCommands with request on the backend thread.
// If testing, ReadLastSessionCommands is invoked directly.
Handle ScheduleGetLastSessionCommands(
InternalGetCommandsRequest* request,
CancelableRequestConsumerBase* consumer);
// Invokes SessionBackend::ReadLastSessionCommands with callback on the
// backend thread.
// If testing, SessionBackend::ReadLastSessionCommands is invoked directly.
CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands(
const InternalGetCommandsCallback& callback,
CancelableTaskTracker* tracker);

// In production, this posts the task to the FILE thread. For
// tests, it immediately runs the specified task on the current
Expand Down
48 changes: 18 additions & 30 deletions chrome/browser/sessions/persistent_tab_restore_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_service_factory.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/common/cancelable_task_tracker.h"
#include "content/public/browser/session_storage_namespace.h"

namespace {
Expand Down Expand Up @@ -178,14 +179,11 @@ 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(
Handle handle,
scoped_refptr<InternalGetCommandsRequest> request);
void OnGotLastSessionCommands(ScopedVector<SessionCommand> commands);

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

// Validates all entries in |entries|, deleting any with no navigations. This
// also deletes any entries beyond the max number of entries we can hold.
Expand All @@ -195,8 +193,7 @@ class PersistentTabRestoreService::Delegate
// previous session. This creates and add entries to |staging_entries_| and
// invokes LoadStateChanged. |ignored_active_window| is ignored because we
// don't need to restore activation.
void OnGotPreviousSession(Handle handle,
std::vector<SessionWindow*>* windows,
void OnGotPreviousSession(ScopedVector<SessionWindow> windows,
SessionID::id_type ignored_active_window);

// Converts a SessionWindow into a Window, returning true on success. We use 0
Expand Down Expand Up @@ -233,11 +230,8 @@ class PersistentTabRestoreService::Delegate
// LoadStateChanged is invoked, which adds these entries to entries_.
std::vector<Entry*> staging_entries_;

// Used when loading previous tabs/session.
CancelableRequestConsumer load_consumer_;

// Used when loading open tabs/session when recovering from a crash.
CancelableRequestConsumer crash_consumer_;
// Used when loading previous tabs/session and open tabs/session.
CancelableTaskTracker cancelable_task_tracker_;

DISALLOW_COPY_AND_ASSIGN(Delegate);
};
Expand Down Expand Up @@ -348,8 +342,8 @@ void PersistentTabRestoreService::Delegate::LoadTabsFromLastSession() {
// that we need to load the windows from session service (which will have
// saved them).
session_service->GetLastSession(
&crash_consumer_,
base::Bind(&Delegate::OnGotPreviousSession, base::Unretained(this)));
base::Bind(&Delegate::OnGotPreviousSession, base::Unretained(this)),
&cancelable_task_tracker_);
} else {
load_state_ |= LOADED_LAST_SESSION;
}
Expand All @@ -359,10 +353,8 @@ void PersistentTabRestoreService::Delegate::LoadTabsFromLastSession() {
// this won't contain the tabs/window that were open at the point of the
// crash (the call to GetLastSession above requests those).
ScheduleGetLastSessionCommands(
new InternalGetCommandsRequest(
base::Bind(&Delegate::OnGotLastSessionCommands,
base::Unretained(this))),
&load_consumer_);
base::Bind(&Delegate::OnGotLastSessionCommands, base::Unretained(this)),
&cancelable_task_tracker_);
}

bool PersistentTabRestoreService::Delegate::IsLoaded() const {
Expand Down Expand Up @@ -550,10 +542,9 @@ int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist(
}

void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands(
Handle handle,
scoped_refptr<InternalGetCommandsRequest> request) {
ScopedVector<SessionCommand> commands) {
std::vector<Entry*> entries;
CreateEntriesFromCommands(request, &entries);
CreateEntriesFromCommands(commands.get(), &entries);
// Closed tabs always go to the end.
staging_entries_.insert(staging_entries_.end(), entries.begin(),
entries.end());
Expand All @@ -562,13 +553,11 @@ void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands(
}

void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
scoped_refptr<InternalGetCommandsRequest> request,
const std::vector<SessionCommand*>& commands,
std::vector<Entry*>* loaded_entries) {
if (request->canceled() ||
tab_restore_service_helper_->entries().size() == kMaxEntries)
if (tab_restore_service_helper_->entries().size() == kMaxEntries)
return;

std::vector<SessionCommand*>& commands = request->commands;
// Iterate through the commands populating entries and id_to_entry.
ScopedVector<Entry> entries;
IDToEntry id_to_entry;
Expand Down Expand Up @@ -780,11 +769,10 @@ void PersistentTabRestoreService::Delegate::ValidateAndDeleteEmptyEntries(
}

void PersistentTabRestoreService::Delegate::OnGotPreviousSession(
Handle handle,
std::vector<SessionWindow*>* windows,
ScopedVector<SessionWindow> windows,
SessionID::id_type ignored_active_window) {
std::vector<Entry*> entries;
CreateEntriesFromWindows(windows, &entries);
CreateEntriesFromWindows(&windows.get(), &entries);
// Previous session tabs go first.
staging_entries_.insert(staging_entries_.begin(), entries.begin(),
entries.end());
Expand Down
21 changes: 8 additions & 13 deletions chrome/browser/sessions/session_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,16 @@ void SessionBackend::AppendCommands(
}

void SessionBackend::ReadLastSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) {
if (request->canceled())
const CancelableTaskTracker::IsCanceledCallback& is_canceled,
const BaseSessionService::InternalGetCommandsCallback& callback) {
if (is_canceled.Run())
return;

Init();
ReadLastSessionCommandsImpl(&(request->commands));
request->ForwardResult(request->handle(), request);

ScopedVector<SessionCommand> commands;
ReadLastSessionCommandsImpl(&(commands.get()));
callback.Run(commands.Pass());
}

bool SessionBackend::ReadLastSessionCommandsImpl(
Expand Down Expand Up @@ -294,15 +298,6 @@ void SessionBackend::MoveCurrentSessionToLastSession() {
ResetFile();
}

void SessionBackend::ReadCurrentSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) {
if (request->canceled())
return;
Init();
ReadCurrentSessionCommandsImpl(&(request->commands));
request->ForwardResult(request->handle(), request);
}

bool SessionBackend::ReadCurrentSessionCommandsImpl(
std::vector<SessionCommand*>* commands) {
Init();
Expand Down
9 changes: 3 additions & 6 deletions chrome/browser/sessions/session_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/sessions/base_session_service.h"
#include "chrome/browser/sessions/session_command.h"
#include "chrome/common/cancelable_task_tracker.h"

namespace net {
class FileStream;
Expand Down Expand Up @@ -66,7 +67,8 @@ class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> {
// Invoked from the service to read the commands that make up the last
// session, invokes ReadLastSessionCommandsImpl to do the work.
void ReadLastSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request);
const CancelableTaskTracker::IsCanceledCallback& is_canceled,
const BaseSessionService::InternalGetCommandsCallback& callback);

// Reads the commands from the last file.
//
Expand All @@ -82,11 +84,6 @@ class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> {
// browsers are running.
void MoveCurrentSessionToLastSession();

// Invoked from the service to read the commands that make up the current
// session, invokes ReadCurrentSessionCommandsImpl to do the work.
void ReadCurrentSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request);

// Reads the commands from the current file.
//
// On success, the read commands are added to commands. It is up to the
Expand Down
15 changes: 8 additions & 7 deletions chrome/browser/sessions/session_restore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/callback.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/metrics/histogram.h"
#include "base/platform_file.h"
#include "base/stl_util.h"
Expand All @@ -33,6 +34,7 @@
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
#include "chrome/common/cancelable_task_tracker.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/dom_storage_context.h"
Expand Down Expand Up @@ -523,8 +525,8 @@ class SessionRestoreImpl : public content::NotificationObserver {
SessionServiceFactory::GetForProfile(profile_);
DCHECK(session_service);
session_service->GetLastSession(
&request_consumer_,
base::Bind(&SessionRestoreImpl::OnGotSession, base::Unretained(this)));
base::Bind(&SessionRestoreImpl::OnGotSession, base::Unretained(this)),
&cancelable_task_tracker_);

if (synchronous_) {
{
Expand Down Expand Up @@ -711,8 +713,7 @@ class SessionRestoreImpl : public content::NotificationObserver {
return browser;
}

void OnGotSession(SessionService::Handle handle,
std::vector<SessionWindow*>* windows,
void OnGotSession(ScopedVector<SessionWindow> windows,
SessionID::id_type active_window_id) {
base::TimeDelta time_to_got_sessions =
base::TimeTicks::Now() - restore_started_;
Expand All @@ -728,13 +729,13 @@ class SessionRestoreImpl : public content::NotificationObserver {
#endif
if (synchronous_) {
// See comment above windows_ as to why we don't process immediately.
windows_.swap(*windows);
windows_.swap(windows.get());
active_window_id_ = active_window_id;
MessageLoop::current()->QuitNow();
return;
}

ProcessSessionWindows(windows, active_window_id);
ProcessSessionWindows(&windows.get(), active_window_id);
}

Browser* ProcessSessionWindows(std::vector<SessionWindow*>* windows,
Expand Down Expand Up @@ -1103,7 +1104,7 @@ class SessionRestoreImpl : public content::NotificationObserver {
std::vector<GURL> urls_to_open_;

// Used to get the session.
CancelableRequestConsumer request_consumer_;
CancelableTaskTracker cancelable_task_tracker_;

// Responsible for loading the tabs.
scoped_refptr<TabLoader> tab_loader_;
Expand Down
Loading

0 comments on commit 4c7f9c5

Please sign in to comment.