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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_
11 #include "base/scoped_observer.h"
12 #include "chrome/browser/extensions/api/log_private/filter_handler.h"
13 #include "chrome/browser/extensions/api/log_private/log_parser.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "chrome/browser/feedback/system_logs/about_system_logs_fetcher.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"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "net/base/net_log.h"
22 #include "net/base/net_log_logger.h"
30 namespace extensions {
31 class ExtensionRegistry;
34 class FileResource : public ApiResource {
36 FileResource(const std::string& owner_extension_id,
37 const base::FilePath& path);
38 virtual ~FileResource();
40 // ApiResource overrides.
41 virtual bool IsPersistent() const override;
43 static const char kSequenceToken[];
44 static const base::SequencedWorkerPool::WorkerShutdown kShutdownBehavior =
45 base::SequencedWorkerPool::BLOCK_SHUTDOWN;
50 DISALLOW_COPY_AND_ASSIGN(FileResource);
53 class LogPrivateAPI : public BrowserContextKeyedAPI,
54 public ExtensionRegistryObserver,
55 public net::NetLog::ThreadSafeObserver {
57 // Convenience method to get the LogPrivateAPI for a profile.
58 static LogPrivateAPI* Get(content::BrowserContext* context);
60 explicit LogPrivateAPI(content::BrowserContext* context);
61 virtual ~LogPrivateAPI();
63 void StartNetInternalsWatch(const std::string& extension_id,
64 api::log_private::EventSink event_sink,
65 const base::Closure& closure);
66 void StopNetInternalsWatch(const std::string& extension_id,
67 const base::Closure& closure);
68 void StopAllWatches(const std::string& extension_id,
69 const base::Closure& closure);
70 void RegisterTempFile(const std::string& owner_extension_id,
71 const base::FilePath& file_path);
73 // BrowserContextKeyedAPI implementation.
74 static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance();
77 friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>;
80 // ExtensionRegistryObserver implementation.
81 virtual void OnExtensionUnloaded(
82 content::BrowserContext* browser_context,
83 const Extension* extension,
84 UnloadedExtensionInfo::Reason reason) override;
86 // ChromeNetLog::ThreadSafeObserver implementation:
87 virtual void OnAddEntry(const net::NetLog::Entry& entry) override;
89 void PostPendingEntries();
90 void AddEntriesOnUI(scoped_ptr<base::ListValue> value);
92 // Initializes a new instance of net::NetLogLogger and passes it back via
93 // |net_logger| param.
94 void InitializeNetLogger(const std::string& owner_extension_id,
95 net::NetLogLogger** net_logger);
97 // Starts observing network events with a new |net_logger| instance.
98 void StartObservingNetEvents(IOThread* io_thread,
99 net::NetLogLogger** net_logger);
100 void MaybeStartNetInternalLogging(const std::string& caller_extension_id,
102 api::log_private::EventSink event_sink);
103 void MaybeStopNetInternalLogging(const base::Closure& closure);
104 void StopNetInternalLogging();
106 // BrowserContextKeyedAPI implementation.
107 static const char* service_name() {
108 return "LogPrivateAPI";
110 static const bool kServiceIsNULLWhileTesting = true;
111 static const bool kServiceRedirectedInIncognito = true;
113 content::BrowserContext* const browser_context_;
114 bool logging_net_internals_;
115 api::log_private::EventSink event_sink_;
116 std::set<std::string> net_internal_watches_;
117 scoped_ptr<base::ListValue> pending_entries_;
118 scoped_ptr<net::NetLogLogger> net_log_logger_;
119 // Listen to extension unloaded notifications.
120 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
121 extension_registry_observer_;
122 ApiResourceManager<FileResource, WorkerPoolThreadTraits<FileResource> >
126 DISALLOW_COPY_AND_ASSIGN(LogPrivateAPI);
129 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction {
131 LogPrivateGetHistoricalFunction();
132 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical",
133 LOGPRIVATE_GETHISTORICAL);
136 virtual ~LogPrivateGetHistoricalFunction();
137 virtual bool RunAsync() override;
140 void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info);
142 scoped_ptr<FilterHandler> filter_handler_;
144 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction);
147 class LogPrivateStartEventRecorderFunction : public AsyncExtensionFunction {
149 LogPrivateStartEventRecorderFunction();
150 DECLARE_EXTENSION_FUNCTION("logPrivate.startEventRecorder",
151 LOGPRIVATE_STARTEVENTRECODER);
154 virtual ~LogPrivateStartEventRecorderFunction();
155 virtual bool RunAsync() override;
158 void OnEventRecorderStarted();
160 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartEventRecorderFunction);
163 class LogPrivateStopEventRecorderFunction : public AsyncExtensionFunction {
165 LogPrivateStopEventRecorderFunction();
166 DECLARE_EXTENSION_FUNCTION("logPrivate.stopEventRecorder",
167 LOGPRIVATE_STOPEVENTRECODER);
170 virtual ~LogPrivateStopEventRecorderFunction();
172 // AsyncExtensionFunction overrides.
173 virtual bool RunAsync() override;
176 void OnEventRecorderStopped();
178 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopEventRecorderFunction);
181 class LogPrivateDumpLogsFunction : public AsyncExtensionFunction {
183 LogPrivateDumpLogsFunction();
184 DECLARE_EXTENSION_FUNCTION("logPrivate.dumpLogs", LOGPRIVATE_DUMPLOGS);
187 virtual ~LogPrivateDumpLogsFunction();
189 // AsyncExtensionFunction overrides.
190 virtual bool RunAsync() override;
193 // Callback for DebugLogWriter::StoreLogs() call.
194 void OnStoreLogsCompleted(const base::FilePath& log_path, bool succeeded);
195 // Callback for LogPrivateAPI::StopAllWatches() call.
196 void OnStopAllWatches();
197 DISALLOW_COPY_AND_ASSIGN(LogPrivateDumpLogsFunction);
200 } // namespace extensions
202 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_