OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/net/net_log_temp_file.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/net/chrome_net_log.h" |
| 10 #include "chrome/browser/net/net_log_logger.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) |
| 16 : state_(STATE_UNINITIALIZED), |
| 17 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), |
| 18 chrome_net_log_(chrome_net_log) { |
| 19 } |
| 20 |
| 21 NetLogTempFile::~NetLogTempFile() { |
| 22 if (net_log_logger_) |
| 23 net_log_logger_->StopObserving(); |
| 24 } |
| 25 |
| 26 void NetLogTempFile::ProcessCommand(Command command) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 28 if (!EnsureInit()) |
| 29 return; |
| 30 |
| 31 switch (command) { |
| 32 case DO_START: |
| 33 StartNetLog(); |
| 34 break; |
| 35 case DO_STOP: |
| 36 StopNetLog(); |
| 37 break; |
| 38 default: |
| 39 NOTREACHED(); |
| 40 break; |
| 41 } |
| 42 } |
| 43 |
| 44 DictionaryValue* NetLogTempFile::GetState() { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 46 base::DictionaryValue* dict = new base::DictionaryValue; |
| 47 |
| 48 EnsureInit(); |
| 49 |
| 50 #ifndef NDEBUG |
| 51 dict->SetString("file", log_path_.LossyDisplayName()); |
| 52 #endif // NDEBUG |
| 53 |
| 54 switch (state_) { |
| 55 case STATE_ALLOW_START: |
| 56 dict->SetString("state", "ALLOW_START"); |
| 57 break; |
| 58 case STATE_ALLOW_STOP: |
| 59 dict->SetString("state", "ALLOW_STOP"); |
| 60 break; |
| 61 case STATE_ALLOW_START_SEND: |
| 62 dict->SetString("state", "ALLOW_START_SEND"); |
| 63 break; |
| 64 default: |
| 65 dict->SetString("state", "UNINITIALIZED"); |
| 66 break; |
| 67 } |
| 68 return dict; |
| 69 } |
| 70 |
| 71 bool NetLogTempFile::EnsureInit() { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 73 if (state_ != STATE_UNINITIALIZED) |
| 74 return true; |
| 75 |
| 76 if (!GetNetExportLog()) |
| 77 return false; |
| 78 |
| 79 if (NetExportLogExists()) |
| 80 state_ = STATE_ALLOW_START_SEND; |
| 81 else |
| 82 state_ = STATE_ALLOW_START; |
| 83 |
| 84 return true; |
| 85 } |
| 86 |
| 87 void NetLogTempFile::StartNetLog() { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 89 if (state_ == STATE_ALLOW_STOP) |
| 90 return; |
| 91 |
| 92 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 93 DCHECK(!log_path_.empty()); |
| 94 |
| 95 // Try to make sure we can create the file. |
| 96 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 97 // to the user if we couldn't create the file. |
| 98 FILE* fp = file_util::OpenFile(log_path_, "w"); |
| 99 if (!fp) |
| 100 return; |
| 101 file_util::CloseFile(fp); |
| 102 |
| 103 net_log_logger_.reset(new NetLogLogger(log_path_)); |
| 104 net_log_logger_->StartObserving(chrome_net_log_); |
| 105 state_ = STATE_ALLOW_STOP; |
| 106 } |
| 107 |
| 108 void NetLogTempFile::StopNetLog() { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 110 if (state_ != STATE_ALLOW_STOP) |
| 111 return; |
| 112 |
| 113 net_log_logger_->StopObserving(); |
| 114 net_log_logger_.reset(); |
| 115 state_ = STATE_ALLOW_START_SEND; |
| 116 } |
| 117 |
| 118 bool NetLogTempFile::GetFilePath(FilePath* path) { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 120 if (state_ != STATE_ALLOW_START_SEND) |
| 121 return false; |
| 122 |
| 123 if (!NetExportLogExists()) |
| 124 return false; |
| 125 |
| 126 DCHECK(!log_path_.empty()); |
| 127 #if defined(OS_POSIX) |
| 128 // Users, group and others can read, write and traverse. |
| 129 int mode = file_util::FILE_PERMISSION_MASK; |
| 130 file_util::SetPosixFilePermissions(log_path_, mode); |
| 131 #endif // defined(OS_POSIX) |
| 132 |
| 133 *path = log_path_; |
| 134 return true; |
| 135 } |
| 136 |
| 137 bool NetLogTempFile::GetNetExportLog() { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 139 FilePath temp_dir; |
| 140 if (!GetNetExportLogDirectory(&temp_dir)) |
| 141 return false; |
| 142 |
| 143 log_path_ = temp_dir.Append(log_filename_); |
| 144 return true; |
| 145 } |
| 146 |
| 147 bool NetLogTempFile::GetNetExportLogDirectory(FilePath* path) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 149 return file_util::GetTempDir(path); |
| 150 } |
| 151 |
| 152 bool NetLogTempFile::NetExportLogExists() { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 154 DCHECK(!log_path_.empty()); |
| 155 return file_util::PathExists(log_path_); |
| 156 } |
OLD | NEW |