blob: 2308a40d4535db4432ab527b34ae76693ce3927d [file] [log] [blame]
James Cook7415f4c72020-10-22 17:40:581// 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 Cook21b97902020-11-03 01:03:397#include "base/bind.h"
James Cookc559a502020-10-22 20:14:048#include "base/logging.h"
James Cook7415f4c72020-10-22 17:40:589#include "base/notreached.h"
10#include "chrome/browser/platform_util_internal.h"
James Cookc559a502020-10-22 20:14:0411#include "chromeos/crosapi/mojom/file_manager.mojom.h"
12#include "chromeos/lacros/lacros_chrome_service_impl.h"
James Cook13e14812020-11-21 02:19:1213#include "content/public/browser/browser_task_traits.h"
14#include "content/public/browser/browser_thread.h"
James Cook7415f4c72020-10-22 17:40:5815
16namespace platform_util {
James Cook21b97902020-11-03 01:03:3917namespace {
18
19void 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 Cooka3883a512020-12-01 00:12:1529// Requests that ash open an item at |path|.
James Cook13e14812020-11-21 02:19:1230void OpenItemOnUiThread(const base::FilePath& path, OpenItemType type) {
31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
James Cook21b97902020-11-03 01:03:3932 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 Cook7415f4c72020-10-22 17:40:5847}
48
James Cook13e14812020-11-21 02:19:1249} // namespace
50
51namespace internal {
52
53void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) {
James Cooka3883a512020-12-01 00:12:1554 // The file manager remote can only be accessed on the UI thread.
James Cook13e14812020-11-21 02:19:1255 content::GetUIThreadTaskRunner({})->PostTask(
56 FROM_HERE, base::BindOnce(&OpenItemOnUiThread, path, type));
57}
58
James Cook7415f4c72020-10-22 17:40:5859} // namespace internal
60
61void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
James Cook13e14812020-11-21 02:19:1262 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
James Cookc559a502020-10-22 20:14:0463 auto* service = chromeos::LacrosChromeServiceImpl::Get();
James Cook21b97902020-11-03 01:03:3964 int interface_version =
65 service->GetInterfaceVersion(crosapi::mojom::FileManager::Uuid_);
James Cook21b97902020-11-03 01:03:3966 if (interface_version < 1) {
James Cook52694fe2021-01-26 21:48:0267 DLOG(ERROR) << "Unsupported ash version.";
James Cook21b97902020-11-03 01:03:3968 return;
69 }
70 service->file_manager_remote()->ShowItemInFolder(
71 full_path, base::BindOnce(&OnOpenResult, full_path));
James Cook7415f4c72020-10-22 17:40:5872}
73
74void 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