OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/history/history_api.h" | 5 #include "chrome/browser/extensions/api/history/history_api.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 12 #include "base/callback.h" |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
14 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
15 #include "base/location.h" | 16 #include "base/location.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/task/cancelable_task_tracker.h" | 20 #include "base/task/cancelable_task_tracker.h" |
21 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "base/values.h" | 23 #include "base/values.h" |
24 #include "chrome/browser/extensions/activity_log/activity_log.h" | 24 #include "chrome/browser/extensions/activity_log/activity_log.h" |
25 #include "chrome/browser/history/history_service_factory.h" | 25 #include "chrome/browser/history/history_service_factory.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 136 } |
137 | 137 |
138 HistoryEventRouter::~HistoryEventRouter() { | 138 HistoryEventRouter::~HistoryEventRouter() { |
139 } | 139 } |
140 | 140 |
141 void HistoryEventRouter::OnURLVisited(history::HistoryService* history_service, | 141 void HistoryEventRouter::OnURLVisited(history::HistoryService* history_service, |
142 ui::PageTransition transition, | 142 ui::PageTransition transition, |
143 const history::URLRow& row, | 143 const history::URLRow& row, |
144 const history::RedirectList& redirects, | 144 const history::RedirectList& redirects, |
145 base::Time visit_time) { | 145 base::Time visit_time) { |
146 scoped_ptr<base::ListValue> args = OnVisited::Create(GetHistoryItem(row)); | 146 std::unique_ptr<base::ListValue> args = |
| 147 OnVisited::Create(GetHistoryItem(row)); |
147 DispatchEvent(profile_, events::HISTORY_ON_VISITED, | 148 DispatchEvent(profile_, events::HISTORY_ON_VISITED, |
148 api::history::OnVisited::kEventName, std::move(args)); | 149 api::history::OnVisited::kEventName, std::move(args)); |
149 } | 150 } |
150 | 151 |
151 void HistoryEventRouter::OnURLsDeleted(history::HistoryService* history_service, | 152 void HistoryEventRouter::OnURLsDeleted(history::HistoryService* history_service, |
152 bool all_history, | 153 bool all_history, |
153 bool expired, | 154 bool expired, |
154 const history::URLRows& deleted_rows, | 155 const history::URLRows& deleted_rows, |
155 const std::set<GURL>& favicon_urls) { | 156 const std::set<GURL>& favicon_urls) { |
156 OnVisitRemoved::Removed removed; | 157 OnVisitRemoved::Removed removed; |
157 removed.all_history = all_history; | 158 removed.all_history = all_history; |
158 | 159 |
159 std::vector<std::string>* urls = new std::vector<std::string>(); | 160 std::vector<std::string>* urls = new std::vector<std::string>(); |
160 for (const auto& row : deleted_rows) | 161 for (const auto& row : deleted_rows) |
161 urls->push_back(row.url().spec()); | 162 urls->push_back(row.url().spec()); |
162 removed.urls.reset(urls); | 163 removed.urls.reset(urls); |
163 | 164 |
164 scoped_ptr<base::ListValue> args = OnVisitRemoved::Create(removed); | 165 std::unique_ptr<base::ListValue> args = OnVisitRemoved::Create(removed); |
165 DispatchEvent(profile_, events::HISTORY_ON_VISIT_REMOVED, | 166 DispatchEvent(profile_, events::HISTORY_ON_VISIT_REMOVED, |
166 api::history::OnVisitRemoved::kEventName, std::move(args)); | 167 api::history::OnVisitRemoved::kEventName, std::move(args)); |
167 } | 168 } |
168 | 169 |
169 void HistoryEventRouter::DispatchEvent(Profile* profile, | 170 void HistoryEventRouter::DispatchEvent( |
170 events::HistogramValue histogram_value, | 171 Profile* profile, |
171 const std::string& event_name, | 172 events::HistogramValue histogram_value, |
172 scoped_ptr<base::ListValue> event_args) { | 173 const std::string& event_name, |
| 174 std::unique_ptr<base::ListValue> event_args) { |
173 if (profile && EventRouter::Get(profile)) { | 175 if (profile && EventRouter::Get(profile)) { |
174 scoped_ptr<Event> event( | 176 std::unique_ptr<Event> event( |
175 new Event(histogram_value, event_name, std::move(event_args))); | 177 new Event(histogram_value, event_name, std::move(event_args))); |
176 event->restrict_to_browser_context = profile; | 178 event->restrict_to_browser_context = profile; |
177 EventRouter::Get(profile)->BroadcastEvent(std::move(event)); | 179 EventRouter::Get(profile)->BroadcastEvent(std::move(event)); |
178 } | 180 } |
179 } | 181 } |
180 | 182 |
181 HistoryAPI::HistoryAPI(content::BrowserContext* context) | 183 HistoryAPI::HistoryAPI(content::BrowserContext* context) |
182 : browser_context_(context) { | 184 : browser_context_(context) { |
183 EventRouter* event_router = EventRouter::Get(browser_context_); | 185 EventRouter* event_router = EventRouter::Get(browser_context_); |
184 event_router->RegisterObserver(this, api::history::OnVisited::kEventName); | 186 event_router->RegisterObserver(this, api::history::OnVisited::kEventName); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 FROM_HERE, | 267 FROM_HERE, |
266 base::Bind(&HistoryFunctionWithCallback::SendResponseToCallback, this)); | 268 base::Bind(&HistoryFunctionWithCallback::SendResponseToCallback, this)); |
267 } | 269 } |
268 | 270 |
269 void HistoryFunctionWithCallback::SendResponseToCallback() { | 271 void HistoryFunctionWithCallback::SendResponseToCallback() { |
270 SendResponse(true); | 272 SendResponse(true); |
271 Release(); // Balanced in RunAsync(). | 273 Release(); // Balanced in RunAsync(). |
272 } | 274 } |
273 | 275 |
274 bool HistoryGetVisitsFunction::RunAsyncImpl() { | 276 bool HistoryGetVisitsFunction::RunAsyncImpl() { |
275 scoped_ptr<GetVisits::Params> params(GetVisits::Params::Create(*args_)); | 277 std::unique_ptr<GetVisits::Params> params(GetVisits::Params::Create(*args_)); |
276 EXTENSION_FUNCTION_VALIDATE(params.get()); | 278 EXTENSION_FUNCTION_VALIDATE(params.get()); |
277 | 279 |
278 GURL url; | 280 GURL url; |
279 if (!ValidateUrl(params->details.url, &url)) | 281 if (!ValidateUrl(params->details.url, &url)) |
280 return false; | 282 return false; |
281 | 283 |
282 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 284 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
283 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); | 285 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); |
284 hs->QueryURL(url, | 286 hs->QueryURL(url, |
285 true, // Retrieve full history of a URL. | 287 true, // Retrieve full history of a URL. |
(...skipping 11 matching lines...) Expand all Loading... |
297 if (success && !visits.empty()) { | 299 if (success && !visits.empty()) { |
298 for (const history::VisitRow& visit : visits) | 300 for (const history::VisitRow& visit : visits) |
299 visit_item_vec.push_back(GetVisitItem(visit)); | 301 visit_item_vec.push_back(GetVisitItem(visit)); |
300 } | 302 } |
301 | 303 |
302 results_ = GetVisits::Results::Create(visit_item_vec); | 304 results_ = GetVisits::Results::Create(visit_item_vec); |
303 SendAsyncResponse(); | 305 SendAsyncResponse(); |
304 } | 306 } |
305 | 307 |
306 bool HistorySearchFunction::RunAsyncImpl() { | 308 bool HistorySearchFunction::RunAsyncImpl() { |
307 scoped_ptr<Search::Params> params(Search::Params::Create(*args_)); | 309 std::unique_ptr<Search::Params> params(Search::Params::Create(*args_)); |
308 EXTENSION_FUNCTION_VALIDATE(params.get()); | 310 EXTENSION_FUNCTION_VALIDATE(params.get()); |
309 | 311 |
310 base::string16 search_text = base::UTF8ToUTF16(params->query.text); | 312 base::string16 search_text = base::UTF8ToUTF16(params->query.text); |
311 | 313 |
312 history::QueryOptions options; | 314 history::QueryOptions options; |
313 options.SetRecentDayRange(1); | 315 options.SetRecentDayRange(1); |
314 options.max_count = 100; | 316 options.max_count = 100; |
315 | 317 |
316 if (params->query.start_time.get()) | 318 if (params->query.start_time.get()) |
317 options.begin_time = GetTime(*params->query.start_time); | 319 options.begin_time = GetTime(*params->query.start_time); |
(...skipping 17 matching lines...) Expand all Loading... |
335 HistoryItemList history_item_vec; | 337 HistoryItemList history_item_vec; |
336 if (results && !results->empty()) { | 338 if (results && !results->empty()) { |
337 for (const history::URLResult* item : *results) | 339 for (const history::URLResult* item : *results) |
338 history_item_vec.push_back(GetHistoryItem(*item)); | 340 history_item_vec.push_back(GetHistoryItem(*item)); |
339 } | 341 } |
340 results_ = Search::Results::Create(history_item_vec); | 342 results_ = Search::Results::Create(history_item_vec); |
341 SendAsyncResponse(); | 343 SendAsyncResponse(); |
342 } | 344 } |
343 | 345 |
344 bool HistoryAddUrlFunction::RunAsync() { | 346 bool HistoryAddUrlFunction::RunAsync() { |
345 scoped_ptr<AddUrl::Params> params(AddUrl::Params::Create(*args_)); | 347 std::unique_ptr<AddUrl::Params> params(AddUrl::Params::Create(*args_)); |
346 EXTENSION_FUNCTION_VALIDATE(params.get()); | 348 EXTENSION_FUNCTION_VALIDATE(params.get()); |
347 | 349 |
348 GURL url; | 350 GURL url; |
349 if (!ValidateUrl(params->details.url, &url)) | 351 if (!ValidateUrl(params->details.url, &url)) |
350 return false; | 352 return false; |
351 | 353 |
352 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 354 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
353 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); | 355 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); |
354 hs->AddPage(url, base::Time::Now(), history::SOURCE_EXTENSION); | 356 hs->AddPage(url, base::Time::Now(), history::SOURCE_EXTENSION); |
355 | 357 |
356 SendResponse(true); | 358 SendResponse(true); |
357 return true; | 359 return true; |
358 } | 360 } |
359 | 361 |
360 bool HistoryDeleteUrlFunction::RunAsync() { | 362 bool HistoryDeleteUrlFunction::RunAsync() { |
361 scoped_ptr<DeleteUrl::Params> params(DeleteUrl::Params::Create(*args_)); | 363 std::unique_ptr<DeleteUrl::Params> params(DeleteUrl::Params::Create(*args_)); |
362 EXTENSION_FUNCTION_VALIDATE(params.get()); | 364 EXTENSION_FUNCTION_VALIDATE(params.get()); |
363 | 365 |
364 if (!VerifyDeleteAllowed()) | 366 if (!VerifyDeleteAllowed()) |
365 return false; | 367 return false; |
366 | 368 |
367 GURL url; | 369 GURL url; |
368 if (!ValidateUrl(params->details.url, &url)) | 370 if (!ValidateUrl(params->details.url, &url)) |
369 return false; | 371 return false; |
370 | 372 |
371 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 373 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
372 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); | 374 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); |
373 hs->DeleteURL(url); | 375 hs->DeleteURL(url); |
374 | 376 |
375 // Also clean out from the activity log. If the activity log testing flag is | 377 // Also clean out from the activity log. If the activity log testing flag is |
376 // set then don't clean so testers can see what potentially malicious | 378 // set then don't clean so testers can see what potentially malicious |
377 // extensions have been trying to clean from their logs. | 379 // extensions have been trying to clean from their logs. |
378 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 380 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
379 switches::kEnableExtensionActivityLogTesting)) { | 381 switches::kEnableExtensionActivityLogTesting)) { |
380 ActivityLog* activity_log = ActivityLog::GetInstance(GetProfile()); | 382 ActivityLog* activity_log = ActivityLog::GetInstance(GetProfile()); |
381 DCHECK(activity_log); | 383 DCHECK(activity_log); |
382 activity_log->RemoveURL(url); | 384 activity_log->RemoveURL(url); |
383 } | 385 } |
384 | 386 |
385 SendResponse(true); | 387 SendResponse(true); |
386 return true; | 388 return true; |
387 } | 389 } |
388 | 390 |
389 bool HistoryDeleteRangeFunction::RunAsyncImpl() { | 391 bool HistoryDeleteRangeFunction::RunAsyncImpl() { |
390 scoped_ptr<DeleteRange::Params> params(DeleteRange::Params::Create(*args_)); | 392 std::unique_ptr<DeleteRange::Params> params( |
| 393 DeleteRange::Params::Create(*args_)); |
391 EXTENSION_FUNCTION_VALIDATE(params.get()); | 394 EXTENSION_FUNCTION_VALIDATE(params.get()); |
392 | 395 |
393 if (!VerifyDeleteAllowed()) | 396 if (!VerifyDeleteAllowed()) |
394 return false; | 397 return false; |
395 | 398 |
396 base::Time start_time = GetTime(params->range.start_time); | 399 base::Time start_time = GetTime(params->range.start_time); |
397 base::Time end_time = GetTime(params->range.end_time); | 400 base::Time end_time = GetTime(params->range.end_time); |
398 | 401 |
399 std::set<GURL> restrict_urls; | 402 std::set<GURL> restrict_urls; |
400 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 403 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 } | 449 } |
447 | 450 |
448 return true; | 451 return true; |
449 } | 452 } |
450 | 453 |
451 void HistoryDeleteAllFunction::DeleteComplete() { | 454 void HistoryDeleteAllFunction::DeleteComplete() { |
452 SendAsyncResponse(); | 455 SendAsyncResponse(); |
453 } | 456 } |
454 | 457 |
455 } // namespace extensions | 458 } // namespace extensions |
OLD | NEW |