Commit b4cad1b8 authored by jhawkins@chromium.org's avatar jhawkins@chromium.org

base::Bind: Convert more of chrome/browser/sessions.

BUG=none
TEST=none

R=csilv@chromium.org

Review URL: http://codereview.chromium.org/8677030

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111489 0039d316-1c4b-4281-b951-d872f2087c98
parent 31e0fead
......@@ -4,6 +4,7 @@
#include "chrome/browser/sessions/base_session_service.h"
#include "base/bind.h"
#include "base/pickle.h"
#include "base/stl_util.h"
#include "base/threading/thread.h"
......@@ -66,7 +67,7 @@ BaseSessionService::BaseSessionService(SessionType type,
: profile_(profile),
path_(path),
backend_thread_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(save_factory_(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
pending_reset_(false),
commands_since_reset_(0) {
if (profile) {
......@@ -89,8 +90,8 @@ void BaseSessionService::DeleteLastSession() {
if (!backend_thread()) {
backend()->DeleteLastSession();
} else {
backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
backend(), &SessionBackend::DeleteLastSession));
backend_thread()->message_loop()->PostTask(
FROM_HERE, base::Bind(&SessionBackend::DeleteLastSession, backend()));
}
}
......@@ -104,9 +105,10 @@ void BaseSessionService::ScheduleCommand(SessionCommand* command) {
void BaseSessionService::StartSaveTimer() {
// Don't start a timer when testing (profile == NULL or
// MessageLoop::current() is NULL).
if (MessageLoop::current() && profile() && save_factory_.empty()) {
MessageLoop::current()->PostDelayedTask(FROM_HERE,
save_factory_.NewRunnableMethod(&BaseSessionService::Save),
if (MessageLoop::current() && profile() && !weak_factory_.HasWeakPtrs()) {
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()),
kSaveDelayMS);
}
}
......@@ -121,10 +123,11 @@ void BaseSessionService::Save() {
backend()->AppendCommands(
new std::vector<SessionCommand*>(pending_commands_), pending_reset_);
} else {
backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
backend(), &SessionBackend::AppendCommands,
new std::vector<SessionCommand*>(pending_commands_),
pending_reset_));
backend_thread()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SessionBackend::AppendCommands, backend(),
new std::vector<SessionCommand*>(pending_commands_),
pending_reset_));
}
// Backend took ownership of commands.
pending_commands_.clear();
......@@ -259,8 +262,10 @@ BaseSessionService::Handle BaseSessionService::ScheduleGetLastSessionCommands(
scoped_refptr<InternalGetCommandsRequest> request_wrapper(request);
AddRequest(request, consumer);
if (backend_thread()) {
backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
backend(), &SessionBackend::ReadLastSessionCommands, request_wrapper));
backend_thread()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SessionBackend::ReadLastSessionCommands, backend(),
request_wrapper));
} else {
backend()->ReadLastSessionCommands(request);
}
......@@ -274,10 +279,10 @@ BaseSessionService::Handle
scoped_refptr<InternalGetCommandsRequest> request_wrapper(request);
AddRequest(request, consumer);
if (backend_thread()) {
backend_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(backend(),
&SessionBackend::ReadCurrentSessionCommands,
request_wrapper));
backend_thread()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SessionBackend::ReadCurrentSessionCommands, backend(),
request_wrapper));
} else {
backend()->ReadCurrentSessionCommands(request);
}
......
......@@ -7,9 +7,10 @@
#pragma once
#include "base/basictypes.h"
#include "base/callback_old.h"
#include "base/callback.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/sessions/session_id.h"
#include "content/browser/cancelable_request.h"
......@@ -55,7 +56,8 @@ class BaseSessionService : public CancelableRequestProvider,
class InternalGetCommandsRequest;
typedef Callback2<Handle, scoped_refptr<InternalGetCommandsRequest> >::Type
typedef base::Callback<void(Handle,
scoped_refptr<InternalGetCommandsRequest>)>
InternalGetCommandsCallback;
// Callback used when fetching the last session. The last session consists
......@@ -63,7 +65,7 @@ class BaseSessionService : public CancelableRequestProvider,
class InternalGetCommandsRequest :
public CancelableRequest<InternalGetCommandsCallback> {
public:
explicit InternalGetCommandsRequest(CallbackType* callback)
explicit InternalGetCommandsRequest(const CallbackType& callback)
: CancelableRequest<InternalGetCommandsCallback>(callback) {
}
......@@ -172,7 +174,7 @@ class BaseSessionService : public CancelableRequestProvider,
base::Thread* backend_thread_;
// Used to invoke Save.
ScopedRunnableMethodFactory<BaseSessionService> save_factory_;
base::WeakPtrFactory<BaseSessionService> weak_factory_;
// Commands we need to send over to the backend.
std::vector<SessionCommand*> pending_commands_;
......
......@@ -244,9 +244,7 @@ void SessionBackend::ReadLastSessionCommands(
return;
Init();
ReadLastSessionCommandsImpl(&(request->commands));
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
request->ForwardResult(request->handle(), request);
}
bool SessionBackend::ReadLastSessionCommandsImpl(
......@@ -297,9 +295,7 @@ void SessionBackend::ReadCurrentSessionCommands(
return;
Init();
ReadCurrentSessionCommandsImpl(&(request->commands));
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
request->ForwardResult(request->handle(), request);
}
bool SessionBackend::ReadCurrentSessionCommandsImpl(
......
......@@ -9,6 +9,8 @@
#include <set>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop.h"
......@@ -76,7 +78,7 @@ class InternalSessionRequest
: public BaseSessionService::InternalGetCommandsRequest {
public:
InternalSessionRequest(
CallbackType* callback,
const CallbackType& callback,
SessionService::SessionCallback* real_callback)
: BaseSessionService::InternalGetCommandsRequest(callback),
real_callback(real_callback) {
......@@ -183,8 +185,9 @@ void SessionService::MoveCurrentSessionToLastSession() {
if (!backend_thread()) {
backend()->MoveCurrentSessionToLastSession();
} else {
backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
backend(), &SessionBackend::MoveCurrentSessionToLastSession));
backend_thread()->message_loop()->PostTask(
FROM_HERE, base::Bind(&SessionBackend::MoveCurrentSessionToLastSession,
backend()));
}
}
......@@ -419,8 +422,10 @@ SessionService::Handle SessionService::GetLastSession(
SessionCallback* callback) {
return ScheduleGetLastSessionCommands(
new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
callback), consumer);
base::Bind(&SessionService::OnGotSessionCommands,
base::Unretained(this)),
callback),
consumer);
}
SessionService::Handle SessionService::GetCurrentSession(
......@@ -430,7 +435,8 @@ SessionService::Handle SessionService::GetCurrentSession(
// If there are no pending window closes, we can get the current session
// from memory.
scoped_refptr<InternalSessionRequest> request(new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
base::Bind(&SessionService::OnGotSessionCommands,
base::Unretained(this)),
callback));
AddRequest(request, consumer);
IdToRange tab_to_available_range;
......@@ -438,16 +444,16 @@ SessionService::Handle SessionService::GetCurrentSession(
BuildCommandsFromBrowsers(&(request->commands),
&tab_to_available_range,
&windows_to_track);
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
request->ForwardResult(request->handle(), request);
return request->handle();
} else {
// If there are pending window closes, read the current session from disk.
return ScheduleGetCurrentSessionCommands(
new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
callback), consumer);
base::Bind(&SessionService::OnGotSessionCommands,
base::Unretained(this)),
callback),
consumer);
}
}
......
......@@ -10,7 +10,7 @@
#include <string>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/callback_old.h"
#include "base/time.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/sessions/base_session_service.h"
......
......@@ -8,6 +8,8 @@
#include <iterator>
#include <map>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/memory/scoped_vector.h"
#include "base/metrics/histogram.h"
......@@ -439,7 +441,8 @@ void TabRestoreService::LoadTabsFromLastSession() {
// crash (the call to GetLastSession above requests those).
ScheduleGetLastSessionCommands(
new InternalGetCommandsRequest(
NewCallback(this, &TabRestoreService::OnGotLastSessionCommands)),
base::Bind(&TabRestoreService::OnGotLastSessionCommands,
base::Unretained(this))),
&load_consumer_);
}
......
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