| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "content/browser/download/quarantine.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <sys/types.h> | |
| 9 #include <sys/xattr.h> | |
| 10 | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/threading/thread_restrictions.h" | |
| 15 #include "content/browser/download/quarantine_constants_linux.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 const char kSourceURLExtendedAttrName[] = "user.xdg.origin.url"; | |
| 21 const char kReferrerURLExtendedAttrName[] = "user.xdg.referrer.url"; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 bool SetExtendedFileAttribute(const char* path, | |
| 26 const char* name, | |
| 27 const char* value, | |
| 28 size_t value_size, | |
| 29 int flags) { | |
| 30 base::ThreadRestrictions::AssertIOAllowed(); | |
| 31 int result = setxattr(path, name, value, value_size, flags); | |
| 32 if (result) { | |
| 33 DPLOG(ERROR) << "Could not set extended attribute " << name << " on file " | |
| 34 << path; | |
| 35 return false; | |
| 36 } | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 QuarantineFileResult QuarantineFile(const base::FilePath& file, | |
| 43 const GURL& source_url, | |
| 44 const GURL& referrer_url, | |
| 45 const std::string& client_guid) { | |
| 46 DCHECK(base::PathIsWritable(file)); | |
| 47 | |
| 48 bool source_succeeded = | |
| 49 source_url.is_valid() && | |
| 50 SetExtendedFileAttribute(file.value().c_str(), kSourceURLExtendedAttrName, | |
| 51 source_url.spec().c_str(), | |
| 52 source_url.spec().length(), 0); | |
| 53 | |
| 54 // Referrer being empty is not considered an error. This could happen if the | |
| 55 // referrer policy resulted in an empty referrer for the download request. | |
| 56 bool referrer_succeeded = | |
| 57 !referrer_url.is_valid() || | |
| 58 SetExtendedFileAttribute( | |
| 59 file.value().c_str(), kReferrerURLExtendedAttrName, | |
| 60 referrer_url.spec().c_str(), referrer_url.spec().length(), 0); | |
| 61 return source_succeeded && referrer_succeeded | |
| 62 ? QuarantineFileResult::OK | |
| 63 : QuarantineFileResult::ANNOTATION_FAILED; | |
| 64 } | |
| 65 | |
| 66 } // namespace content | |
| OLD | NEW |