1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/file_select_helper_contacts_android.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/functional/bind.h"
10 #include "base/task/thread_pool.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/browser_task_traits.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/shell_dialogs/selected_file_info.h"
16 FileSelectHelperContactsAndroid::FileSelectHelperContactsAndroid(
18 : FileSelectHelper(profile) {}
20 void FileSelectHelperContactsAndroid::FileSelectedWithExtraInfo(
21 const ui::SelectedFileInfo& file,
24 base::ThreadPool::PostTask(
26 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
28 &FileSelectHelperContactsAndroid::ProcessContactsForAndroid, this,
32 void FileSelectHelperContactsAndroid::ProcessContactsForAndroid(
33 const std::string& contacts) {
34 base::FilePath temp_file;
35 if (base::CreateTemporaryFile(&temp_file)) {
37 WriteFile(temp_file, contacts.c_str(), contacts.length()) > 0;
38 temporary_files_.push_back(temp_file);
40 temp_file = base::FilePath();
43 content::GetUIThreadTaskRunner({})->PostTask(
46 &FileSelectHelperContactsAndroid::ProcessContactsForAndroidOnUIThread,
50 void FileSelectHelperContactsAndroid::ProcessContactsForAndroidOnUIThread(
51 const base::FilePath& temp_file) {
52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 std::vector<ui::SelectedFileInfo> files;
56 if (temp_file.empty()) {
57 ConvertToFileChooserFileInfoList(files);
61 ui::SelectedFileInfo file_info;
62 file_info.local_path = temp_file;
63 file_info.display_name = "contacts.json";
64 files.push_back(file_info);
66 // Typically, |temporary_files| are deleted after |web_contents_| is
67 // destroyed. If |web_contents_| is already NULL, then the temporary files
68 // need to be deleted now.
70 DeleteTemporaryFiles();
75 ConvertToFileChooserFileInfoList(files);