Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chromeos/arc/arc_downloads_watcher_service.h" | 5 #include "chrome/browser/chromeos/arc/arc_downloads_watcher_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <iterator> | |
| 7 #include <map> | 9 #include <map> |
| 8 #include <memory> | 10 #include <memory> |
| 9 #include <utility> | 11 #include <utility> |
| 10 | 12 |
| 11 #include "base/callback.h" | 13 #include "base/callback.h" |
| 12 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/files/file_path_watcher.h" | 16 #include "base/files/file_path_watcher.h" |
| 15 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/string_util.h" | |
| 16 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 17 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 20 #include "components/arc/arc_bridge_service.h" | 23 #include "components/arc/arc_bridge_service.h" |
| 21 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 22 #include "mojo/public/cpp/bindings/array.h" | 25 #include "mojo/public/cpp/bindings/array.h" |
| 23 | 26 |
| 24 using content::BrowserThread; | 27 using content::BrowserThread; |
| 25 | 28 |
| 26 // Mapping from Android file paths to last modified timestamps. | 29 // Mapping from Android file paths to last modified timestamps. |
| 27 using TimestampMap = std::map<base::FilePath, base::Time>; | 30 using TimestampMap = std::map<base::FilePath, base::Time>; |
| 28 | 31 |
| 29 namespace arc { | 32 namespace arc { |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 | 35 |
| 33 const base::FilePath::CharType kAndroidDownloadDir[] = | 36 const base::FilePath::CharType kAndroidDownloadDir[] = |
| 34 FILE_PATH_LITERAL("/storage/emulated/0/Download"); | 37 FILE_PATH_LITERAL("/storage/emulated/0/Download"); |
| 35 | 38 |
| 36 // How long to wait for new inotify events before building the updated timestamp | 39 // How long to wait for new inotify events before building the updated timestamp |
| 37 // map. | 40 // map. |
| 38 const base::TimeDelta kBuildTimestampMapDelay = | 41 const base::TimeDelta kBuildTimestampMapDelay = |
| 39 base::TimeDelta::FromMilliseconds(1000); | 42 base::TimeDelta::FromMilliseconds(1000); |
| 40 | 43 |
| 44 // The set of media file extensions supported by Android MediaScanner. | |
| 45 // Entries must be sorted by lexicographical order for binary search. | |
|
Yusuke Sato
2016/09/30 22:51:23
and must be lower-case, I think.
Shuhei Takahashi
2016/10/03 08:48:52
Done.
| |
| 46 // This list should be in sync with | |
| 47 // frameworks/base/media/java/android/media/MediaScanner.java. | |
|
Yusuke Sato
2016/09/30 22:51:23
What about adding a git hash to document the revis
Shuhei Takahashi
2016/10/03 08:48:52
Mentioned that this list from aosp-marshmallow. No
| |
| 48 const char* kAndroidSupportedMediaExtensions[] = { | |
| 49 ".3g2", // FILE_TYPE_3GPP2, video/3gpp2 | |
| 50 ".3gp", // FILE_TYPE_3GPP, video/3gpp | |
| 51 ".3gpp", // FILE_TYPE_3GPP, video/3gpp | |
| 52 ".3gpp2", // FILE_TYPE_3GPP2, video/3gpp2 | |
| 53 ".aac", // FILE_TYPE_AAC, audio/aac, audio/aac-adts | |
| 54 ".amr", // FILE_TYPE_AMR, audio/amr | |
| 55 ".asf", // FILE_TYPE_ASF, video/x-ms-asf | |
| 56 ".avi", // FILE_TYPE_AVI, video/avi | |
| 57 ".awb", // FILE_TYPE_AWB, audio/amr-wb | |
| 58 ".bmp", // FILE_TYPE_BMP, image/x-ms-bmp | |
| 59 ".fl", // FILE_TYPE_FL, application/x-android-drm-fl | |
| 60 ".gif", // FILE_TYPE_GIF, image/gif | |
| 61 ".imy", // FILE_TYPE_IMY, audio/imelody | |
| 62 ".jpeg", // FILE_TYPE_JPEG, image/jpeg | |
| 63 ".jpg", // FILE_TYPE_JPEG, image/jpeg | |
| 64 ".m4a", // FILE_TYPE_M4A, audio/mp4 | |
| 65 ".m4v", // FILE_TYPE_M4V, video/mp4 | |
| 66 ".mid", // FILE_TYPE_MID, audio/midi | |
| 67 ".midi", // FILE_TYPE_MID, audio/midi | |
| 68 ".mka", // FILE_TYPE_MKA, audio/x-matroska | |
| 69 ".mkv", // FILE_TYPE_MKV, video/x-matroska | |
| 70 ".mp3", // FILE_TYPE_MP3, audio/mpeg | |
| 71 ".mp4", // FILE_TYPE_MP4, video/mp4 | |
| 72 ".mpeg", // FILE_TYPE_MP4, video/mpeg, video/mp2p | |
| 73 ".mpg", // FILE_TYPE_MP4, video/mpeg, video/mp2p | |
| 74 ".mpga", // FILE_TYPE_MP3, audio/mpeg | |
| 75 ".mxmf", // FILE_TYPE_MID, audio/midi | |
| 76 ".oga", // FILE_TYPE_OGG, application/ogg | |
| 77 ".ogg", // FILE_TYPE_OGG, audio/ogg, application/ogg | |
| 78 ".ota", // FILE_TYPE_MID, audio/midi | |
| 79 ".png", // FILE_TYPE_PNG, image/png | |
| 80 ".rtttl", // FILE_TYPE_MID, audio/midi | |
| 81 ".rtx", // FILE_TYPE_MID, audio/midi | |
| 82 ".smf", // FILE_TYPE_SMF, audio/sp-midi | |
| 83 ".ts", // FILE_TYPE_MP2TS, video/mp2ts | |
| 84 ".wav", // FILE_TYPE_WAV, audio/x-wav | |
| 85 ".wbmp", // FILE_TYPE_WBMP, image/vnd.wap.wbmp | |
| 86 ".webm", // FILE_TYPE_WEBM, video/webm | |
| 87 ".webp", // FILE_TYPE_WEBP, image/webp | |
| 88 ".wma", // FILE_TYPE_WMA, audio/x-ms-wma | |
| 89 ".wmv", // FILE_TYPE_WMV, video/x-ms-wmv | |
| 90 ".xmf", // FILE_TYPE_MID, audio/midi | |
| 91 }; | |
| 92 | |
| 41 // Compares two TimestampMaps and returns the list of file paths added/removed | 93 // Compares two TimestampMaps and returns the list of file paths added/removed |
| 42 // or whose timestamp have changed. | 94 // or whose timestamp have changed. |
| 43 std::vector<base::FilePath> CollectChangedPaths( | 95 std::vector<base::FilePath> CollectChangedPaths( |
| 44 const TimestampMap& timestamp_map_a, | 96 const TimestampMap& timestamp_map_a, |
| 45 const TimestampMap& timestamp_map_b) { | 97 const TimestampMap& timestamp_map_b) { |
| 46 std::vector<base::FilePath> changed_paths; | 98 std::vector<base::FilePath> changed_paths; |
| 47 | 99 |
| 48 TimestampMap::const_iterator iter_a = timestamp_map_a.begin(); | 100 TimestampMap::const_iterator iter_a = timestamp_map_a.begin(); |
| 49 TimestampMap::const_iterator iter_b = timestamp_map_b.begin(); | 101 TimestampMap::const_iterator iter_b = timestamp_map_b.begin(); |
| 50 while (iter_a != timestamp_map_a.end() && iter_b != timestamp_map_b.end()) { | 102 while (iter_a != timestamp_map_a.end() && iter_b != timestamp_map_b.end()) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 68 ++iter_a; | 120 ++iter_a; |
| 69 } | 121 } |
| 70 while (iter_b != timestamp_map_b.end()) { | 122 while (iter_b != timestamp_map_b.end()) { |
| 71 changed_paths.emplace_back(iter_b->first); | 123 changed_paths.emplace_back(iter_b->first); |
| 72 ++iter_b; | 124 ++iter_b; |
| 73 } | 125 } |
| 74 | 126 |
| 75 return changed_paths; | 127 return changed_paths; |
| 76 } | 128 } |
| 77 | 129 |
| 130 // Returns true if the extension of the file path looks like a media file. | |
| 131 bool HasMediaExtension(const base::FilePath& path) { | |
|
Yusuke Sato
2016/09/30 22:51:23
Would you mind adding chrome/browser/chromeos/arc/
Shuhei Takahashi
2016/10/03 08:48:52
Yes that's good, and actually this function had a
| |
| 132 const std::string extension = base::ToLowerASCII(path.Extension()); | |
|
Yusuke Sato
2016/09/30 22:51:23
..and add DCHECK(std::is_sorted(...)); check here?
Shuhei Takahashi
2016/10/03 08:48:52
Done.
| |
| 133 auto iter = std::lower_bound( | |
| 134 std::begin(kAndroidSupportedMediaExtensions), | |
| 135 std::end(kAndroidSupportedMediaExtensions), extension, | |
| 136 [](const char* a, const std::string& b) { return a == b; }); | |
|
Yusuke Sato
2016/09/30 22:51:23
nit: any reason to create a string every time?
.
Shuhei Takahashi
2016/10/03 08:48:52
Sounds good, done.
| |
| 137 return iter != std::end(kAndroidSupportedMediaExtensions); | |
| 138 } | |
| 139 | |
| 78 // Scans files under |downloads_dir| recursively and builds a map from file | 140 // Scans files under |downloads_dir| recursively and builds a map from file |
| 79 // paths (in Android filesystem) to last modified timestamps. | 141 // paths (in Android filesystem) to last modified timestamps. |
| 80 TimestampMap BuildTimestampMap(base::FilePath downloads_dir) { | 142 TimestampMap BuildTimestampMap(base::FilePath downloads_dir) { |
| 81 DCHECK(!downloads_dir.EndsWithSeparator()); | 143 DCHECK(!downloads_dir.EndsWithSeparator()); |
| 82 TimestampMap timestamp_map; | 144 TimestampMap timestamp_map; |
| 83 | 145 |
| 84 // Enumerate normal files only; directories and symlinks are skipped. | 146 // Enumerate normal files only; directories and symlinks are skipped. |
| 85 base::FileEnumerator enumerator(downloads_dir, true, | 147 base::FileEnumerator enumerator(downloads_dir, true, |
| 86 base::FileEnumerator::FILES); | 148 base::FileEnumerator::FILES); |
| 87 for (base::FilePath cros_path = enumerator.Next(); !cros_path.empty(); | 149 for (base::FilePath cros_path = enumerator.Next(); !cros_path.empty(); |
| 88 cros_path = enumerator.Next()) { | 150 cros_path = enumerator.Next()) { |
| 151 // Skip non-media files for efficiency. | |
| 152 if (!HasMediaExtension(cros_path)) | |
| 153 continue; | |
| 89 // Android file path can be obtained by replacing |downloads_dir| prefix | 154 // Android file path can be obtained by replacing |downloads_dir| prefix |
| 90 // with |kAndroidDownloadDir|. | 155 // with |kAndroidDownloadDir|. |
| 91 base::FilePath android_path(kAndroidDownloadDir); | 156 base::FilePath android_path(kAndroidDownloadDir); |
| 92 downloads_dir.AppendRelativePath(cros_path, &android_path); | 157 downloads_dir.AppendRelativePath(cros_path, &android_path); |
| 93 const base::FileEnumerator::FileInfo& info = enumerator.GetInfo(); | 158 const base::FileEnumerator::FileInfo& info = enumerator.GetInfo(); |
| 94 timestamp_map[android_path] = info.GetLastModifiedTime(); | 159 timestamp_map[android_path] = info.GetLastModifiedTime(); |
| 95 } | 160 } |
| 96 return timestamp_map; | 161 return timestamp_map; |
| 97 } | 162 } |
| 98 | 163 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 354 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 290 | 355 |
| 291 auto* instance = arc_bridge_service()->file_system()->GetInstanceForMethod( | 356 auto* instance = arc_bridge_service()->file_system()->GetInstanceForMethod( |
| 292 "RequestMediaScan"); | 357 "RequestMediaScan"); |
| 293 if (!instance) | 358 if (!instance) |
| 294 return; | 359 return; |
| 295 instance->RequestMediaScan(std::move(paths)); | 360 instance->RequestMediaScan(std::move(paths)); |
| 296 } | 361 } |
| 297 | 362 |
| 298 } // namespace arc | 363 } // namespace arc |
| OLD | NEW |