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 (state_ == STATE_UNINITIALIZED) | |
30 Init(); | |
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 // Determine the state_ if it is not set. | |
47 if (state_ == STATE_UNINITIALIZED) | |
48 Init(); | |
49 | |
50 base::DictionaryValue* dict = new base::DictionaryValue; | |
51 | |
52 #ifndef NDEBUG | |
53 dict->SetString("file", log_path_.LossyDisplayName()); | |
54 #endif // NDEBUG | |
55 | |
56 switch (state_) { | |
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 void NetLogTempFile::Init() { | |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
72 if (state_ != STATE_UNINITIALIZED) | |
73 return; | |
74 | |
75 if (!GetNetExportLog()) | |
76 return; | |
77 | |
78 if (NetExportLogExists()) | |
79 state_ = STATE_ALLOW_START_SEND; | |
80 else | |
81 state_ = STATE_ALLOW_START; | |
82 } | |
83 | |
84 void NetLogTempFile::StartNetLog() { | |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
86 if (state_ == STATE_ALLOW_STOP) | |
87 return; | |
88 | |
mmenke
2013/01/24 03:55:34
Could you add a:
if (!GetNetExportLog())
return
ramant (doing other things)
2013/01/24 21:56:49
Good point. Added the following checks and state_
| |
89 // Try to make sure we can create the file. | |
90 // TODO(rtenneti): Find a better for doing the following. | |
91 FILE* fp = file_util::OpenFile(log_path_, "w"); | |
92 if (!fp) | |
93 return; | |
94 file_util::CloseFile(fp); | |
95 | |
96 net_log_logger_.reset(new NetLogLogger(log_path_)); | |
97 net_log_logger_->StartObserving(chrome_net_log_); | |
98 state_ = STATE_ALLOW_STOP; | |
99 } | |
100 | |
101 void NetLogTempFile::StopNetLog() { | |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
103 if (state_ != STATE_ALLOW_STOP) | |
104 return; | |
105 | |
106 net_log_logger_->StopObserving(); | |
107 net_log_logger_.reset(); | |
108 state_ = STATE_ALLOW_START_SEND; | |
109 } | |
110 | |
111 bool NetLogTempFile::GetFilePath(FilePath* path) { | |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
113 if (state_ != STATE_ALLOW_START_SEND) | |
114 return false; | |
115 | |
116 if (!NetExportLogExists()) | |
117 return false; | |
118 | |
119 #if defined(OS_POSIX) | |
120 // Users, group and others can read, write and traverse. | |
121 int mode = file_util::FILE_PERMISSION_MASK; | |
122 file_util::SetPosixFilePermissions(log_path_, mode); | |
123 #endif // defined(OS_POSIX) | |
124 | |
125 *path = log_path_; | |
126 return true; | |
127 } | |
128 | |
129 bool NetLogTempFile::GetNetExportLog() { | |
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
131 FilePath temp_dir; | |
132 if (!file_util::GetTempDir(&temp_dir)) | |
133 return false; | |
134 | |
135 log_path_ = temp_dir.Append(log_filename_); | |
136 return true; | |
137 } | |
138 | |
139 bool NetLogTempFile::NetExportLogExists() { | |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
141 return file_util::PathExists(log_path_); | |
142 } | |
OLD | NEW |