Only set File::[tracing_]path_ when file tracing is enabled.

R=rvargas@chromium.org
BUG=none
TEST=less |path_| cruft lying around

Review URL: https://codereview.chromium.org/1216183004

Cr-Commit-Position: refs/heads/master@{#337168}


CrOS-Libchrome-Original-Commit: fd5b108c82d56f6022dfbe62a023d1e81ff6f83b
diff --git a/base/files/file.cc b/base/files/file.cc
index 6dfeb91..47b9f88 100644
--- a/base/files/file.cc
+++ b/base/files/file.cc
@@ -52,7 +52,7 @@
 
 File::File(RValue other)
     : file_(other.object->TakePlatformFile()),
-      path_(other.object->path_),
+      tracing_path_(other.object->tracing_path_),
       error_details_(other.object->error_details()),
       created_(other.object->created()),
       async_(other.object->async_) {
@@ -76,7 +76,7 @@
   if (this != other.object) {
     Close();
     SetPlatformFile(other.object->TakePlatformFile());
-    path_ = other.object->path_;
+    tracing_path_ = other.object->tracing_path_;
     error_details_ = other.object->error_details();
     created_ = other.object->created();
     async_ = other.object->async_;
@@ -90,9 +90,10 @@
     error_details_ = FILE_ERROR_ACCESS_DENIED;
     return;
   }
-  path_ = path;
+  if (FileTracing::IsCategoryEnabled())
+    tracing_path_ = path;
   SCOPED_FILE_TRACE("Initialize");
-  DoInitialize(flags);
+  DoInitialize(path, flags);
 }
 #endif
 
diff --git a/base/files/file.h b/base/files/file.h
index 94ef56f..cba4353 100644
--- a/base/files/file.h
+++ b/base/files/file.h
@@ -355,9 +355,9 @@
   };
 #endif
 
-  // Creates or opens the given file. Only called if |path_| has no
+  // Creates or opens the given file. Only called if |path| has no
   // traversal ('..') components.
-  void DoInitialize(uint32 flags);
+  void DoInitialize(const FilePath& path, uint32 flags);
 
   // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore,
   // cf. issue 473337.
@@ -371,8 +371,9 @@
   MemoryCheckingScopedFD file_;
 #endif
 
-  // Path that |Initialize()| was called with. Only set if safe (i.e. no '..').
-  FilePath path_;
+  // A path to use for tracing purposes. Set if file tracing is enabled during
+  // |Initialize()|.
+  FilePath tracing_path_;
 
   // Object tied to the lifetime of |this| that enables/disables tracing.
   FileTracing::ScopedEnabler trace_enabler_;
diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
index bb49d2d..7fb617c 100644
--- a/base/files/file_posix.cc
+++ b/base/files/file_posix.cc
@@ -466,7 +466,7 @@
 // NaCl doesn't implement system calls to open files directly.
 #if !defined(OS_NACL)
 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
-void File::DoInitialize(uint32 flags) {
+void File::DoInitialize(const FilePath& path, uint32 flags) {
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(!IsValid());
 
@@ -521,7 +521,7 @@
   mode |= S_IRGRP | S_IROTH;
 #endif
 
-  int descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
+  int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
 
   if (flags & FLAG_OPEN_ALWAYS) {
     if (descriptor < 0) {
@@ -529,7 +529,7 @@
       if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
         open_flags |= O_EXCL;   // together with O_CREAT implies O_NOFOLLOW
 
-      descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
+      descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
       if (descriptor >= 0)
         created_ = true;
     }
@@ -544,7 +544,7 @@
     created_ = true;
 
   if (flags & FLAG_DELETE_ON_CLOSE)
-    unlink(path_.value().c_str());
+    unlink(path.value().c_str());
 
   async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
   error_details_ = FILE_OK;
diff --git a/base/files/file_tracing.cc b/base/files/file_tracing.cc
index c25772d..92a5780 100644
--- a/base/files/file_tracing.cc
+++ b/base/files/file_tracing.cc
@@ -13,6 +13,11 @@
 }
 
 // static
+bool FileTracing::IsCategoryEnabled() {
+  return g_provider && g_provider->FileTracingCategoryIsEnabled();
+}
+
+// static
 void FileTracing::SetProvider(FileTracing::Provider* provider) {
   g_provider = provider;
 }
@@ -34,19 +39,11 @@
     g_provider->FileTracingEventEnd(name_, id_);
 }
 
-bool FileTracing::ScopedTrace::ShouldInitialize() const {
-  return g_provider && g_provider->FileTracingCategoryIsEnabled();
-}
-
 void FileTracing::ScopedTrace::Initialize(
     const char* name, File* file, int64 size) {
-  if (!g_provider)
-    return;
-
   id_ = &file->trace_enabler_;
   name_ = name;
-
-  g_provider->FileTracingEventBegin(name_, id_, file->path_, size);
+  g_provider->FileTracingEventBegin(name_, id_, file->tracing_path_, size);
 }
 
 }  // namespace base
diff --git a/base/files/file_tracing.h b/base/files/file_tracing.h
index 149bd78..92324c9 100644
--- a/base/files/file_tracing.h
+++ b/base/files/file_tracing.h
@@ -13,7 +13,7 @@
 
 #define SCOPED_FILE_TRACE_WITH_SIZE(name, size) \
     FileTracing::ScopedTrace scoped_file_trace; \
-    if (scoped_file_trace.ShouldInitialize()) \
+    if (FileTracing::IsCategoryEnabled()) \
       scoped_file_trace.Initialize(FILE_TRACING_PREFIX "::" name, this, size)
 
 #define SCOPED_FILE_TRACE(name) SCOPED_FILE_TRACE_WITH_SIZE(name, 0)
@@ -25,6 +25,9 @@
 
 class BASE_EXPORT FileTracing {
  public:
+  // Whether the file tracing category is enabled.
+  static bool IsCategoryEnabled();
+
   class Provider {
    public:
     // Whether the file tracing category is currently enabled.
@@ -61,9 +64,6 @@
     ScopedTrace();
     ~ScopedTrace();
 
-    // Whether this trace should be initialized or not.
-    bool ShouldInitialize() const;
-
     // Called only if the tracing category is enabled. |name| is the name of the
     // event to trace (e.g. "Read", "Write") and must have an application
     // lifetime (e.g. static or literal). |file| is the file being traced; must