Chromium Code Reviews| 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/browser/instant/instant_io_context.h" | 5 #include "chrome/browser/instant/instant_io_context.h" |
| 6 | 6 |
| 7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/browser/resource_context.h" | 8 #include "content/public/browser/resource_context.h" |
| 9 #include "content/public/browser/resource_request_info.h" | 9 #include "content/public/browser/resource_request_info.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Size of the most visited item cache. | |
| 18 const int kMostVisitedItemCacheSize = 1000; | |
|
palmer
2013/03/13 23:50:37
You define this in two .cc files; should be in a s
dhollowa
2013/03/14 00:02:43
How are you picking this number? It seems too hig
Shishir
2013/03/14 19:53:03
We need a larger number because of the race condit
Shishir
2013/03/14 19:53:03
Done.
dhollowa
2013/03/14 23:40:00
This should be removed in favor of the common defi
| |
| 19 | |
| 17 // Retrieves the Instant data from the |context|'s named user-data. | 20 // Retrieves the Instant data from the |context|'s named user-data. |
| 18 InstantIOContext* GetDataForResourceContext( | 21 InstantIOContext* GetDataForResourceContext( |
| 19 content::ResourceContext* context) { | 22 content::ResourceContext* context) { |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 21 return base::UserDataAdapter<InstantIOContext>::Get( | 24 return base::UserDataAdapter<InstantIOContext>::Get( |
| 22 context, InstantIOContext::kInstantIOContextKeyName); | 25 context, InstantIOContext::kInstantIOContextKeyName); |
| 23 } | 26 } |
| 24 | 27 |
| 25 InstantIOContext* GetDataForRequest(const net::URLRequest* request) { | 28 InstantIOContext* GetDataForRequest(const net::URLRequest* request) { |
| 26 const content::ResourceRequestInfo* info = | 29 const content::ResourceRequestInfo* info = |
| 27 content::ResourceRequestInfo::ForRequest(request); | 30 content::ResourceRequestInfo::ForRequest(request); |
| 28 if (!info) | 31 if (!info) |
| 29 return NULL; | 32 return NULL; |
| 30 | 33 |
| 31 return GetDataForResourceContext(info->GetContext()); | 34 return GetDataForResourceContext(info->GetContext()); |
| 32 } | 35 } |
| 33 | 36 |
| 34 } // namespace | 37 } // namespace |
| 35 | 38 |
| 36 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; | 39 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; |
| 37 | 40 |
| 38 InstantIOContext::InstantIOContext() { | 41 InstantIOContext::InstantIOContext() |
| 42 : most_visited_item_cache_(kMostVisitedItemCacheSize) { | |
| 39 // The InstantIOContext is created on the UI thread but is accessed | 43 // The InstantIOContext is created on the UI thread but is accessed |
| 40 // on the IO thread. | 44 // on the IO thread. |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 42 } | 46 } |
| 43 | 47 |
| 44 InstantIOContext::~InstantIOContext() { | 48 InstantIOContext::~InstantIOContext() { |
| 45 } | 49 } |
| 46 | 50 |
| 47 // static | 51 // static |
| 48 void InstantIOContext::SetUserDataOnIO( | 52 void InstantIOContext::SetUserDataOnIO( |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 68 } | 72 } |
| 69 | 73 |
| 70 // static | 74 // static |
| 71 void InstantIOContext::ClearInstantProcessesOnIO( | 75 void InstantIOContext::ClearInstantProcessesOnIO( |
| 72 scoped_refptr<InstantIOContext> instant_io_context) { | 76 scoped_refptr<InstantIOContext> instant_io_context) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 74 instant_io_context->process_ids_.clear(); | 78 instant_io_context->process_ids_.clear(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 // static | 81 // static |
| 78 void InstantIOContext::AddMostVisitedItemIDOnIO( | 82 void InstantIOContext::AddMostVisitedItemsOnIO( |
| 79 scoped_refptr<InstantIOContext> instant_io_context, | 83 scoped_refptr<InstantIOContext> instant_io_context, |
| 80 uint64 most_visited_item_id, const GURL& url) { | 84 const std::vector<InstantMostVisitedItemIDPair>& items) { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 82 instant_io_context->most_visited_item_id_to_url_map_[most_visited_item_id] = | 86 instant_io_context->most_visited_item_cache_.AddItemsWithRestrictedID(items); |
| 83 url; | |
| 84 } | 87 } |
| 85 | 88 |
| 86 // static | |
| 87 void InstantIOContext::DeleteMostVisitedURLsOnIO( | |
| 88 scoped_refptr<InstantIOContext> instant_io_context, | |
| 89 std::vector<uint64> deleted_ids, bool all_history) { | |
| 90 if (all_history) { | |
| 91 instant_io_context->most_visited_item_id_to_url_map_.clear(); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 for (size_t i = 0; i < deleted_ids.size(); ++i) { | |
| 96 instant_io_context->most_visited_item_id_to_url_map_.erase( | |
| 97 instant_io_context->most_visited_item_id_to_url_map_.find( | |
| 98 deleted_ids[i])); | |
| 99 } | |
| 100 } | |
| 101 | 89 |
| 102 // static | 90 // static |
| 103 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { | 91 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { |
| 104 const content::ResourceRequestInfo* info = | 92 const content::ResourceRequestInfo* info = |
| 105 content::ResourceRequestInfo::ForRequest(request); | 93 content::ResourceRequestInfo::ForRequest(request); |
| 106 if (!info) | 94 if (!info) |
| 107 return false; | 95 return false; |
| 108 | 96 |
| 109 InstantIOContext* instant_io_context = GetDataForRequest(request); | 97 InstantIOContext* instant_io_context = GetDataForRequest(request); |
| 110 if (!instant_io_context) | 98 if (!instant_io_context) |
| 111 return false; | 99 return false; |
| 112 | 100 |
| 113 int process_id = -1; | 101 int process_id = -1; |
| 114 int render_view_id = -1; | 102 int render_view_id = -1; |
| 115 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && | 103 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && |
| 116 instant_io_context->IsInstantProcess(process_id)) | 104 instant_io_context->IsInstantProcess(process_id)) |
| 117 return true; | 105 return true; |
| 118 return false; | 106 return false; |
| 119 } | 107 } |
| 120 | 108 |
| 121 // static | 109 // static |
| 122 bool InstantIOContext::GetURLForMostVisitedItemId( | 110 bool InstantIOContext::GetURLForMostVisitedItemID( |
| 123 const net::URLRequest* request, | 111 const net::URLRequest* request, |
| 124 uint64 most_visited_item_id, | 112 InstantRestrictedID restricted_id, |
| 125 GURL* url) { | 113 GURL* url) { |
| 126 InstantIOContext* instant_io_context = GetDataForRequest(request); | 114 InstantIOContext* instant_io_context = GetDataForRequest(request); |
| 127 if (!instant_io_context) { | 115 if (!instant_io_context) { |
| 128 *url = GURL(); | 116 *url = GURL(); |
| 129 return false; | 117 return false; |
| 130 } | 118 } |
| 131 | 119 |
| 132 return instant_io_context->GetURLForMostVisitedItemId(most_visited_item_id, | 120 return instant_io_context->GetURLForMostVisitedItemID(restricted_id, url); |
| 133 url); | |
| 134 } | 121 } |
| 135 | 122 |
| 136 bool InstantIOContext::IsInstantProcess(int process_id) const { | 123 bool InstantIOContext::IsInstantProcess(int process_id) const { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 138 return process_ids_.count(process_id) != 0; | 125 return process_ids_.count(process_id) != 0; |
| 139 } | 126 } |
| 140 | 127 |
| 141 bool InstantIOContext::GetURLForMostVisitedItemId(uint64 most_visited_item_id, | 128 bool InstantIOContext::GetURLForMostVisitedItemID( |
| 142 GURL* url) { | 129 InstantRestrictedID restricted_id, |
| 143 std::map<uint64, GURL>::iterator it = | 130 GURL* url) const { |
| 144 most_visited_item_id_to_url_map_.find(most_visited_item_id); | 131 InstantMostVisitedItem item; |
| 145 if (it != most_visited_item_id_to_url_map_.end()) { | 132 if (most_visited_item_cache_.GetItemWithRestrictedID(restricted_id, &item)) { |
| 146 *url = it->second; | 133 *url = item.url; |
| 147 return true; | 134 return true; |
| 148 } | 135 } |
| 136 | |
| 149 *url = GURL(); | 137 *url = GURL(); |
| 150 return false; | 138 return false; |
| 151 } | 139 } |
| OLD | NEW |