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()) | |
23 net_log_logger_->StopObserving(); | |
24 } | |
25 | |
26 void NetLogTempFile::ProcessCommand(Command command) { | |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
28 // Determine the state_ if it is not set. | |
29 if (!Init()) | |
mmenke
2013/01/25 17:39:12
Suggest renaming this to "EnsureInit()" to better
ramant (doing other things)
2013/01/25 20:05:24
Done.
| |
30 return; | |
31 | |
32 switch (command) { | |
33 case DO_START: | |
34 StartNetLog(); | |
35 break; | |
36 case DO_STOP: | |
37 StopNetLog(); | |
38 break; | |
39 default: | |
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 // Determine the state_ if it is not set. | |
49 if (!Init()) | |
50 return dict; | |
51 | |
52 #ifndef NDEBUG | |
53 dict->SetString("file", log_path_.LossyDisplayName()); | |
54 #endif // NDEBUG | |
55 | |
56 switch (state_) { | |
mmenke
2013/01/25 17:39:12
Think we should always return a state, even when I
ramant (doing other things)
2013/01/25 20:05:24
Done.
| |
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", "ALLOW_START"); | |
65 break; | |
66 } | |
67 return dict; | |
68 } | |
69 | |
70 bool NetLogTempFile::Init() { | |
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); | |
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. | |
96 FILE* fp = file_util::OpenFile(log_path_, "w"); | |
97 if (!fp) | |
mmenke
2013/01/25 17:39:12
Wonder if we should surface some error to the user
ramant (doing other things)
2013/01/25 20:05:24
Added this to TODO.
| |
98 return; | |
99 file_util::CloseFile(fp); | |
100 | |
101 net_log_logger_.reset(new NetLogLogger(log_path_)); | |
102 net_log_logger_->StartObserving(chrome_net_log_); | |
103 state_ = STATE_ALLOW_STOP; | |
104 } | |
105 | |
106 void NetLogTempFile::StopNetLog() { | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
108 if (state_ != STATE_ALLOW_STOP) | |
109 return; | |
110 | |
111 net_log_logger_->StopObserving(); | |
112 net_log_logger_.reset(); | |
113 state_ = STATE_ALLOW_START_SEND; | |
114 } | |
115 | |
116 bool NetLogTempFile::GetFilePath(FilePath* path) { | |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
118 if (state_ != STATE_ALLOW_START_SEND) | |
119 return false; | |
120 | |
121 if (!NetExportLogExists()) | |
122 return false; | |
123 | |
124 #if defined(OS_POSIX) | |
125 // Users, group and others can read, write and traverse. | |
126 int mode = file_util::FILE_PERMISSION_MASK; | |
127 file_util::SetPosixFilePermissions(log_path_, mode); | |
128 #endif // defined(OS_POSIX) | |
129 | |
130 *path = log_path_; | |
131 return true; | |
132 } | |
133 | |
134 bool NetLogTempFile::GetNetExportLog() { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
136 FilePath temp_dir; | |
137 if (!GetNetExportLogDirectory(&temp_dir)) | |
138 return false; | |
139 | |
140 log_path_ = temp_dir.Append(log_filename_); | |
141 return true; | |
142 } | |
143 | |
144 bool NetLogTempFile::GetNetExportLogDirectory(FilePath* path) { | |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
146 return file_util::GetTempDir(path); | |
147 } | |
148 | |
149 bool NetLogTempFile::NetExportLogExists() { | |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
151 return file_util::PathExists(log_path_); | |
152 } | |
OLD | NEW |