[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / file_select_helper_contacts_android.cc
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.
4
5 #include "chrome/browser/file_select_helper_contacts_android.h"
6
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"
15
16 FileSelectHelperContactsAndroid::FileSelectHelperContactsAndroid(
17     Profile* profile)
18     : FileSelectHelper(profile) {}
19
20 void FileSelectHelperContactsAndroid::FileSelectedWithExtraInfo(
21     const ui::SelectedFileInfo& file,
22     int index,
23     void* params) {
24   base::ThreadPool::PostTask(
25       FROM_HERE,
26       {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
27       base::BindOnce(
28           &FileSelectHelperContactsAndroid::ProcessContactsForAndroid, this,
29           (char*)params));
30 }
31
32 void FileSelectHelperContactsAndroid::ProcessContactsForAndroid(
33     const std::string& contacts) {
34   base::FilePath temp_file;
35   if (base::CreateTemporaryFile(&temp_file)) {
36     bool success =
37         WriteFile(temp_file, contacts.c_str(), contacts.length()) > 0;
38     temporary_files_.push_back(temp_file);
39     if (!success)
40       temp_file = base::FilePath();
41   }
42
43   content::GetUIThreadTaskRunner({})->PostTask(
44       FROM_HERE,
45       base::BindOnce(
46           &FileSelectHelperContactsAndroid::ProcessContactsForAndroidOnUIThread,
47           this, temp_file));
48 }
49
50 void FileSelectHelperContactsAndroid::ProcessContactsForAndroidOnUIThread(
51     const base::FilePath& temp_file) {
52   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
53
54   std::vector<ui::SelectedFileInfo> files;
55
56   if (temp_file.empty()) {
57     ConvertToFileChooserFileInfoList(files);
58     return;
59   }
60
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);
65
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.
69   if (!web_contents_) {
70     DeleteTemporaryFiles();
71     RunFileChooserEnd();
72     return;
73   }
74
75   ConvertToFileChooserFileInfoList(files);
76 }