Chromium Code Reviews| 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_.get()) | |
|
mmenke
2013/01/28 21:51:41
nit: .get() not needed (And the preferred for is
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
| 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: | |
|
mmenke
2013/01/28 21:51:41
NOTREACHED()?
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
| 39 break; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 DictionaryValue* NetLogTempFile::GetState() { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 45 base::DictionaryValue* dict = new base::DictionaryValue; | |
| 46 | |
| 47 ignore_result(EnsureInit()); | |
|
mmenke
2013/01/28 21:51:41
I don't beloeve ignore_result is needed unless WAR
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
| 48 | |
| 49 #ifndef NDEBUG | |
| 50 dict->SetString("file", log_path_.LossyDisplayName()); | |
| 51 #endif // NDEBUG | |
| 52 | |
| 53 switch (state_) { | |
| 54 case STATE_ALLOW_START: | |
| 55 dict->SetString("state", "ALLOW_START"); | |
| 56 break; | |
| 57 case STATE_ALLOW_STOP: | |
| 58 dict->SetString("state", "ALLOW_STOP"); | |
| 59 break; | |
| 60 case STATE_ALLOW_START_SEND: | |
| 61 dict->SetString("state", "ALLOW_START_SEND"); | |
| 62 break; | |
| 63 default: | |
| 64 dict->SetString("state", "UNINITIALIZED"); | |
| 65 break; | |
| 66 } | |
| 67 return dict; | |
| 68 } | |
| 69 | |
| 70 bool NetLogTempFile::EnsureInit() { | |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 72 if (state_ != STATE_UNINITIALIZED) | |
| 73 return true; | |
| 74 | |
| 75 if (!GetNetExportLog()) | |
| 76 return false; | |
| 77 | |
| 78 if (NetExportLogExists()) | |
| 79 state_ = STATE_ALLOW_START_SEND; | |
| 80 else | |
| 81 state_ = STATE_ALLOW_START; | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void NetLogTempFile::StartNetLog() { | |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 88 if (state_ == STATE_ALLOW_STOP) | |
| 89 return; | |
| 90 | |
| 91 DCHECK_NE(state_, STATE_UNINITIALIZED); | |
|
mmenke
2013/01/28 21:51:41
nit: Expected goes first, so this should be:
DCH
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
| 92 DCHECK(!log_path_.empty()); | |
| 93 | |
| 94 // Try to make sure we can create the file. | |
| 95 // TODO(rtenneti): Find a better for doing the following. Surface some error | |
| 96 // to the user if we couldn't create the file. | |
| 97 FILE* fp = file_util::OpenFile(log_path_, "w"); | |
| 98 if (!fp) | |
| 99 return; | |
| 100 file_util::CloseFile(fp); | |
| 101 | |
| 102 net_log_logger_.reset(new NetLogLogger(log_path_)); | |
| 103 net_log_logger_->StartObserving(chrome_net_log_); | |
| 104 state_ = STATE_ALLOW_STOP; | |
| 105 } | |
| 106 | |
| 107 void NetLogTempFile::StopNetLog() { | |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 109 if (state_ != STATE_ALLOW_STOP) | |
| 110 return; | |
| 111 | |
| 112 net_log_logger_->StopObserving(); | |
| 113 net_log_logger_.reset(); | |
| 114 state_ = STATE_ALLOW_START_SEND; | |
| 115 } | |
| 116 | |
| 117 bool NetLogTempFile::GetFilePath(FilePath* path) { | |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 119 if (state_ != STATE_ALLOW_START_SEND) | |
| 120 return false; | |
| 121 | |
| 122 if (!NetExportLogExists()) | |
| 123 return false; | |
| 124 | |
|
mmenke
2013/01/28 21:51:41
optional: DCHECK(!log_path_.empty());
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
| 125 #if defined(OS_POSIX) | |
| 126 // Users, group and others can read, write and traverse. | |
| 127 int mode = file_util::FILE_PERMISSION_MASK; | |
| 128 file_util::SetPosixFilePermissions(log_path_, mode); | |
| 129 #endif // defined(OS_POSIX) | |
| 130 | |
| 131 *path = log_path_; | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 bool NetLogTempFile::GetNetExportLog() { | |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 137 FilePath temp_dir; | |
| 138 if (!GetNetExportLogDirectory(&temp_dir)) | |
| 139 return false; | |
| 140 | |
| 141 log_path_ = temp_dir.Append(log_filename_); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 bool NetLogTempFile::GetNetExportLogDirectory(FilePath* path) { | |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
| 147 return file_util::GetTempDir(path); | |
| 148 } | |
| 149 | |
| 150 bool NetLogTempFile::NetExportLogExists() { | |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
|
mmenke
2013/01/28 21:51:41
DCHECK_NE(STATE_UNINITIALIZED, state_);
DCHECK(!lo
ramant (doing other things)
2013/01/29 04:48:37
Added "DCHECK(!log_path_.empty());".
This functio
| |
| 152 return file_util::PathExists(log_path_); | |
| 153 } | |
| OLD | NEW |