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/devtools/devtools_file_system_indexer.h" | 5 #include "chrome/browser/devtools/devtools_file_system_indexer.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
13 #include "base/files/file_util_proxy.h" | 13 #include "base/files/file_util_proxy.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 | 19 |
20 using base::Bind; | 20 using base::Bind; |
21 using base::Callback; | 21 using base::Callback; |
22 using base::FileEnumerator; | 22 using base::FileEnumerator; |
23 using base::FilePath; | 23 using base::FilePath; |
24 using base::FileUtilProxy; | |
25 using base::Time; | 24 using base::Time; |
26 using base::TimeDelta; | 25 using base::TimeDelta; |
27 using base::TimeTicks; | 26 using base::TimeTicks; |
28 using base::PassPlatformFile; | 27 using base::PassPlatformFile; |
29 using base::PlatformFile; | 28 using base::PlatformFile; |
30 using content::BrowserThread; | 29 using content::BrowserThread; |
31 using std::map; | 30 using std::map; |
32 using std::set; | 31 using std::set; |
33 using std::string; | 32 using std::string; |
34 using std::vector; | 33 using std::vector; |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 261 |
263 DevToolsFileSystemIndexer::FileSystemIndexingJob::FileSystemIndexingJob( | 262 DevToolsFileSystemIndexer::FileSystemIndexingJob::FileSystemIndexingJob( |
264 const FilePath& file_system_path, | 263 const FilePath& file_system_path, |
265 const TotalWorkCallback& total_work_callback, | 264 const TotalWorkCallback& total_work_callback, |
266 const WorkedCallback& worked_callback, | 265 const WorkedCallback& worked_callback, |
267 const DoneCallback& done_callback) | 266 const DoneCallback& done_callback) |
268 : file_system_path_(file_system_path), | 267 : file_system_path_(file_system_path), |
269 total_work_callback_(total_work_callback), | 268 total_work_callback_(total_work_callback), |
270 worked_callback_(worked_callback), | 269 worked_callback_(worked_callback), |
271 done_callback_(done_callback), | 270 done_callback_(done_callback), |
| 271 current_file_(BrowserThread::GetMessageLoopProxyForThread( |
| 272 BrowserThread::FILE).get()), |
272 files_indexed_(0), | 273 files_indexed_(0), |
273 stopped_(false) { | 274 stopped_(false) { |
274 current_trigrams_set_.resize(kTrigramCount); | 275 current_trigrams_set_.resize(kTrigramCount); |
275 current_trigrams_.reserve(kTrigramCount); | 276 current_trigrams_.reserve(kTrigramCount); |
276 } | 277 } |
277 | 278 |
278 DevToolsFileSystemIndexer::FileSystemIndexingJob::~FileSystemIndexingJob() {} | 279 DevToolsFileSystemIndexer::FileSystemIndexingJob::~FileSystemIndexingJob() {} |
279 | 280 |
280 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Start() { | 281 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Start() { |
281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 void DevToolsFileSystemIndexer::FileSystemIndexingJob::IndexFiles() { | 331 void DevToolsFileSystemIndexer::FileSystemIndexingJob::IndexFiles() { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
332 if (stopped_) | 333 if (stopped_) |
333 return; | 334 return; |
334 if (indexing_it_ == file_path_times_.end()) { | 335 if (indexing_it_ == file_path_times_.end()) { |
335 g_trigram_index.Get().NormalizeVectors(); | 336 g_trigram_index.Get().NormalizeVectors(); |
336 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_callback_); | 337 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_callback_); |
337 return; | 338 return; |
338 } | 339 } |
339 FilePath file_path = indexing_it_->first; | 340 FilePath file_path = indexing_it_->first; |
340 FileUtilProxy::CreateOrOpen( | 341 current_file_.CreateOrOpen( |
341 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(), | 342 file_path, |
342 file_path, | 343 base::File::FLAG_OPEN | base::File::FLAG_READ, |
343 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 344 Bind(&FileSystemIndexingJob::StartFileIndexing, this)); |
344 Bind(&FileSystemIndexingJob::StartFileIndexing, this)); | |
345 } | 345 } |
346 | 346 |
347 void DevToolsFileSystemIndexer::FileSystemIndexingJob::StartFileIndexing( | 347 void DevToolsFileSystemIndexer::FileSystemIndexingJob::StartFileIndexing( |
348 base::File::Error error, | 348 base::File::Error error) { |
349 PassPlatformFile pass_file, | 349 if (!current_file_.IsValid()) { |
350 bool) { | |
351 if (error != base::File::FILE_OK) { | |
352 current_file_ = base::kInvalidPlatformFileValue; | |
353 FinishFileIndexing(false); | 350 FinishFileIndexing(false); |
354 return; | 351 return; |
355 } | 352 } |
356 current_file_ = pass_file.ReleaseValue(); | |
357 current_file_offset_ = 0; | 353 current_file_offset_ = 0; |
358 current_trigrams_.clear(); | 354 current_trigrams_.clear(); |
359 std::fill(current_trigrams_set_.begin(), current_trigrams_set_.end(), false); | 355 std::fill(current_trigrams_set_.begin(), current_trigrams_set_.end(), false); |
360 ReadFromFile(); | 356 ReadFromFile(); |
361 } | 357 } |
362 | 358 |
363 void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReadFromFile() { | 359 void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReadFromFile() { |
364 if (stopped_) { | 360 if (stopped_) { |
365 CloseFile(); | 361 CloseFile(); |
366 return; | 362 return; |
367 } | 363 } |
368 FileUtilProxy::Read( | 364 current_file_.Read(current_file_offset_, kMaxReadLength, |
369 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(), | 365 Bind(&FileSystemIndexingJob::OnRead, this)); |
370 current_file_, | |
371 current_file_offset_, | |
372 kMaxReadLength, | |
373 Bind(&FileSystemIndexingJob::OnRead, this)); | |
374 } | 366 } |
375 | 367 |
376 void DevToolsFileSystemIndexer::FileSystemIndexingJob::OnRead( | 368 void DevToolsFileSystemIndexer::FileSystemIndexingJob::OnRead( |
377 base::File::Error error, | 369 base::File::Error error, |
378 const char* data, | 370 const char* data, |
379 int bytes_read) { | 371 int bytes_read) { |
380 if (error != base::File::FILE_OK) { | 372 if (error != base::File::FILE_OK) { |
381 FinishFileIndexing(false); | 373 FinishFileIndexing(false); |
382 return; | 374 return; |
383 } | 375 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 FilePath file_path = indexing_it_->first; | 410 FilePath file_path = indexing_it_->first; |
419 g_trigram_index.Get().SetTrigramsForFile( | 411 g_trigram_index.Get().SetTrigramsForFile( |
420 file_path, current_trigrams_, file_path_times_[file_path]); | 412 file_path, current_trigrams_, file_path_times_[file_path]); |
421 } | 413 } |
422 ReportWorked(); | 414 ReportWorked(); |
423 ++indexing_it_; | 415 ++indexing_it_; |
424 IndexFiles(); | 416 IndexFiles(); |
425 } | 417 } |
426 | 418 |
427 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseFile() { | 419 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseFile() { |
428 if (current_file_ != base::kInvalidPlatformFileValue) { | 420 if (current_file_.IsValid()) |
429 FileUtilProxy::Close( | 421 current_file_.Close(Bind(&FileSystemIndexingJob::CloseCallback, this)); |
430 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(), | |
431 current_file_, | |
432 Bind(&FileSystemIndexingJob::CloseCallback, this)); | |
433 } | |
434 } | 422 } |
435 | 423 |
436 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseCallback( | 424 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CloseCallback( |
437 base::File::Error error) {} | 425 base::File::Error error) {} |
438 | 426 |
439 void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReportWorked() { | 427 void DevToolsFileSystemIndexer::FileSystemIndexingJob::ReportWorked() { |
440 TimeTicks current_time = TimeTicks::Now(); | 428 TimeTicks current_time = TimeTicks::Now(); |
441 bool should_send_worked_nitification = true; | 429 bool should_send_worked_nitification = true; |
442 if (!last_worked_notification_time_.is_null()) { | 430 if (!last_worked_notification_time_.is_null()) { |
443 TimeDelta delta = current_time - last_worked_notification_time_; | 431 TimeDelta delta = current_time - last_worked_notification_time_; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); | 490 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); |
503 vector<string> result; | 491 vector<string> result; |
504 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); | 492 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); |
505 vector<FilePath>::const_iterator it = file_paths.begin(); | 493 vector<FilePath>::const_iterator it = file_paths.begin(); |
506 for (; it != file_paths.end(); ++it) { | 494 for (; it != file_paths.end(); ++it) { |
507 if (path.IsParent(*it)) | 495 if (path.IsParent(*it)) |
508 result.push_back(it->AsUTF8Unsafe()); | 496 result.push_back(it->AsUTF8Unsafe()); |
509 } | 497 } |
510 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); | 498 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); |
511 } | 499 } |
OLD | NEW |