blob: 8ad9fb670ee9b7e65f7552abd93c880b611742a7 [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"
fdoray46e31b52017-02-07 15:59:3710#include "base/task_scheduler/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())
28 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
29 base::Bind(callback, OPEN_FAILED_PATH_NOT_FOUND));
30 return;
31 }
32 if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
33 if (!callback.is_null())
34 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
35 base::Bind(callback, OPEN_FAILED_INVALID_TYPE));
36 return;
37 }
38
39 if (shell_operations_allowed)
40 internal::PlatformOpenVerifiedItem(path, type);
41 if (!callback.is_null())
42 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
43 base::Bind(callback, OPEN_SUCCEEDED));
44}
45
46} // namespace
47
48namespace internal {
49
50void DisableShellOperationsForTesting() {
51 shell_operations_allowed = false;
52}
53
54} // namespace internal
55
56void OpenItem(Profile* profile,
57 const base::FilePath& full_path,
58 OpenItemType item_type,
59 const OpenOperationCallback& callback) {
60 DCHECK_CURRENTLY_ON(BrowserThread::UI);
fdoray46e31b52017-02-07 15:59:3761 base::PostTaskWithTraits(FROM_HERE,
62 base::TaskTraits().MayBlock().WithPriority(
63 base::TaskPriority::BACKGROUND),
64 base::Bind(&VerifyAndOpenItemOnBlockingThread,
65 full_path, item_type, callback));
asanka655d1112015-03-07 05:33:4166}
67
68} // namespace platform_util