OLD | NEW |
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 "chrome/common/search/search_urls.h" | 5 #include "chrome/common/search/search_urls.h" |
6 | 6 |
7 #include "content/public/common/url_constants.h" | 7 #include "content/public/common/url_constants.h" |
8 #include "url/gurl.h" | 8 #include "url/gurl.h" |
9 | 9 |
10 namespace search { | 10 namespace search { |
11 | 11 |
12 namespace { | 12 namespace { |
| 13 |
| 14 const char kServiceWorkerFileName[] = "newtab-serviceworker.js"; |
| 15 |
13 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { | 16 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { |
14 return my_url.host_piece() == other_url.host_piece() && | 17 return my_url.host_piece() == other_url.host_piece() && |
15 my_url.port() == other_url.port() && | 18 my_url.port() == other_url.port() && |
16 (my_url.scheme_piece() == other_url.scheme_piece() || | 19 (my_url.scheme_piece() == other_url.scheme_piece() || |
17 (my_url.SchemeIs(url::kHttpsScheme) && | 20 (my_url.SchemeIs(url::kHttpsScheme) && |
18 other_url.SchemeIs(url::kHttpScheme))); | 21 other_url.SchemeIs(url::kHttpScheme))); |
19 } | 22 } |
| 23 |
20 } // namespace | 24 } // namespace |
21 | 25 |
22 bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) { | 26 bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) { |
23 return MatchesOrigin(my_url, other_url) && | 27 return MatchesOrigin(my_url, other_url) && |
24 my_url.path_piece() == other_url.path_piece(); | 28 my_url.path_piece() == other_url.path_piece(); |
25 } | 29 } |
26 | 30 |
| 31 bool IsMatchingServiceWorker(const GURL& my_url, const GURL& document_url) { |
| 32 // The origin should match. |
| 33 if (!MatchesOrigin(my_url, document_url)) |
| 34 return false; |
| 35 |
| 36 // The url filename should be the new tab page ServiceWorker. |
| 37 std::string my_filename = my_url.ExtractFileName(); |
| 38 if (my_filename != kServiceWorkerFileName) |
| 39 return false; |
| 40 |
| 41 // The paths up to the filenames should be the same. |
| 42 std::string my_path_without_filename = my_url.path(); |
| 43 my_path_without_filename = my_path_without_filename.substr( |
| 44 0, my_path_without_filename.length() - my_filename.length()); |
| 45 std::string document_filename = document_url.ExtractFileName(); |
| 46 std::string document_path_without_filename = document_url.path(); |
| 47 document_path_without_filename = document_path_without_filename.substr( |
| 48 0, document_path_without_filename.length() - document_filename.length()); |
| 49 |
| 50 return my_path_without_filename == document_path_without_filename; |
| 51 } |
| 52 |
27 } // namespace search | 53 } // namespace search |
OLD | NEW |