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 | |
| 12 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) | |
| 13 : state_(STATE_UNINITIALIZED), | |
| 14 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), | |
| 15 chrome_net_log_(chrome_net_log) { | |
| 16 } | |
| 17 | |
| 18 NetLogTempFile::~NetLogTempFile() { | |
| 19 if (net_log_logger_.get()) | |
| 20 net_log_logger_->StopObserving(); | |
| 21 } | |
| 22 | |
| 23 void NetLogTempFile::ProcessCommand(Command command) { | |
|
mmenke
2013/01/23 19:01:27
Think you should DCHECK that all of these are call
ramant (doing other things)
2013/01/24 03:23:55
Done.
| |
| 24 // Determine the state_ if it is not set. | |
| 25 if (state_ == STATE_UNINITIALIZED) | |
| 26 Init(); | |
| 27 | |
| 28 switch (command) { | |
| 29 case DO_START: | |
| 30 StartNetLog(); | |
| 31 break; | |
| 32 case DO_STOP: | |
| 33 StopNetLog(); | |
| 34 break; | |
| 35 default: | |
| 36 break; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 DictionaryValue* NetLogTempFile::GetState() { | |
| 41 // Determine the state_ if it is not set. | |
| 42 if (state_ == STATE_UNINITIALIZED) | |
| 43 Init(); | |
| 44 | |
| 45 base::DictionaryValue* dict = new base::DictionaryValue; | |
| 46 | |
| 47 #ifndef NDEBUG | |
| 48 dict->SetString("file", log_path_.MaybeAsASCII()); | |
|
mmenke
2013/01/23 19:01:27
nit: Maybe LossyDisplayName() would be better?
ramant (doing other things)
2013/01/24 03:23:55
Done.
| |
| 49 #endif // NDEBUG | |
| 50 | |
| 51 switch (state_) { | |
| 52 case STATE_ALLOW_STOP: | |
| 53 dict->SetString("state", "ALLOW_STOP"); | |
| 54 break; | |
| 55 case STATE_ALLOW_START_SEND: | |
| 56 dict->SetString("state", "ALLOW_START_SEND"); | |
| 57 break; | |
| 58 default: | |
| 59 dict->SetString("state", "ALLOW_START"); | |
| 60 break; | |
| 61 } | |
| 62 return dict; | |
| 63 } | |
| 64 | |
| 65 void NetLogTempFile::Init() { | |
| 66 if (state_ != STATE_UNINITIALIZED) | |
| 67 return; | |
| 68 | |
| 69 if (GetNetExportLog(&log_path_)) | |
| 70 state_ = STATE_ALLOW_START_SEND; | |
| 71 else | |
| 72 state_ = STATE_ALLOW_START; | |
|
mmenke
2013/01/23 19:01:27
If file_util::GetTempDir fails, we'll end up in ST
ramant (doing other things)
2013/01/24 03:23:55
Done.
| |
| 73 } | |
| 74 | |
| 75 void NetLogTempFile::StartNetLog() { | |
| 76 if (state_ == STATE_ALLOW_STOP) | |
| 77 return; | |
| 78 | |
| 79 // Try to make sure we can create the file. | |
| 80 // TODO(rtenneti): Find a better for doing the following. | |
| 81 FILE* fp = file_util::OpenFile(log_path_, "w"); | |
| 82 if (!fp) | |
| 83 return; | |
| 84 file_util::CloseFile(fp); | |
| 85 | |
| 86 net_log_logger_.reset(new NetLogLogger(log_path_)); | |
| 87 net_log_logger_->StartObserving(chrome_net_log_); | |
| 88 state_ = STATE_ALLOW_STOP; | |
| 89 } | |
| 90 | |
| 91 void NetLogTempFile::StopNetLog() { | |
| 92 if (state_ != STATE_ALLOW_STOP) | |
| 93 return; | |
| 94 | |
| 95 net_log_logger_->StopObserving(); | |
| 96 net_log_logger_.reset(); | |
| 97 state_ = STATE_ALLOW_START_SEND; | |
| 98 } | |
| 99 | |
| 100 bool NetLogTempFile::GetFilePath(FilePath* path) { | |
| 101 if (state_ != STATE_ALLOW_START_SEND) | |
| 102 return false; | |
| 103 | |
| 104 FilePath net_export_log; | |
| 105 if (!GetNetExportLog(&net_export_log)) | |
| 106 return false; | |
| 107 | |
| 108 #if defined(OS_POSIX) | |
| 109 // Users, group and others can read, write and traverse. | |
| 110 int mode = file_util::FILE_PERMISSION_MASK; | |
| 111 file_util::SetPosixFilePermissions(net_export_log, mode); | |
| 112 #endif // defined(OS_POSIX) | |
| 113 | |
| 114 *path = net_export_log; | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 118 bool NetLogTempFile::GetNetExportLog(FilePath* path) { | |
| 119 FilePath temp_dir; | |
| 120 if (!file_util::GetTempDir(&temp_dir)) | |
| 121 return false; | |
| 122 | |
| 123 *path = temp_dir.Append(log_filename_); | |
| 124 return file_util::PathExists(*path); | |
| 125 } | |
| OLD | NEW |