diff options
author | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 20:51:06 +0000 |
---|---|---|
committer | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 20:51:06 +0000 |
commit | 15f9cdedac1784a85dca7867858c905f0ed90d2b (patch) | |
tree | bb001e718240c0fd86aa43a55f4a76e978b72880 /webkit | |
parent | b89f7e4d3f98fffe88bd07a57c735e28c37e692c (diff) | |
download | chromium_src-15f9cdedac1784a85dca7867858c905f0ed90d2b.zip chromium_src-15f9cdedac1784a85dca7867858c905f0ed90d2b.tar.gz chromium_src-15f9cdedac1784a85dca7867858c905f0ed90d2b.tar.bz2 |
Finally getting to the appcache parts instead of all of this webkitApi/ipc plumbing craziness.
BUG=39368
TEST=new unittests
Review URL: http://codereview.chromium.org/2121002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47844 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/appcache/appcache_host.cc | 50 | ||||
-rw-r--r-- | webkit/appcache/appcache_host.h | 28 | ||||
-rw-r--r-- | webkit/appcache/appcache_host_unittest.cc | 36 | ||||
-rw-r--r-- | webkit/appcache/appcache_interceptor.cc | 5 | ||||
-rw-r--r-- | webkit/appcache/appcache_request_handler.cc | 38 | ||||
-rw-r--r-- | webkit/appcache/appcache_request_handler.h | 20 | ||||
-rw-r--r-- | webkit/appcache/appcache_request_handler_unittest.cc | 113 |
7 files changed, 237 insertions, 53 deletions
diff --git a/webkit/appcache/appcache_host.cc b/webkit/appcache/appcache_host.cc index d4ca661..6d0336b 100644 --- a/webkit/appcache/appcache_host.cc +++ b/webkit/appcache/appcache_host.cc @@ -6,13 +6,15 @@ #include "base/logging.h" #include "webkit/appcache/appcache.h" +#include "webkit/appcache/appcache_backend_impl.h" #include "webkit/appcache/appcache_request_handler.h" namespace appcache { AppCacheHost::AppCacheHost(int host_id, AppCacheFrontend* frontend, AppCacheService* service) - : host_id_(host_id), pending_main_resource_cache_id_(kNoCacheId), + : host_id_(host_id), parent_host_id_(kNoHostId), parent_process_id_(0), + pending_main_resource_cache_id_(kNoCacheId), pending_selected_cache_id_(kNoCacheId), frontend_(frontend), service_(service), pending_get_status_callback_(NULL), pending_start_update_callback_(NULL), @@ -93,6 +95,31 @@ void AppCacheHost::SelectCache(const GURL& document_url, FinishCacheSelection(NULL, NULL); } +void AppCacheHost::SelectCacheForWorker(int parent_process_id, + int parent_host_id) { + DCHECK(!pending_start_update_callback_ && + !pending_swap_cache_callback_ && + !pending_get_status_callback_ && + !is_selection_pending()); + + parent_process_id_ = parent_process_id; + parent_host_id_ = parent_host_id; + FinishCacheSelection(NULL, NULL); +} + +void AppCacheHost::SelectCacheForSharedWorker(int64 appcache_id) { + DCHECK(!pending_start_update_callback_ && + !pending_swap_cache_callback_ && + !pending_get_status_callback_ && + !is_selection_pending()); + + if (appcache_id != kNoCacheId) { + LoadSelectedCache(appcache_id); + return; + } + FinishCacheSelection(NULL, NULL); +} + // TODO(michaeln): change method name to MarkEntryAsForeign for consistency void AppCacheHost::MarkAsForeignEntry(const GURL& document_url, int64 cache_document_was_loaded_from) { @@ -197,15 +224,28 @@ void AppCacheHost::DoPendingSwapCache() { pending_callback_param_ = NULL; } +AppCacheHost* AppCacheHost::GetParentAppCacheHost() const { + DCHECK(is_for_dedicated_worker()); + AppCacheBackendImpl* backend = service_->GetBackend(parent_process_id_); + return backend ? backend->GetHost(parent_host_id_) : NULL; +} + AppCacheRequestHandler* AppCacheHost::CreateRequestHandler( URLRequest* request, - bool is_main_request) { - if (is_main_request) - return new AppCacheRequestHandler(this, true); + ResourceType::Type resource_type) { + if (is_for_dedicated_worker()) { + AppCacheHost* parent_host = GetParentAppCacheHost(); + if (parent_host) + return parent_host->CreateRequestHandler(request, resource_type); + return NULL; + } + + if (AppCacheRequestHandler::IsMainResourceType(resource_type)) + return new AppCacheRequestHandler(this, resource_type); if ((associated_cache() && associated_cache()->is_complete()) || is_selection_pending()) { - return new AppCacheRequestHandler(this, false); + return new AppCacheRequestHandler(this, resource_type); } return NULL; } diff --git a/webkit/appcache/appcache_host.h b/webkit/appcache/appcache_host.h index 2a2607e..1c7534f 100644 --- a/webkit/appcache/appcache_host.h +++ b/webkit/appcache/appcache_host.h @@ -14,6 +14,7 @@ #include "webkit/appcache/appcache_interfaces.h" #include "webkit/appcache/appcache_service.h" #include "webkit/appcache/appcache_storage.h" +#include "webkit/glue/resource_type.h" class URLRequest; @@ -56,6 +57,9 @@ class AppCacheHost : public AppCacheStorage::Delegate, void SelectCache(const GURL& document_url, const int64 cache_document_was_loaded_from, const GURL& manifest_url); + void SelectCacheForWorker(int parent_process_id, + int parent_host_id); + void SelectCacheForSharedWorker(int64 appcache_id); void MarkAsForeignEntry(const GURL& document_url, int64 cache_document_was_loaded_from); void GetStatusWithCallback(GetStatusCallback* callback, @@ -66,9 +70,9 @@ class AppCacheHost : public AppCacheStorage::Delegate, void* callback_param); // Support for loading resources out of the appcache. - // Returns NULL if the host is not associated with a complete cache. - AppCacheRequestHandler* CreateRequestHandler(URLRequest* request, - bool is_main_request); + // May return NULL if the request isn't subject to retrieval from an appache. + AppCacheRequestHandler* CreateRequestHandler( + URLRequest* request, ResourceType::Type resource_type); // Establishes an association between this host and a cache. 'cache' may be // NULL to break any existing association. Associations are established @@ -124,9 +128,26 @@ class AppCacheHost : public AppCacheStorage::Delegate, virtual void OnContentBlocked(AppCacheGroup* group); virtual void OnUpdateComplete(AppCacheGroup* group); + // Returns true if this host is for a dedicated worker context. + bool is_for_dedicated_worker() const { + return parent_host_id_ != kNoHostId; + } + + // Returns the parent context's host instance. This is only valid + // to call when this instance is_for_dedicated_worker. + AppCacheHost* GetParentAppCacheHost() const; + // Identifies the corresponding appcache host in the child process. int host_id_; + // Hosts for dedicated workers are special cased to shunt + // request handling off to the dedicated worker's parent. + // The scriptable api is not accessible in dedicated workers + // so the other aspects of this class are not relevant for + // these special case instances. + int parent_host_id_; + int parent_process_id_; + // The cache associated with this host, if any. scoped_refptr<AppCache> associated_cache_; @@ -190,6 +211,7 @@ class AppCacheHost : public AppCacheStorage::Delegate, FRIEND_TEST(AppCacheHostTest, FailedCacheLoad); FRIEND_TEST(AppCacheHostTest, FailedGroupLoad); FRIEND_TEST(AppCacheHostTest, SetSwappableCache); + FRIEND_TEST(AppCacheHostTest, ForDedicatedWorker); FRIEND_TEST(AppCacheGroupTest, QueueUpdate); DISALLOW_COPY_AND_ASSIGN(AppCacheHost); }; diff --git a/webkit/appcache/appcache_host_unittest.cc b/webkit/appcache/appcache_host_unittest.cc index be287f7..85cc4b4 100644 --- a/webkit/appcache/appcache_host_unittest.cc +++ b/webkit/appcache/appcache_host_unittest.cc @@ -4,8 +4,10 @@ #include "base/callback.h" #include "base/scoped_ptr.h" +#include "net/url_request/url_request.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/appcache/appcache.h" +#include "webkit/appcache/appcache_backend_impl.h" #include "webkit/appcache/appcache_group.h" #include "webkit/appcache/appcache_host.h" #include "webkit/appcache/mock_appcache_service.h" @@ -301,5 +303,39 @@ TEST_F(AppCacheHostTest, SetSwappableCache) { EXPECT_FALSE(host.swappable_cache_.get()); // group2 had no newest cache } +TEST_F(AppCacheHostTest, ForDedicatedWorker) { + const int kMockProcessId = 1; + const int kParentHostId = 1; + const int kWorkerHostId = 2; + + AppCacheBackendImpl backend_impl; + backend_impl.Initialize(&service_, &mock_frontend_, kMockProcessId); + backend_impl.RegisterHost(kParentHostId); + backend_impl.RegisterHost(kWorkerHostId); + + AppCacheHost* parent_host = backend_impl.GetHost(kParentHostId); + EXPECT_FALSE(parent_host->is_for_dedicated_worker()); + + AppCacheHost* worker_host = backend_impl.GetHost(kWorkerHostId); + worker_host->SelectCacheForWorker(kParentHostId, kMockProcessId); + EXPECT_TRUE(worker_host->is_for_dedicated_worker()); + EXPECT_EQ(parent_host, worker_host->GetParentAppCacheHost()); + + // We should have received an OnCacheSelected msg for the worker_host. + // The host for workers always indicates 'no cache selected' regardless + // of its parent's state. This is OK because the worker cannot access + // the scriptable interface, the only function available is resource + // loading (see appcache_request_handler_unittests those tests). + EXPECT_EQ(kWorkerHostId, mock_frontend_.last_host_id_); + EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); + EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); + + // Simulate the parent being torn down. + backend_impl.UnregisterHost(kParentHostId); + parent_host = NULL; + EXPECT_EQ(NULL, backend_impl.GetHost(kParentHostId)); + EXPECT_EQ(NULL, worker_host->GetParentAppCacheHost()); +} + } // namespace appcache diff --git a/webkit/appcache/appcache_interceptor.cc b/webkit/appcache/appcache_interceptor.cc index e5f7747..6fcd322 100644 --- a/webkit/appcache/appcache_interceptor.cc +++ b/webkit/appcache/appcache_interceptor.cc @@ -39,12 +39,9 @@ void AppCacheInterceptor::SetExtraRequestInfo( if (!host) return; - // TODO(michaeln): SHARED_WORKER type too - bool is_main_request = ResourceType::IsFrame(resource_type); - // Create a handler for this request and associate it with the request. AppCacheRequestHandler* handler = - host->CreateRequestHandler(request, is_main_request); + host->CreateRequestHandler(request, resource_type); if (handler) SetHandler(request, handler); } diff --git a/webkit/appcache/appcache_request_handler.cc b/webkit/appcache/appcache_request_handler.cc index 41143b2..a0775ad 100644 --- a/webkit/appcache/appcache_request_handler.cc +++ b/webkit/appcache/appcache_request_handler.cc @@ -12,8 +12,8 @@ namespace appcache { AppCacheRequestHandler::AppCacheRequestHandler(AppCacheHost* host, - bool is_main_resource) - : host_(host), is_main_request_(is_main_resource), + ResourceType::Type resource_type) + : host_(host), resource_type_(resource_type), is_waiting_for_cache_selection_(false), found_cache_id_(0), found_network_namespace_(false) { DCHECK(host_); @@ -27,7 +27,7 @@ AppCacheRequestHandler::~AppCacheRequestHandler() { } } -AppCacheStorage* AppCacheRequestHandler::storage() { +AppCacheStorage* AppCacheRequestHandler::storage() const { DCHECK(host_); return host_->service()->storage(); } @@ -66,7 +66,7 @@ AppCacheURLRequestJob* AppCacheRequestHandler::MaybeLoadResource( found_manifest_url_ = GURL(); found_network_namespace_ = false; - if (is_main_request_) + if (is_main_resource()) MaybeLoadMainResource(request); else MaybeLoadSubResource(request); @@ -86,7 +86,7 @@ AppCacheURLRequestJob* AppCacheRequestHandler::MaybeLoadFallbackForRedirect( URLRequest* request, const GURL& location) { if (!host_ || !IsSchemeAndMethodSupported(request)) return NULL; - if (is_main_request_) + if (is_main_resource()) return NULL; if (request->url().GetOrigin() == location.GetOrigin()) return NULL; @@ -189,20 +189,26 @@ void AppCacheRequestHandler::OnMainResponseFound( int64 cache_id, const GURL& manifest_url, bool was_blocked_by_policy) { DCHECK(host_); - DCHECK(is_main_request_); + DCHECK(is_main_resource()); DCHECK(!entry.IsForeign()); DCHECK(!fallback_entry.IsForeign()); DCHECK(!(entry.has_response_id() && fallback_entry.has_response_id())); - if (was_blocked_by_policy) - host_->NotifyMainResourceBlocked(); - - if (cache_id != kNoCacheId) { - // AppCacheHost loads and holds a reference to the main resource cache - // for two reasons, firstly to preload the cache into the working set - // in advance of subresource loads happening, secondly to prevent the - // AppCache from falling out of the working set on frame navigations. - host_->LoadMainResourceCache(cache_id); + if (ResourceType::IsFrame(resource_type_)) { + if (was_blocked_by_policy) + host_->NotifyMainResourceBlocked(); + + if (cache_id != kNoCacheId) { + // AppCacheHost loads and holds a reference to the main resource cache + // for two reasons, firstly to preload the cache into the working set + // in advance of subresource loads happening, secondly to prevent the + // AppCache from falling out of the working set on frame navigations. + host_->LoadMainResourceCache(cache_id); + } + } else { + DCHECK(ResourceType::IsSharedWorker(resource_type_)); + if (was_blocked_by_policy) + host_->frontend()->OnContentBlocked(host_->host_id()); } // 6.11.1 Navigating across documents, steps 10 and 14. @@ -294,7 +300,7 @@ void AppCacheRequestHandler::ContinueMaybeLoadSubResource() { void AppCacheRequestHandler::OnCacheSelectionComplete(AppCacheHost* host) { DCHECK(host == host_); - if (is_main_request_) + if (is_main_resource()) return; if (!is_waiting_for_cache_selection_) return; diff --git a/webkit/appcache/appcache_request_handler.h b/webkit/appcache/appcache_request_handler.h index 3a4c057..77613f8 100644 --- a/webkit/appcache/appcache_request_handler.h +++ b/webkit/appcache/appcache_request_handler.h @@ -8,6 +8,7 @@ #include "net/url_request/url_request.h" #include "webkit/appcache/appcache_entry.h" #include "webkit/appcache/appcache_host.h" +#include "webkit/glue/resource_type.h" class URLRequest; class URLRequestJob; @@ -35,11 +36,16 @@ class AppCacheRequestHandler : public URLRequest::UserData, void GetExtraResponseInfo(int64* cache_id, GURL* manifest_url); + static bool IsMainResourceType(ResourceType::Type type) { + return ResourceType::IsFrame(type) || + ResourceType::IsSharedWorker(type); + } + private: friend class AppCacheHost; // Callers should use AppCacheHost::CreateRequestHandler. - AppCacheRequestHandler(AppCacheHost* host, bool is_main_resource); + AppCacheRequestHandler(AppCacheHost* host, ResourceType::Type resource_type); // AppCacheHost::Observer override virtual void OnDestructionImminent(AppCacheHost* host); @@ -52,9 +58,14 @@ class AppCacheRequestHandler : public URLRequest::UserData, void DeliverErrorResponse(); // Helper to retrieve a pointer to the storage object. - AppCacheStorage* storage(); + AppCacheStorage* storage() const; + + bool is_main_resource() const { + return IsMainResourceType(resource_type_); + } // Main-resource loading ------------------------------------- + // Frame and SharedWorker main resources are handled here. void MaybeLoadMainResource(URLRequest* request); @@ -66,6 +77,7 @@ class AppCacheRequestHandler : public URLRequest::UserData, bool was_blocked_by_policy); // Sub-resource loading ------------------------------------- + // Dedicated worker and all manner of sub-resources are handled here. void MaybeLoadSubResource(URLRequest* request); void ContinueMaybeLoadSubResource(); @@ -78,8 +90,8 @@ class AppCacheRequestHandler : public URLRequest::UserData, // What host we're servicing a request for. AppCacheHost* host_; - // Main vs subresource loads are somewhat different. - bool is_main_request_; + // Frame vs subresource vs sharedworker loads are somewhat different. + ResourceType::Type resource_type_; // Subresource requests wait until after cache selection completes. bool is_waiting_for_cache_selection_; diff --git a/webkit/appcache/appcache_request_handler_unittest.cc b/webkit/appcache/appcache_request_handler_unittest.cc index 83a0236..5ca8f4c1 100644 --- a/webkit/appcache/appcache_request_handler_unittest.cc +++ b/webkit/appcache/appcache_request_handler_unittest.cc @@ -10,12 +10,15 @@ #include "net/url_request/url_request_error_job.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/appcache/appcache.h" +#include "webkit/appcache/appcache_backend_impl.h" #include "webkit/appcache/appcache_request_handler.h" #include "webkit/appcache/appcache_url_request_job.h" #include "webkit/appcache/mock_appcache_service.h" namespace appcache { +static const int kMockProcessId = 1; + class AppCacheRequestHandlerTest : public testing::Test { public: class MockFrontend : public AppCacheFrontend { @@ -103,7 +106,7 @@ class AppCacheRequestHandlerTest : public testing::Test { // Test harness -------------------------------------------------- AppCacheRequestHandlerTest() - : orig_http_factory_(NULL) { + : host_(NULL), orig_http_factory_(NULL) { } template <class Method> @@ -120,8 +123,12 @@ class AppCacheRequestHandlerTest : public testing::Test { "http", MockHttpJobFactory); mock_service_.reset(new MockAppCacheService); mock_frontend_.reset(new MockFrontend); - host_.reset( - new AppCacheHost(1, mock_frontend_.get(), mock_service_.get())); + backend_impl_.reset(new AppCacheBackendImpl); + backend_impl_->Initialize(mock_service_.get(), mock_frontend_.get(), + kMockProcessId); + const int kHostId = 1; + backend_impl_->RegisterHost(kHostId); + host_ = backend_impl_->GetHost(kHostId); } void TearDownTest() { @@ -132,9 +139,10 @@ class AppCacheRequestHandlerTest : public testing::Test { job_ = NULL; handler_.reset(); request_.reset(); - host_.reset(); + backend_impl_.reset(); mock_frontend_.reset(); mock_service_.reset(); + host_ = NULL; } void TestFinished() { @@ -171,7 +179,8 @@ class AppCacheRequestHandlerTest : public testing::Test { this, &AppCacheRequestHandlerTest::Verify_MainResource_Miss)); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), true)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::MAIN_FRAME)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); @@ -209,7 +218,8 @@ class AppCacheRequestHandlerTest : public testing::Test { this, &AppCacheRequestHandlerTest::Verify_MainResource_Hit)); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), true)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::MAIN_FRAME)); EXPECT_TRUE(handler_.get()); mock_storage()->SimulateFindMainResource( @@ -248,7 +258,8 @@ class AppCacheRequestHandlerTest : public testing::Test { this, &AppCacheRequestHandlerTest::Verify_MainResource_Fallback)); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), true)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::MAIN_FRAME)); EXPECT_TRUE(handler_.get()); mock_storage()->SimulateFindMainResource( @@ -293,7 +304,8 @@ class AppCacheRequestHandlerTest : public testing::Test { void SubResource_Miss_WithNoCacheSelected() { request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); // We avoid creating handler when possible, sub-resource requests are not // subject to retrieval from an appcache when there's no associated cache. @@ -310,7 +322,8 @@ class AppCacheRequestHandlerTest : public testing::Test { host_->AssociateCache(MakeNewCache()); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); @@ -335,7 +348,8 @@ class AppCacheRequestHandlerTest : public testing::Test { host_->pending_selected_cache_id_ = cache->cache_id(); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_TRUE(job_.get()); @@ -364,7 +378,8 @@ class AppCacheRequestHandlerTest : public testing::Test { AppCacheEntry(AppCacheEntry::EXPLICIT, 1), AppCacheEntry(), false); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_TRUE(job_.get()); @@ -391,7 +406,8 @@ class AppCacheRequestHandlerTest : public testing::Test { AppCacheEntry(), AppCacheEntry(AppCacheEntry::EXPLICIT, 1), false); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_FALSE(job_.get()); @@ -419,7 +435,8 @@ class AppCacheRequestHandlerTest : public testing::Test { AppCacheEntry(), AppCacheEntry(AppCacheEntry::EXPLICIT, 1), false); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_FALSE(job_.get()); @@ -448,7 +465,8 @@ class AppCacheRequestHandlerTest : public testing::Test { AppCacheEntry(), AppCacheEntry(), true); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_FALSE(job_.get()); @@ -472,10 +490,12 @@ class AppCacheRequestHandlerTest : public testing::Test { AppCacheEntry(AppCacheEntry::EXPLICIT, 1), AppCacheEntry(), false); request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); - host_.reset(); + backend_impl_->UnregisterHost(1); + host_ = NULL; EXPECT_FALSE(handler_->MaybeLoadResource(request_.get())); EXPECT_FALSE(handler_->MaybeLoadFallbackForRedirect( @@ -492,14 +512,16 @@ class AppCacheRequestHandlerTest : public testing::Test { host_->pending_selected_cache_id_ = 1; request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); EXPECT_TRUE(job_.get()); EXPECT_TRUE(job_->is_waiting()); - host_.reset(); + backend_impl_->UnregisterHost(1); + host_ = NULL; EXPECT_TRUE(job_->has_been_killed()); EXPECT_FALSE(handler_->MaybeLoadResource(request_.get())); @@ -517,7 +539,8 @@ class AppCacheRequestHandlerTest : public testing::Test { host_->pending_selected_cache_id_ = 1; request_.reset(new MockURLRequest(GURL("ftp://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), false)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::SUB_RESOURCE)); EXPECT_TRUE(handler_.get()); // we could redirect to http (conceivably) EXPECT_FALSE(handler_->MaybeLoadResource(request_.get())); @@ -532,7 +555,8 @@ class AppCacheRequestHandlerTest : public testing::Test { void CanceledRequest() { request_.reset(new MockURLRequest(GURL("http://blah/"))); - handler_.reset(host_->CreateRequestHandler(request_.get(), true)); + handler_.reset(host_->CreateRequestHandler(request_.get(), + ResourceType::MAIN_FRAME)); EXPECT_TRUE(handler_.get()); job_ = handler_->MaybeLoadResource(request_.get()); @@ -552,6 +576,48 @@ class AppCacheRequestHandlerTest : public testing::Test { TestFinished(); } + // WorkerRequest ----------------------------- + + void WorkerRequest() { + EXPECT_TRUE(AppCacheRequestHandler::IsMainResourceType( + ResourceType::MAIN_FRAME)); + EXPECT_TRUE(AppCacheRequestHandler::IsMainResourceType( + ResourceType::SUB_FRAME)); + EXPECT_TRUE(AppCacheRequestHandler::IsMainResourceType( + ResourceType::SHARED_WORKER)); + EXPECT_FALSE(AppCacheRequestHandler::IsMainResourceType( + ResourceType::WORKER)); + + request_.reset(new MockURLRequest(GURL("http://blah/"))); + + const int kParentHostId = host_->host_id(); + const int kWorkerHostId = 2; + const int kAbandonedWorkerHostId = 3; + const int kNonExsitingHostId = 700; + + backend_impl_->RegisterHost(kWorkerHostId); + AppCacheHost* worker_host = backend_impl_->GetHost(kWorkerHostId); + worker_host->SelectCacheForWorker(kParentHostId, kMockProcessId); + handler_.reset(worker_host->CreateRequestHandler( + request_.get(), ResourceType::SHARED_WORKER)); + EXPECT_TRUE(handler_.get()); + // Verify that the handler is associated with the parent host. + EXPECT_EQ(host_, handler_->host_); + + // Create a new worker host, but associate it with a parent host that + // does not exists to simulate the host having been torn down. + backend_impl_->UnregisterHost(kWorkerHostId); + backend_impl_->RegisterHost(kAbandonedWorkerHostId); + worker_host = backend_impl_->GetHost(kAbandonedWorkerHostId); + EXPECT_EQ(NULL, backend_impl_->GetHost(kNonExsitingHostId)); + worker_host->SelectCacheForWorker(kNonExsitingHostId, kMockProcessId); + handler_.reset(worker_host->CreateRequestHandler( + request_.get(), ResourceType::SHARED_WORKER)); + EXPECT_FALSE(handler_.get()); + + TestFinished(); + } + // Test case helpers -------------------------------------------------- AppCache* MakeNewCache() { @@ -574,8 +640,9 @@ class AppCacheRequestHandlerTest : public testing::Test { scoped_ptr<base::WaitableEvent> test_finished_event_; std::stack<Task*> task_stack_; scoped_ptr<MockAppCacheService> mock_service_; + scoped_ptr<AppCacheBackendImpl> backend_impl_; scoped_ptr<MockFrontend> mock_frontend_; - scoped_ptr<AppCacheHost> host_; + AppCacheHost* host_; scoped_ptr<MockURLRequest> request_; scoped_ptr<AppCacheRequestHandler> handler_; scoped_refptr<AppCacheURLRequestJob> job_; @@ -650,6 +717,10 @@ TEST_F(AppCacheRequestHandlerTest, CanceledRequest) { RunTestOnIOThread(&AppCacheRequestHandlerTest::CanceledRequest); } +TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { + RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); +} + } // namespace appcache // AppCacheRequestHandlerTest is expected to always live longer than the |