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/search/instant_service.h" | 5 #include "chrome/browser/search/instant_service.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "chrome/browser/history/history_notifications.h" | 8 #include "chrome/browser/history/history_notifications.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 std::string path = url.path().substr(1); | 103 std::string path = url.path().substr(1); |
104 | 104 |
105 // Check that path is of Most Visited item ID form. | 105 // Check that path is of Most Visited item ID form. |
106 InstantRestrictedID dummy = 0; | 106 InstantRestrictedID dummy = 0; |
107 return base::StringToInt(path, &dummy); | 107 return base::StringToInt(path, &dummy); |
108 } | 108 } |
109 | 109 |
110 void InstantService::AddInstantProcess(int process_id) { | 110 void InstantService::AddInstantProcess(int process_id) { |
111 process_ids_.insert(process_id); | 111 process_ids_.insert(process_id); |
112 | 112 |
113 if (instant_io_context_) { | 113 if (instant_io_context_.get()) { |
114 BrowserThread::PostTask( | 114 BrowserThread::PostTask(BrowserThread::IO, |
115 BrowserThread::IO, FROM_HERE, | 115 FROM_HERE, |
116 base::Bind(&InstantIOContext::AddInstantProcessOnIO, | 116 base::Bind(&InstantIOContext::AddInstantProcessOnIO, |
117 instant_io_context_, process_id)); | 117 instant_io_context_, |
| 118 process_id)); |
118 } | 119 } |
119 } | 120 } |
120 | 121 |
121 bool InstantService::IsInstantProcess(int process_id) const { | 122 bool InstantService::IsInstantProcess(int process_id) const { |
122 return process_ids_.find(process_id) != process_ids_.end(); | 123 return process_ids_.find(process_id) != process_ids_.end(); |
123 } | 124 } |
124 | 125 |
125 void InstantService::AddMostVisitedItems( | 126 void InstantService::AddMostVisitedItems( |
126 const std::vector<InstantMostVisitedItem>& items) { | 127 const std::vector<InstantMostVisitedItem>& items) { |
127 most_visited_item_cache_.AddItems(items); | 128 most_visited_item_cache_.AddItems(items); |
128 | 129 |
129 // Post task to the IO thread to copy the data. | 130 // Post task to the IO thread to copy the data. |
130 if (instant_io_context_) { | 131 if (instant_io_context_.get()) { |
131 std::vector<InstantMostVisitedItemIDPair> items; | 132 std::vector<InstantMostVisitedItemIDPair> items; |
132 most_visited_item_cache_.GetCurrentItems(&items); | 133 most_visited_item_cache_.GetCurrentItems(&items); |
133 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
134 BrowserThread::IO, FROM_HERE, | 135 BrowserThread::IO, FROM_HERE, |
135 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, | 136 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
136 instant_io_context_, | 137 instant_io_context_, |
137 items)); | 138 items)); |
138 } | 139 } |
139 } | 140 } |
140 | 141 |
141 void InstantService::GetCurrentMostVisitedItems( | 142 void InstantService::GetCurrentMostVisitedItems( |
142 std::vector<InstantMostVisitedItemIDPair>* items) const { | 143 std::vector<InstantMostVisitedItemIDPair>* items) const { |
143 most_visited_item_cache_.GetCurrentItems(items); | 144 most_visited_item_cache_.GetCurrentItems(items); |
144 } | 145 } |
145 | 146 |
146 bool InstantService::GetMostVisitedItemForID( | 147 bool InstantService::GetMostVisitedItemForID( |
147 InstantRestrictedID most_visited_item_id, | 148 InstantRestrictedID most_visited_item_id, |
148 InstantMostVisitedItem* item) const { | 149 InstantMostVisitedItem* item) const { |
149 return most_visited_item_cache_.GetItemWithRestrictedID( | 150 return most_visited_item_cache_.GetItemWithRestrictedID( |
150 most_visited_item_id, item); | 151 most_visited_item_id, item); |
151 } | 152 } |
152 | 153 |
153 void InstantService::Shutdown() { | 154 void InstantService::Shutdown() { |
154 process_ids_.clear(); | 155 process_ids_.clear(); |
155 | 156 |
156 if (instant_io_context_) { | 157 if (instant_io_context_.get()) { |
157 BrowserThread::PostTask( | 158 BrowserThread::PostTask( |
158 BrowserThread::IO, FROM_HERE, | 159 BrowserThread::IO, |
| 160 FROM_HERE, |
159 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 161 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
160 instant_io_context_)); | 162 instant_io_context_)); |
161 } | 163 } |
162 instant_io_context_ = NULL; | 164 instant_io_context_ = NULL; |
163 } | 165 } |
164 | 166 |
165 void InstantService::Observe(int type, | 167 void InstantService::Observe(int type, |
166 const content::NotificationSource& source, | 168 const content::NotificationSource& source, |
167 const content::NotificationDetails& details) { | 169 const content::NotificationDetails& details) { |
168 switch (type) { | 170 switch (type) { |
169 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 171 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
170 int process_id = | 172 int process_id = |
171 content::Source<content::RenderProcessHost>(source)->GetID(); | 173 content::Source<content::RenderProcessHost>(source)->GetID(); |
172 process_ids_.erase(process_id); | 174 process_ids_.erase(process_id); |
173 | 175 |
174 if (instant_io_context_) { | 176 if (instant_io_context_.get()) { |
175 BrowserThread::PostTask( | 177 BrowserThread::PostTask( |
176 BrowserThread::IO, FROM_HERE, | 178 BrowserThread::IO, |
| 179 FROM_HERE, |
177 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 180 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
178 instant_io_context_, process_id)); | 181 instant_io_context_, |
| 182 process_id)); |
179 } | 183 } |
180 break; | 184 break; |
181 } | 185 } |
182 default: | 186 default: |
183 NOTREACHED() << "Unexpected notification type in InstantService."; | 187 NOTREACHED() << "Unexpected notification type in InstantService."; |
184 } | 188 } |
185 } | 189 } |
OLD | NEW |