[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 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. |
[email protected] | 2a1ba51 | 2013-03-28 13:11:13 | [diff] [blame] | 98 | FILE* file = file_util::OpenFile(log_path_, "w"); |
| 99 | if (file == NULL) |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 100 | return; |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 101 | |
[email protected] | 2a1ba51 | 2013-03-28 13:11:13 | [diff] [blame] | 102 | net_log_logger_.reset(new NetLogLogger(file)); |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 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 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 117 | bool NetLogTempFile::GetFilePath(base::FilePath* path) { |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 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 | |
| 125 | DCHECK(!log_path_.empty()); |
| 126 | #if defined(OS_POSIX) |
| 127 | // Users, group and others can read, write and traverse. |
| 128 | int mode = file_util::FILE_PERMISSION_MASK; |
| 129 | file_util::SetPosixFilePermissions(log_path_, mode); |
| 130 | #endif // defined(OS_POSIX) |
| 131 | |
| 132 | *path = log_path_; |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | bool NetLogTempFile::GetNetExportLog() { |
| 137 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 138 | base::FilePath temp_dir; |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 139 | if (!GetNetExportLogDirectory(&temp_dir)) |
| 140 | return false; |
| 141 | |
| 142 | log_path_ = temp_dir.Append(log_filename_); |
| 143 | return true; |
| 144 | } |
| 145 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 146 | bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { |
[email protected] | e4a1cd4 | 2013-02-01 01:11:42 | [diff] [blame] | 147 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 148 | return file_util::GetTempDir(path); |
| 149 | } |
| 150 | |
| 151 | bool NetLogTempFile::NetExportLogExists() { |
| 152 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 153 | DCHECK(!log_path_.empty()); |
| 154 | return file_util::PathExists(log_path_); |
| 155 | } |