Chromium Code Reviews| Index: chrome/browser/devtools/devtools_file_watcher.cc |
| diff --git a/chrome/browser/devtools/devtools_file_watcher.cc b/chrome/browser/devtools/devtools_file_watcher.cc |
| index e5af5a5f198df8f249f0c90fead55bb3a152d088..ee302aaa2399e4e77104b74d1168c1015910c009 100644 |
| --- a/chrome/browser/devtools/devtools_file_watcher.cc |
| +++ b/chrome/browser/devtools/devtools_file_watcher.cc |
| @@ -13,6 +13,7 @@ |
| #include "base/files/file_enumerator.h" |
| #include "base/files/file_path.h" |
| #include "base/files/file_path_watcher.h" |
| +#include "base/files/file_util.h" |
| #include "base/memory/ref_counted.h" |
| #include "content/public/browser/browser_thread.h" |
| @@ -38,13 +39,15 @@ class DevToolsFileWatcher::SharedFileWatcher : |
| DevToolsFileWatcher::SharedFileWatcher>; |
| ~SharedFileWatcher(); |
| + using FilePathTimesMap = std::map<base::FilePath, base::Time>; |
| + void GetModificationTimes(const base::FilePath& path, |
| + FilePathTimesMap* file_path_times); |
| void DirectoryChanged(const base::FilePath& path, bool error); |
| void DispatchNotifications(); |
| std::vector<DevToolsFileWatcher*> listeners_; |
| std::map<base::FilePath, std::unique_ptr<base::FilePathWatcher>> watchers_; |
| - using FilePathTimesMap = std::map<base::FilePath, base::Time>; |
| - FilePathTimesMap file_path_times_; |
| + std::map<base::FilePath, FilePathTimesMap> file_path_times_; |
| std::set<base::FilePath> pending_paths_; |
| base::Time last_event_time_; |
| base::TimeDelta last_dispatch_cost_; |
| @@ -84,11 +87,19 @@ void DevToolsFileWatcher::SharedFileWatcher::AddWatch( |
| if (!success) |
| return; |
| + FilePathTimesMap times_map; |
| + GetModificationTimes(path, ×_map); |
|
dgozman
2016/10/04 02:26:41
One line, avoids copying:
GetModificationTimes(pa
lushnikov
2016/10/04 02:51:59
Done.
|
| + file_path_times_[path] = times_map; |
| +} |
| + |
| +void DevToolsFileWatcher::SharedFileWatcher::GetModificationTimes( |
| + const base::FilePath& path, |
| + FilePathTimesMap* times_map) { |
| base::FileEnumerator enumerator(path, true, base::FileEnumerator::FILES); |
| base::FilePath file_path = enumerator.Next(); |
| while (!file_path.empty()) { |
| base::FileEnumerator::FileInfo file_info = enumerator.GetInfo(); |
| - file_path_times_[file_path] = file_info.GetLastModifiedTime(); |
| + (*times_map)[file_path] = file_info.GetLastModifiedTime(); |
| file_path = enumerator.Next(); |
| } |
| } |
| @@ -121,26 +132,43 @@ void DevToolsFileWatcher::SharedFileWatcher::DirectoryChanged( |
| } |
| void DevToolsFileWatcher::SharedFileWatcher::DispatchNotifications() { |
| + if (!pending_paths_.size()) |
| + return; |
| base::Time start = base::Time::Now(); |
| + std::vector<std::string> added_paths; |
| + std::vector<std::string> removed_paths; |
| std::vector<std::string> changed_paths; |
| - for (auto path : pending_paths_) { |
| - base::FileEnumerator enumerator(path, true, base::FileEnumerator::FILES); |
| - base::FilePath file_path = enumerator.Next(); |
| - while (!file_path.empty()) { |
| - base::FileEnumerator::FileInfo file_info = enumerator.GetInfo(); |
| - base::Time new_time = file_info.GetLastModifiedTime(); |
| - if (file_path_times_[file_path] != new_time) { |
| - file_path_times_[file_path] = new_time; |
| - changed_paths.push_back(file_path.AsUTF8Unsafe()); |
| + |
| + for (const auto& path : pending_paths_) { |
| + FilePathTimesMap& old_times = file_path_times_[path]; |
| + FilePathTimesMap current_times; |
| + GetModificationTimes(path, ¤t_times); |
| + for (auto it = current_times.begin(); it != current_times.end(); ++it) { |
|
dgozman
2016/10/04 02:26:41
for (const auto& pair : current_times)
lushnikov
2016/10/04 02:51:59
Done.
|
| + auto old_timestamp = old_times.find(it->first); |
| + if (old_timestamp == old_times.end()) { |
| + old_times[it->first] = it->second; |
| + added_paths.push_back(it->first.AsUTF8Unsafe()); |
| + } else if (old_timestamp->second != it->second) { |
| + old_timestamp->second = it->second; |
| + changed_paths.push_back(it->first.AsUTF8Unsafe()); |
| + } |
| + } |
| + for (auto it = old_times.begin(); it != old_times.end();) { |
|
dgozman
2016/10/04 02:26:41
ditto
lushnikov
2016/10/04 02:51:59
Done.
|
| + auto new_timestamp = current_times.find(it->first); |
| + if (new_timestamp == current_times.end()) { |
| + removed_paths.push_back(it->first.AsUTF8Unsafe()); |
| + old_times.erase(it++); |
|
dgozman
2016/10/04 02:26:41
Don't do this. Just |old_times.swap(current_times)
lushnikov
2016/10/04 02:51:59
Done.
|
| + } else { |
| + ++it; |
| } |
| - file_path = enumerator.Next(); |
| } |
| } |
| pending_paths_.clear(); |
| for (auto* watcher : listeners_) { |
| BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| - base::Bind(watcher->callback_, changed_paths)); |
| + base::Bind(watcher->callback_, changed_paths, |
| + added_paths, removed_paths)); |
| } |
| last_dispatch_cost_ = base::Time::Now() - start; |
| } |