blob: 136d196b89c5c1fadbb27226e4d2f0fc8bed1594 [file] [log] [blame]
asanka655d1112015-03-07 05:33:411// Copyright 2015 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
7#include "base/files/file.h"
8#include "base/files/file_util.h"
9#include "base/logging.h"
Gabriel Charette44db1422018-08-06 11:19:3310#include "base/task/post_task.h"
asanka655d1112015-03-07 05:33:4111#include "chrome/browser/platform_util_internal.h"
12#include "content/public/browser/browser_thread.h"
13
14using content::BrowserThread;
15
16namespace platform_util {
17
18namespace {
19
20bool shell_operations_allowed = true;
21
22void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path,
23 OpenItemType type,
24 const OpenOperationCallback& callback) {
25 base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
26 if (!base::PathExists(path)) {
27 if (!callback.is_null())
tzik3f7781d2017-04-20 17:09:3328 BrowserThread::PostTask(
29 BrowserThread::UI, FROM_HERE,
30 base::BindOnce(callback, OPEN_FAILED_PATH_NOT_FOUND));
asanka655d1112015-03-07 05:33:4131 return;
32 }
33 if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
34 if (!callback.is_null())
tzik3f7781d2017-04-20 17:09:3335 BrowserThread::PostTask(
36 BrowserThread::UI, FROM_HERE,
37 base::BindOnce(callback, OPEN_FAILED_INVALID_TYPE));
asanka655d1112015-03-07 05:33:4138 return;
39 }
40
41 if (shell_operations_allowed)
42 internal::PlatformOpenVerifiedItem(path, type);
43 if (!callback.is_null())
44 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
tzik3f7781d2017-04-20 17:09:3345 base::BindOnce(callback, OPEN_SUCCEEDED));
asanka655d1112015-03-07 05:33:4146}
47
48} // namespace
49
50namespace internal {
51
52void DisableShellOperationsForTesting() {
53 shell_operations_allowed = false;
54}
55
56} // namespace internal
57
58void OpenItem(Profile* profile,
59 const base::FilePath& full_path,
60 OpenItemType item_type,
61 const OpenOperationCallback& callback) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
fdoray46e31b52017-02-07 15:59:3763 base::PostTaskWithTraits(FROM_HERE,
Gabriel Charetteb10aeebc2018-07-26 20:15:0064 {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
tzik3f7781d2017-04-20 17:09:3365 base::BindOnce(&VerifyAndOpenItemOnBlockingThread,
66 full_path, item_type, callback));
asanka655d1112015-03-07 05:33:4167}
68
69} // namespace platform_util