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