James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 1 | // Copyright 2020 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/platform_util.h" |
| 6 | |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 7 | #include "base/bind.h" |
James Cook | c559a50 | 2020-10-22 20:14:04 | [diff] [blame] | 8 | #include "base/logging.h" |
James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 9 | #include "base/notreached.h" |
| 10 | #include "chrome/browser/platform_util_internal.h" |
James Cook | c559a50 | 2020-10-22 20:14:04 | [diff] [blame] | 11 | #include "chromeos/crosapi/mojom/file_manager.mojom.h" |
| 12 | #include "chromeos/lacros/lacros_chrome_service_impl.h" |
James Cook | 13e1481 | 2020-11-21 02:19:12 | [diff] [blame] | 13 | #include "content/public/browser/browser_task_traits.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 15 | |
| 16 | namespace platform_util { |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | void OnOpenResult(const base::FilePath& path, |
| 20 | crosapi::mojom::OpenResult result) { |
| 21 | if (result == crosapi::mojom::OpenResult::kSucceeded) |
| 22 | return; |
| 23 | // TODO(https://crbug.com/1144316): Show error messages. This will require |
| 24 | // refactoring the existing file manager string files, or introducing new |
| 25 | // lacros strings. |
| 26 | LOG(ERROR) << "Unable to open " << path.AsUTF8Unsafe() << " " << result; |
| 27 | } |
| 28 | |
James Cook | a3883a51 | 2020-12-01 00:12:15 | [diff] [blame] | 29 | // Requests that ash open an item at |path|. |
James Cook | 13e1481 | 2020-11-21 02:19:12 | [diff] [blame] | 30 | void OpenItemOnUiThread(const base::FilePath& path, OpenItemType type) { |
| 31 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 32 | auto* service = chromeos::LacrosChromeServiceImpl::Get(); |
| 33 | if (service->GetInterfaceVersion(crosapi::mojom::FileManager::Uuid_) < 1) { |
| 34 | LOG(ERROR) << "Unsupported ash version."; |
| 35 | return; |
| 36 | } |
| 37 | switch (type) { |
| 38 | case OPEN_FILE: |
| 39 | service->file_manager_remote()->OpenFile( |
| 40 | path, base::BindOnce(&OnOpenResult, path)); |
| 41 | break; |
| 42 | case OPEN_FOLDER: |
| 43 | service->file_manager_remote()->OpenFolder( |
| 44 | path, base::BindOnce(&OnOpenResult, path)); |
| 45 | break; |
| 46 | } |
James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 47 | } |
| 48 | |
James Cook | 13e1481 | 2020-11-21 02:19:12 | [diff] [blame] | 49 | } // namespace |
| 50 | |
| 51 | namespace internal { |
| 52 | |
| 53 | void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) { |
James Cook | a3883a51 | 2020-12-01 00:12:15 | [diff] [blame] | 54 | // The file manager remote can only be accessed on the UI thread. |
James Cook | 13e1481 | 2020-11-21 02:19:12 | [diff] [blame] | 55 | content::GetUIThreadTaskRunner({})->PostTask( |
| 56 | FROM_HERE, base::BindOnce(&OpenItemOnUiThread, path, type)); |
| 57 | } |
| 58 | |
James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 59 | } // namespace internal |
| 60 | |
| 61 | void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { |
James Cook | 13e1481 | 2020-11-21 02:19:12 | [diff] [blame] | 62 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
James Cook | c559a50 | 2020-10-22 20:14:04 | [diff] [blame] | 63 | auto* service = chromeos::LacrosChromeServiceImpl::Get(); |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 64 | int interface_version = |
| 65 | service->GetInterfaceVersion(crosapi::mojom::FileManager::Uuid_); |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 66 | if (interface_version < 1) { |
James Cook | 52694fe | 2021-01-26 21:48:02 | [diff] [blame] | 67 | DLOG(ERROR) << "Unsupported ash version."; |
James Cook | 21b9790 | 2020-11-03 01:03:39 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | service->file_manager_remote()->ShowItemInFolder( |
| 71 | full_path, base::BindOnce(&OnOpenResult, full_path)); |
James Cook | 7415f4c7 | 2020-10-22 17:40:58 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void OpenExternal(Profile* profile, const GURL& url) { |
| 75 | // TODO(https://crbug.com/1140585): Add crosapi for opening links with |
| 76 | // external protocol handlers. |
| 77 | NOTIMPLEMENTED(); |
| 78 | } |
| 79 | |
| 80 | } // namespace platform_util |