OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/file_handlers/directory_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/file_util.h" | |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "net/base/filename_util.h" | |
13 #include "storage/browser/fileapi/file_system_url.h" | |
14 | |
15 #if defined(OS_CHROMEOS) | |
16 #include "extensions/browser/api/extensions_api_client.h" | |
17 #include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h
" | |
18 #endif | |
19 | |
20 namespace extensions { | |
21 namespace app_file_handler_util { | |
22 | |
23 namespace { | |
24 | |
25 void GetIsDirectoryFromFileInfo(const base::FilePath& path, | |
26 bool* is_directory) { | |
27 base::File::Info file_info; | |
28 *is_directory = GetFileInfo(path, &file_info) && file_info.is_directory; | |
29 } | |
30 | |
31 void OnGetIsDirectoryFromFileInfoCompleted( | |
32 std::unique_ptr<bool> is_directory, | |
33 const base::Callback<void(bool)>& callback) { | |
34 callback.Run(*is_directory); | |
35 } | |
36 | |
37 // The callback parameter contains the result and is required to support | |
38 // both native local directories to avoid UI thread and non native local | |
39 // path directories for the IsNonNativeLocalPathDirectory API. | |
40 void EntryIsDirectory(Profile* profile, | |
41 const base::FilePath& path, | |
42 const base::Callback<void(bool)>& callback) { | |
43 #if defined(OS_CHROMEOS) | |
44 NonNativeFileSystemDelegate* delegate = | |
45 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); | |
46 if (delegate && delegate->IsUnderNonNativeLocalPath(profile, path)) { | |
47 delegate->IsNonNativeLocalPathDirectory(profile, path, callback); | |
48 return; | |
49 } | |
50 #endif | |
51 | |
52 std::unique_ptr<bool> is_directory(new bool); | |
53 bool* const is_directory_ptr = is_directory.get(); | |
54 | |
55 content::BrowserThread::PostBlockingPoolTaskAndReply( | |
56 FROM_HERE, | |
57 base::Bind(&GetIsDirectoryFromFileInfo, path, is_directory_ptr), | |
58 base::Bind(&OnGetIsDirectoryFromFileInfoCompleted, | |
59 base::Passed(&is_directory), callback)); | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 IsDirectoryCollector::IsDirectoryCollector(Profile* profile) | |
65 : profile_(profile), left_(0), weak_ptr_factory_(this) {} | |
66 | |
67 IsDirectoryCollector::~IsDirectoryCollector() {} | |
68 | |
69 void IsDirectoryCollector::CollectForEntriesPaths( | |
70 const std::vector<base::FilePath>& paths, | |
71 const CompletionCallback& callback) { | |
72 DCHECK(!callback.is_null()); | |
73 paths_ = paths; | |
74 callback_ = callback; | |
75 | |
76 DCHECK(!result_.get()); | |
77 result_.reset(new std::set<base::FilePath>()); | |
78 left_ = paths.size(); | |
79 | |
80 if (!left_) { | |
81 // Nothing to process. | |
82 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
83 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | |
84 callback_ = CompletionCallback(); | |
85 return; | |
86 } | |
87 | |
88 for (size_t i = 0; i < paths.size(); ++i) { | |
89 EntryIsDirectory(profile_, paths[i], | |
90 base::Bind(&IsDirectoryCollector::OnIsDirectoryCollected, | |
91 weak_ptr_factory_.GetWeakPtr(), i)); | |
92 } | |
93 } | |
94 | |
95 void IsDirectoryCollector::OnIsDirectoryCollected(size_t index, | |
96 bool is_directory) { | |
97 if (is_directory) | |
98 result_->insert(paths_[index]); | |
99 if (!--left_) { | |
100 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
101 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | |
102 // Release the callback to avoid a circullar reference in case an instance | |
103 // of this class is a member of a ref counted class, which instance is bound | |
104 // to this callback. | |
105 callback_ = CompletionCallback(); | |
106 } | |
107 } | |
108 | |
109 } // namespace app_file_handler_util | |
110 } // namespace extensions | |
OLD | NEW |