Commit f9770c4d authored by oshima@chromium.org's avatar oshima@chromium.org

Switch BaseSessionService to use SequencedWorkerPool instead of FILE thread

Removed RunningInProduction and changed SessionServiceTestHelper to flush IO tasks when service is deleted.

BUG=261414

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217003 0039d316-1c4b-4281-b951-d872f2087c98
parent a6448856
......@@ -77,23 +77,15 @@ BaseSessionService::BaseSessionService(SessionType type,
: profile_(profile),
weak_factory_(this),
pending_reset_(false),
commands_since_reset_(0) {
commands_since_reset_(0),
sequence_token_(
content::BrowserThread::GetBlockingPool()->GetSequenceToken()) {
if (profile) {
// We should never be created when incognito.
DCHECK(!profile->IsOffTheRecord());
}
backend_ = new SessionBackend(type, profile_ ? profile_->GetPath() : path);
DCHECK(backend_.get());
// SessionBackend::Init() cannot be scheduled to be called here. There are
// service processes which create the BaseSessionService, but they should not
// initialize the backend. If they do, the backend will cycle the session
// restore files. That in turn prevents the session restore from working when
// the normal chromium process is launched. Normally, the backend will be
// initialized before it's actually used. However, if we're running as a part
// of a test, it must be initialized now.
if (!RunningInProduction())
backend_->Init();
}
BaseSessionService::~BaseSessionService() {
......@@ -299,17 +291,17 @@ CancelableTaskTracker::TaskId
bool BaseSessionService::RunTaskOnBackendThread(
const tracked_objects::Location& from_here,
const base::Closure& task) {
if (profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
return BrowserThread::PostTask(BrowserThread::FILE, from_here, task);
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
if (!pool->IsShutdownInProgress()) {
return pool->PostSequencedWorkerTask(sequence_token_,
from_here,
task);
} else {
// Fall back to executing on the main thread if the file thread
// has gone away (around shutdown time) or if we're running as
// part of a unit test that does not set profile_.
// Fall back to executing on the main thread if the sequence
// worker pool has been requested to shutdown (around shutdown
// time).
task.Run();
return true;
}
}
bool BaseSessionService::RunningInProduction() const {
return profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE);
}
......@@ -13,6 +13,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/common/cancelable_request.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/common/cancelable_task_tracker.h"
......@@ -154,16 +155,11 @@ class BaseSessionService : public CancelableRequestProvider {
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
// thread.
// This posts the task to the SequencedWorkerPool, or run immediately
// if the SequencedWorkerPool has been shutdown.
bool RunTaskOnBackendThread(const tracked_objects::Location& from_here,
const base::Closure& task);
// Returns true if we appear to be running in production, false if we appear
// to be running as part of a unit test or if the FILE thread has gone away.
bool RunningInProduction() const;
// Max number of navigation entries in each direction we'll persist.
static const int max_persist_navigation_count;
......@@ -189,6 +185,9 @@ class BaseSessionService : public CancelableRequestProvider {
// The number of commands sent to the backend before doing a reset.
int commands_since_reset_;
// A token to make sure that all tasks will be serialized.
base::SequencedWorkerPool::SequenceToken sequence_token_;
DISALLOW_COPY_AND_ASSIGN(BaseSessionService);
};
......
......@@ -8,6 +8,7 @@
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_service.h"
......@@ -116,6 +117,7 @@ class PersistentTabRestoreServiceTest : public ChromeRenderViewHostTestHarness {
// Must set service to null first so that it is destroyed before the new
// one is created.
service_->Shutdown();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
service_.reset();
service_.reset(new PersistentTabRestoreService(profile(), time_factory_));
SynchronousLoadTabsFromLastSession();
......@@ -159,7 +161,9 @@ class PersistentTabRestoreServiceTest : public ChromeRenderViewHostTestHarness {
void SynchronousLoadTabsFromLastSession() {
// Ensures that the load is complete before continuing.
service_->LoadTabsFromLastSession();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
base::RunLoop().RunUntilIdle();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
}
GURL url1_;
......
......@@ -10,6 +10,7 @@
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_types.h"
#include "components/sessions/serialized_navigation_entry_test_helper.h"
#include "content/public/browser/browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
......@@ -103,3 +104,10 @@ void SessionServiceTestHelper::AssertSingleWindowWithSingleTab(
SessionBackend* SessionServiceTestHelper::backend() {
return service_->backend();
}
void SessionServiceTestHelper::SetService(SessionService* service) {
service_.reset(service);
// Execute IO tasks posted by the SessionService.
content::BrowserThread::GetBlockingPool()->FlushForTesting();
}
......@@ -70,7 +70,7 @@ class SessionServiceTestHelper {
const std::vector<SessionWindow*>& windows,
size_t nav_count);
void set_service(SessionService* service) { service_.reset(service); }
void SetService(SessionService* service);
SessionService* ReleaseService() { return service_.release(); }
SessionService* service() { return service_.get(); }
......
......@@ -50,7 +50,7 @@ class SessionServiceTest : public BrowserWithTestWindowTest,
path_ = path_.AppendASCII(b);
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
helper_.SetService(session_service);
service()->SetWindowType(
window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
......@@ -68,7 +68,7 @@ class SessionServiceTest : public BrowserWithTestWindowTest,
}
virtual void TearDown() {
helper_.set_service(NULL);
helper_.SetService(NULL);
BrowserWithTestWindowTest::TearDown();
}
......@@ -87,10 +87,10 @@ class SessionServiceTest : public BrowserWithTestWindowTest,
void ReadWindows(std::vector<SessionWindow*>* windows,
SessionID::id_type* active_window_id) {
// Forces closing the file.
helper_.set_service(NULL);
helper_.SetService(NULL);
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
helper_.SetService(session_service);
SessionID::id_type* non_null_active_window_id = active_window_id;
SessionID::id_type dummy_active_window_id = 0;
......@@ -856,12 +856,12 @@ TEST_F(SessionServiceTest, CanOpenV1TabClosed) {
dest_file_path = dest_file_path.AppendASCII("Current Session");
// Forces closing the file.
helper_.set_service(NULL);
helper_.SetService(NULL);
ASSERT_TRUE(base::CopyFile(v1_file_path, dest_file_path));
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
helper_.SetService(session_service);
ScopedVector<SessionWindow> windows;
SessionID::id_type active_window_id = 0;
helper_.ReadWindows(&(windows.get()), &active_window_id);
......
......@@ -3813,7 +3813,7 @@ class NavigationControllerHistoryTest : public NavigationControllerTest {
controller.session_id(), 0);
controller.SetWindowID(window_id);
session_helper_.set_service(service);
session_helper_.SetService(service);
}
virtual void TearDown() {
......@@ -3841,12 +3841,12 @@ class NavigationControllerHistoryTest : public NavigationControllerTest {
// Deletes the current profile manager and creates a new one. Indirectly this
// shuts down the history database and reopens it.
void ReopenDatabase() {
session_helper_.set_service(NULL);
session_helper_.SetService(NULL);
SessionServiceFactory::SetForTestProfile(profile(), NULL);
SessionService* service = new SessionService(profile());
SessionServiceFactory::SetForTestProfile(profile(), service);
session_helper_.set_service(service);
session_helper_.SetService(service);
}
void GetLastSession() {
......
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