OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/scoped_observer.h" | 11 #include "base/scoped_observer.h" |
12 #include "chrome/browser/extensions/api/log_private/filter_handler.h" | 12 #include "chrome/browser/extensions/api/log_private/filter_handler.h" |
13 #include "chrome/browser/extensions/api/log_private/log_parser.h" | 13 #include "chrome/browser/extensions/api/log_private/log_parser.h" |
14 #include "chrome/browser/extensions/chrome_extension_function.h" | 14 #include "chrome/browser/extensions/chrome_extension_function.h" |
15 #include "chrome/browser/feedback/system_logs/about_system_logs_fetcher.h" | 15 #include "chrome/browser/feedback/system_logs/about_system_logs_fetcher.h" |
16 #include "chrome/common/extensions/api/log_private.h" | 16 #include "chrome/common/extensions/api/log_private.h" |
| 17 #include "extensions/browser/api/api_resource.h" |
| 18 #include "extensions/browser/api/api_resource_manager.h" |
17 #include "extensions/browser/browser_context_keyed_api_factory.h" | 19 #include "extensions/browser/browser_context_keyed_api_factory.h" |
18 #include "extensions/browser/extension_registry_observer.h" | 20 #include "extensions/browser/extension_registry_observer.h" |
19 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
| 22 #include "net/base/net_log_logger.h" |
| 23 |
| 24 class IOThread; |
20 | 25 |
21 namespace content { | 26 namespace content { |
22 class BrowserContext; | 27 class BrowserContext; |
23 } | 28 } |
24 | 29 |
25 namespace extensions { | 30 namespace extensions { |
26 class ExtensionRegistry; | 31 class ExtensionRegistry; |
27 | 32 |
| 33 // Tracked log files. |
| 34 class FileResource : public ApiResource { |
| 35 public: |
| 36 FileResource(const std::string& owner_extension_id, |
| 37 const base::FilePath& path); |
| 38 virtual ~FileResource(); |
| 39 |
| 40 // ApiResource overrides. |
| 41 virtual bool IsPersistent() const OVERRIDE; |
| 42 |
| 43 static const content::BrowserThread::ID kThreadId = |
| 44 content::BrowserThread::FILE; |
| 45 |
| 46 private: |
| 47 base::FilePath path_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(FileResource); |
| 50 }; |
| 51 |
28 class LogPrivateAPI : public BrowserContextKeyedAPI, | 52 class LogPrivateAPI : public BrowserContextKeyedAPI, |
29 public ExtensionRegistryObserver, | 53 public ExtensionRegistryObserver, |
30 public net::NetLog::ThreadSafeObserver { | 54 public net::NetLog::ThreadSafeObserver { |
31 public: | 55 public: |
32 // Convenience method to get the LogPrivateAPI for a profile. | 56 // Convenience method to get the LogPrivateAPI for a profile. |
33 static LogPrivateAPI* Get(content::BrowserContext* context); | 57 static LogPrivateAPI* Get(content::BrowserContext* context); |
34 | 58 |
35 explicit LogPrivateAPI(content::BrowserContext* context); | 59 explicit LogPrivateAPI(content::BrowserContext* context); |
36 virtual ~LogPrivateAPI(); | 60 virtual ~LogPrivateAPI(); |
37 | 61 |
38 void StartNetInternalsWatch(const std::string& extension_id); | 62 void StartNetInternalsWatch(const std::string& extension_id, |
39 void StopNetInternalsWatch(const std::string& extension_id); | 63 api::log_private::EventSink event_sink, |
| 64 const base::Closure& closure); |
| 65 void StopNetInternalsWatch(const std::string& extension_id, |
| 66 const base::Closure& closure); |
| 67 void StopAllWatches(const std::string& extension_id, |
| 68 const base::Closure& closure); |
| 69 void RegisterTempFile(const std::string& owner_extension_id, |
| 70 const base::FilePath& file_path); |
| 71 |
| 72 // BrowserContextKeyedAPI overrides. |
| 73 virtual void Shutdown() OVERRIDE; |
40 | 74 |
41 // BrowserContextKeyedAPI implementation. | 75 // BrowserContextKeyedAPI implementation. |
42 static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance(); | 76 static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance(); |
43 | 77 |
44 private: | 78 private: |
45 friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>; | 79 friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>; |
46 | 80 |
47 // ExtensionRegistryObserver implementation. | 81 // ExtensionRegistryObserver implementation. |
48 virtual void OnExtensionUnloaded( | 82 virtual void OnExtensionUnloaded( |
49 content::BrowserContext* browser_context, | 83 content::BrowserContext* browser_context, |
50 const Extension* extension, | 84 const Extension* extension, |
51 UnloadedExtensionInfo::Reason reason) OVERRIDE; | 85 UnloadedExtensionInfo::Reason reason) OVERRIDE; |
52 | 86 |
53 // ChromeNetLog::ThreadSafeObserver implementation: | 87 // ChromeNetLog::ThreadSafeObserver implementation: |
54 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; | 88 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; |
55 | 89 |
56 void PostPendingEntries(); | 90 void PostPendingEntries(); |
57 void AddEntriesOnUI(scoped_ptr<base::ListValue> value); | 91 void AddEntriesOnUI(scoped_ptr<base::ListValue> value); |
58 | 92 |
59 void MaybeStartNetInternalLogging(); | 93 // Initializes a new instance of net::NetLogLogger and passes it back via |
60 void MaybeStopNetInternalLogging(); | 94 // |net_logger| param. |
| 95 void InitializeNetLogger(const std::string& owner_extension_id, |
| 96 net::NetLogLogger** net_logger); |
| 97 |
| 98 // Starts observing network events with a new |net_logger| instance. |
| 99 void StartObservingNetEvents(IOThread* io_thread, |
| 100 net::NetLogLogger** net_logger); |
| 101 void MaybeStartNetInternalLogging(const std::string& caller_extension_id, |
| 102 IOThread* io_thread, |
| 103 api::log_private::EventSink event_sink); |
| 104 void MaybeStopNetInternalLogging(const base::Closure& closure); |
61 void StopNetInternalLogging(); | 105 void StopNetInternalLogging(); |
62 | 106 |
63 // BrowserContextKeyedAPI implementation. | 107 // BrowserContextKeyedAPI implementation. |
64 static const char* service_name() { | 108 static const char* service_name() { |
65 return "LogPrivateAPI"; | 109 return "LogPrivateAPI"; |
66 } | 110 } |
67 static const bool kServiceIsNULLWhileTesting = true; | 111 static const bool kServiceIsNULLWhileTesting = true; |
68 static const bool kServiceRedirectedInIncognito = true; | 112 static const bool kServiceRedirectedInIncognito = true; |
69 | 113 |
70 content::BrowserContext* const browser_context_; | 114 content::BrowserContext* const browser_context_; |
71 bool logging_net_internals_; | 115 bool logging_net_internals_; |
| 116 api::log_private::EventSink event_sink_; |
72 std::set<std::string> net_internal_watches_; | 117 std::set<std::string> net_internal_watches_; |
73 scoped_ptr<base::ListValue> pending_entries_; | 118 scoped_ptr<base::ListValue> pending_entries_; |
74 | 119 scoped_ptr<net::NetLogLogger> net_log_logger_; |
75 // Listen to extension unloaded notifications. | 120 // Listen to extension unloaded notifications. |
76 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 121 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
77 extension_registry_observer_; | 122 extension_registry_observer_; |
| 123 ApiResourceManager<FileResource> log_file_resources_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(LogPrivateAPI); |
78 }; | 126 }; |
79 | 127 |
80 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction { | 128 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction { |
81 public: | 129 public: |
82 LogPrivateGetHistoricalFunction(); | 130 LogPrivateGetHistoricalFunction(); |
83 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical", | 131 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical", |
84 LOGPRIVATE_GETHISTORICAL); | 132 LOGPRIVATE_GETHISTORICAL); |
85 | 133 |
86 protected: | 134 protected: |
87 virtual ~LogPrivateGetHistoricalFunction(); | 135 virtual ~LogPrivateGetHistoricalFunction(); |
88 virtual bool RunAsync() OVERRIDE; | 136 virtual bool RunAsync() OVERRIDE; |
89 | 137 |
90 private: | 138 private: |
91 void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info); | 139 void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info); |
92 | 140 |
93 scoped_ptr<FilterHandler> filter_handler_; | 141 scoped_ptr<FilterHandler> filter_handler_; |
94 | 142 |
95 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction); | 143 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction); |
96 }; | 144 }; |
97 | 145 |
98 class LogPrivateStartNetInternalsWatchFunction | 146 class LogPrivateStartEventRecorderFunction |
99 : public ChromeSyncExtensionFunction { | 147 : public AsyncExtensionFunction { |
100 public: | 148 public: |
101 LogPrivateStartNetInternalsWatchFunction(); | 149 LogPrivateStartEventRecorderFunction(); |
102 DECLARE_EXTENSION_FUNCTION("logPrivate.startNetInternalsWatch", | 150 DECLARE_EXTENSION_FUNCTION("logPrivate.startEventRecorder", |
103 LOGPRIVATE_STARTNETINTERNALSWATCH); | 151 LOGPRIVATE_STARTEVENTRECODER); |
104 | 152 |
105 protected: | 153 protected: |
106 virtual ~LogPrivateStartNetInternalsWatchFunction(); | 154 virtual ~LogPrivateStartEventRecorderFunction(); |
107 virtual bool RunSync() OVERRIDE; | 155 virtual bool RunAsync() OVERRIDE; |
108 | 156 |
109 private: | 157 private: |
110 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartNetInternalsWatchFunction); | 158 void OnEventRecorderStarted(); |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartEventRecorderFunction); |
111 }; | 161 }; |
112 | 162 |
113 class LogPrivateStopNetInternalsWatchFunction | 163 class LogPrivateStopEventRecorderFunction |
114 : public ChromeSyncExtensionFunction { | 164 : public AsyncExtensionFunction { |
115 public: | 165 public: |
116 LogPrivateStopNetInternalsWatchFunction(); | 166 LogPrivateStopEventRecorderFunction(); |
117 DECLARE_EXTENSION_FUNCTION("logPrivate.stopNetInternalsWatch", | 167 DECLARE_EXTENSION_FUNCTION("logPrivate.stopEventRecorder", |
118 LOGPRIVATE_STOPNETINTERNALSWATCH); | 168 LOGPRIVATE_STOPEVENTRECODER); |
119 | 169 |
120 protected: | 170 protected: |
121 virtual ~LogPrivateStopNetInternalsWatchFunction(); | 171 virtual ~LogPrivateStopEventRecorderFunction(); |
122 virtual bool RunSync() OVERRIDE; | 172 |
| 173 // AsyncExtensionFunction overrides. |
| 174 virtual bool RunAsync() OVERRIDE; |
123 | 175 |
124 private: | 176 private: |
125 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopNetInternalsWatchFunction); | 177 void OnEventRecorderStopped(); |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopEventRecorderFunction); |
126 }; | 180 }; |
127 | 181 |
| 182 class LogPrivateDumpLogsFunction |
| 183 : public AsyncExtensionFunction { |
| 184 public: |
| 185 LogPrivateDumpLogsFunction(); |
| 186 DECLARE_EXTENSION_FUNCTION("logPrivate.dumpLogs", |
| 187 LOGPRIVATE_DUMPLOGS); |
| 188 |
| 189 protected: |
| 190 virtual ~LogPrivateDumpLogsFunction(); |
| 191 |
| 192 // AsyncExtensionFunction overrides. |
| 193 virtual bool RunAsync() OVERRIDE; |
| 194 |
| 195 private: |
| 196 // Callback for DebugLogWriter::StoreLogs() call. |
| 197 void OnStoreLogsCompleted(const base::FilePath& log_path, |
| 198 bool succeeded); |
| 199 // Callback for LogPrivateAPI::StopAllWatches() call. |
| 200 void OnStopAllWatches(); |
| 201 DISALLOW_COPY_AND_ASSIGN(LogPrivateDumpLogsFunction); |
| 202 }; |
| 203 |
| 204 |
128 } // namespace extensions | 205 } // namespace extensions |
129 | 206 |
130 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ | 207 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ |
OLD | NEW |