• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_SYSTEM_INDEXER_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_SYSTEM_INDEXER_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/files/file_proxy.h"
14 #include "base/memory/ref_counted.h"
15 
16 class Profile;
17 
18 namespace base {
19 class FilePath;
20 class FileEnumerator;
21 class Time;
22 }
23 
24 namespace content {
25 class WebContents;
26 }
27 
28 class DevToolsFileSystemIndexer
29     : public base::RefCountedThreadSafe<DevToolsFileSystemIndexer> {
30  public:
31 
32   typedef base::Callback<void(int)> TotalWorkCallback;
33   typedef base::Callback<void(int)> WorkedCallback;
34   typedef base::Callback<void()> DoneCallback;
35   typedef base::Callback<void(const std::vector<std::string>&)> SearchCallback;
36 
37   class FileSystemIndexingJob : public base::RefCounted<FileSystemIndexingJob> {
38    public:
39     void Stop();
40 
41    private:
42     friend class base::RefCounted<FileSystemIndexingJob>;
43     friend class DevToolsFileSystemIndexer;
44     FileSystemIndexingJob(const base::FilePath& file_system_path,
45                           const TotalWorkCallback& total_work_callback,
46                           const WorkedCallback& worked_callback,
47                           const DoneCallback& done_callback);
48     virtual ~FileSystemIndexingJob();
49 
50     void Start();
51     void StopOnFileThread();
52     void CollectFilesToIndex();
53     void IndexFiles();
54     void StartFileIndexing(base::File::Error error);
55     void ReadFromFile();
56     void OnRead(base::File::Error error,
57                 const char* data,
58                 int bytes_read);
59     void FinishFileIndexing(bool success);
60     void CloseFile();
61     void CloseCallback(base::File::Error error);
62     void ReportWorked();
63 
64     base::FilePath file_system_path_;
65     TotalWorkCallback total_work_callback_;
66     WorkedCallback worked_callback_;
67     DoneCallback done_callback_;
68     scoped_ptr<base::FileEnumerator> file_enumerator_;
69     typedef std::map<base::FilePath, base::Time> FilePathTimesMap;
70     FilePathTimesMap file_path_times_;
71     FilePathTimesMap::const_iterator indexing_it_;
72     base::FileProxy current_file_;
73     int64 current_file_offset_;
74     typedef int32 Trigram;
75     std::vector<Trigram> current_trigrams_;
76     // The index in this vector is the trigram id.
77     std::vector<bool> current_trigrams_set_;
78     base::TimeTicks last_worked_notification_time_;
79     int files_indexed_;
80     bool stopped_;
81   };
82 
83   DevToolsFileSystemIndexer();
84 
85   // Performs file system indexing for given |file_system_path| and sends
86   // progress callbacks.
87   scoped_refptr<FileSystemIndexingJob> IndexPath(
88       const std::string& file_system_path,
89       const TotalWorkCallback& total_work_callback,
90       const WorkedCallback& worked_callback,
91       const DoneCallback& done_callback);
92 
93   // Performs trigram search for given |query| in |file_system_path|.
94   void SearchInPath(const std::string& file_system_path,
95                     const std::string& query,
96                     const SearchCallback& callback);
97 
98  private:
99   friend class base::RefCountedThreadSafe<DevToolsFileSystemIndexer>;
100 
101   virtual ~DevToolsFileSystemIndexer();
102 
103   void SearchInPathOnFileThread(const std::string& file_system_path,
104                                 const std::string& query,
105                                 const SearchCallback& callback);
106 
107   DISALLOW_COPY_AND_ASSIGN(DevToolsFileSystemIndexer);
108 };
109 
110 #endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_SYSTEM_INDEXER_H_
111