Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: content/browser/service_worker/service_worker_storage.cc

Issue 294593002: ServiceWorker: Support longest-prefix-match for registration scope (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_storage.h" 5 #include "content/browser/service_worker/service_worker_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 NOTREACHED(); 675 NOTREACHED();
676 // TODO(michaeln): Hmmm, what if DeleteReg was invoked after 676 // TODO(michaeln): Hmmm, what if DeleteReg was invoked after
677 // the Find result we're returning here? NOTREACHED condition? 677 // the Find result we're returning here? NOTREACHED condition?
678 return registration; 678 return registration;
679 } 679 }
680 680
681 ServiceWorkerRegistration* 681 ServiceWorkerRegistration*
682 ServiceWorkerStorage::FindInstallingRegistrationForDocument( 682 ServiceWorkerStorage::FindInstallingRegistrationForDocument(
683 const GURL& document_url) { 683 const GURL& document_url) {
684 DCHECK(!document_url.has_ref()); 684 DCHECK(!document_url.has_ref());
685 // TODO(michaeln): if there are multiple matches the one with 685
686 // the longest scope should win. 686 LongestScopeMatcher matcher(document_url);
687 ServiceWorkerRegistration* match = NULL;
688
689 // TODO(nhiroki): This searches over installing registrations linearly and it
690 // couldn't be scalable. Maybe the regs should be partitioned by origin.
687 for (RegistrationRefsById::const_iterator it = 691 for (RegistrationRefsById::const_iterator it =
688 installing_registrations_.begin(); 692 installing_registrations_.begin();
689 it != installing_registrations_.end(); ++it) { 693 it != installing_registrations_.end(); ++it) {
690 if (ServiceWorkerUtils::ScopeMatches( 694 if (matcher.MatchLongest(it->second->pattern()))
691 it->second->pattern(), document_url)) { 695 match = it->second;
692 return it->second;
693 }
694 } 696 }
695 return NULL; 697 return match;
696 } 698 }
697 699
698 ServiceWorkerRegistration* 700 ServiceWorkerRegistration*
699 ServiceWorkerStorage::FindInstallingRegistrationForPattern( 701 ServiceWorkerStorage::FindInstallingRegistrationForPattern(
700 const GURL& scope) { 702 const GURL& scope) {
701 for (RegistrationRefsById::const_iterator it = 703 for (RegistrationRefsById::const_iterator it =
702 installing_registrations_.begin(); 704 installing_registrations_.begin();
703 it != installing_registrations_.end(); ++it) { 705 it != installing_registrations_.end(); ++it) {
704 if (it->second->pattern() == scope) 706 if (it->second->pattern() == scope)
705 return it->second; 707 return it->second;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 if (status != ServiceWorkerDatabase::STATUS_OK) { 891 if (status != ServiceWorkerDatabase::STATUS_OK) {
890 original_task_runner->PostTask( 892 original_task_runner->PostTask(
891 FROM_HERE, 893 FROM_HERE,
892 base::Bind(callback, 894 base::Bind(callback,
893 ServiceWorkerDatabase::RegistrationData(), 895 ServiceWorkerDatabase::RegistrationData(),
894 ResourceList(), 896 ResourceList(),
895 status)); 897 status));
896 return; 898 return;
897 } 899 }
898 900
899 // Find one with a pattern match.
900 // TODO(michaeln): if there are multiple matches the one with
901 // the longest scope should win.
902 ServiceWorkerDatabase::RegistrationData data; 901 ServiceWorkerDatabase::RegistrationData data;
903 ResourceList resources; 902 ResourceList resources;
904 status = ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; 903 status = ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND;
905 for (RegistrationList::const_iterator it = registrations.begin(); 904
906 it != registrations.end(); ++it) { 905 // Find one with a pattern match.
907 if (!ServiceWorkerUtils::ScopeMatches(it->scope, document_url)) 906 LongestScopeMatcher matcher(document_url);
908 continue; 907 int64 match = kInvalidServiceWorkerRegistrationId;
909 status = database->ReadRegistration(it->registration_id, origin, 908 for (size_t i = 0; i < registrations.size(); ++i) {
910 &data, &resources); 909 if (matcher.MatchLongest(registrations[i].scope))
911 break; // We're done looping. 910 match = registrations[i].registration_id;
912 } 911 }
913 912
913 if (match != kInvalidServiceWorkerRegistrationId)
914 status = database->ReadRegistration(match, origin, &data, &resources);
915
914 original_task_runner->PostTask( 916 original_task_runner->PostTask(
915 FROM_HERE, 917 FROM_HERE,
916 base::Bind(callback, data, resources, status)); 918 base::Bind(callback, data, resources, status));
917 } 919 }
918 920
919 void ServiceWorkerStorage::FindForPatternInDB( 921 void ServiceWorkerStorage::FindForPatternInDB(
920 ServiceWorkerDatabase* database, 922 ServiceWorkerDatabase* database,
921 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 923 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
922 const GURL& scope, 924 const GURL& scope,
923 const FindInDBCallback& callback) { 925 const FindInDBCallback& callback) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 const FindInDBCallback& callback) { 963 const FindInDBCallback& callback) {
962 ServiceWorkerDatabase::RegistrationData data; 964 ServiceWorkerDatabase::RegistrationData data;
963 ResourceList resources; 965 ResourceList resources;
964 ServiceWorkerDatabase::Status status = 966 ServiceWorkerDatabase::Status status =
965 database->ReadRegistration(registration_id, origin, &data, &resources); 967 database->ReadRegistration(registration_id, origin, &data, &resources);
966 original_task_runner->PostTask( 968 original_task_runner->PostTask(
967 FROM_HERE, base::Bind(callback, data, resources, status)); 969 FROM_HERE, base::Bind(callback, data, resources, status));
968 } 970 }
969 971
970 } // namespace content 972 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698