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/files/scoped_file.h"
12 #include "base/scoped_observer.h"
13 #include "chrome/browser/extensions/api/log_private/filter_handler.h"
14 #include "chrome/browser/extensions/api/log_private/log_parser.h"
15 #include "chrome/browser/extensions/chrome_extension_function.h"
16 #include "chrome/browser/feedback/system_logs/about_system_logs_fetcher.h"
17 #include "chrome/common/extensions/api/log_private.h"
18 #include "extensions/browser/api/api_resource.h"
19 #include "extensions/browser/api/api_resource_manager.h"
20 #include "extensions/browser/browser_context_keyed_api_factory.h"
21 #include "extensions/browser/extension_registry_observer.h"
22 #include "net/log/net_log.h"
23 #include "net/log/write_to_file_net_log_observer.h"
31 namespace extensions
{
32 class ExtensionRegistry
;
35 class FileResource
: public ApiResource
{
37 FileResource(const std::string
& owner_extension_id
,
38 const base::FilePath
& path
);
39 ~FileResource() override
;
41 // ApiResource overrides.
42 bool IsPersistent() const override
;
44 static const char kSequenceToken
[];
45 static const base::SequencedWorkerPool::WorkerShutdown kShutdownBehavior
=
46 base::SequencedWorkerPool::BLOCK_SHUTDOWN
;
51 DISALLOW_COPY_AND_ASSIGN(FileResource
);
54 class LogPrivateAPI
: public BrowserContextKeyedAPI
,
55 public ExtensionRegistryObserver
,
56 public net::NetLog::ThreadSafeObserver
{
58 // Convenience method to get the LogPrivateAPI for a profile.
59 static LogPrivateAPI
* Get(content::BrowserContext
* context
);
61 explicit LogPrivateAPI(content::BrowserContext
* context
);
62 ~LogPrivateAPI() override
;
64 void StartNetInternalsWatch(const std::string
& extension_id
,
65 api::log_private::EventSink event_sink
,
66 const base::Closure
& closure
);
67 void StopNetInternalsWatch(const std::string
& extension_id
,
68 const base::Closure
& closure
);
69 void StopAllWatches(const std::string
& extension_id
,
70 const base::Closure
& closure
);
71 void RegisterTempFile(const std::string
& owner_extension_id
,
72 const base::FilePath
& file_path
);
74 // BrowserContextKeyedAPI implementation.
75 static BrowserContextKeyedAPIFactory
<LogPrivateAPI
>* GetFactoryInstance();
78 friend class BrowserContextKeyedAPIFactory
<LogPrivateAPI
>;
81 // ExtensionRegistryObserver implementation.
82 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
83 const Extension
* extension
,
84 UnloadedExtensionInfo::Reason reason
) override
;
86 // ChromeNetLog::ThreadSafeObserver implementation:
87 void OnAddEntry(const net::NetLog::Entry
& entry
) override
;
89 void PostPendingEntries();
90 void AddEntriesOnUI(scoped_ptr
<base::ListValue
> value
);
92 // Creates a file that will be written to by net::WriteToFileNetLogObserver.
93 void CreateTempNetLogFile(const std::string
& owner_extension_id
,
94 base::ScopedFILE
* file
);
96 // Starts observing network events with a new |net_logger| instance.
97 void StartObservingNetEvents(IOThread
* io_thread
, base::ScopedFILE
* file
);
98 void MaybeStartNetInternalLogging(const std::string
& caller_extension_id
,
100 api::log_private::EventSink event_sink
);
101 void MaybeStopNetInternalLogging(const base::Closure
& closure
);
102 void StopNetInternalLogging();
104 // BrowserContextKeyedAPI implementation.
105 static const char* service_name() {
106 return "LogPrivateAPI";
108 static const bool kServiceIsNULLWhileTesting
= true;
109 static const bool kServiceRedirectedInIncognito
= true;
111 content::BrowserContext
* const browser_context_
;
112 bool logging_net_internals_
;
113 api::log_private::EventSink event_sink_
;
114 std::set
<std::string
> net_internal_watches_
;
115 scoped_ptr
<base::ListValue
> pending_entries_
;
116 scoped_ptr
<net::WriteToFileNetLogObserver
> write_to_file_observer_
;
117 // Listen to extension unloaded notifications.
118 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
119 extension_registry_observer_
;
120 ApiResourceManager
<FileResource
, WorkerPoolThreadTraits
<FileResource
> >
124 DISALLOW_COPY_AND_ASSIGN(LogPrivateAPI
);
127 class LogPrivateGetHistoricalFunction
: public AsyncExtensionFunction
{
129 LogPrivateGetHistoricalFunction();
130 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical",
131 LOGPRIVATE_GETHISTORICAL
);
134 ~LogPrivateGetHistoricalFunction() override
;
135 bool RunAsync() override
;
138 void OnSystemLogsLoaded(scoped_ptr
<system_logs::SystemLogsResponse
> sys_info
);
140 scoped_ptr
<FilterHandler
> filter_handler_
;
142 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction
);
145 class LogPrivateStartEventRecorderFunction
: public AsyncExtensionFunction
{
147 LogPrivateStartEventRecorderFunction();
148 DECLARE_EXTENSION_FUNCTION("logPrivate.startEventRecorder",
149 LOGPRIVATE_STARTEVENTRECODER
);
152 ~LogPrivateStartEventRecorderFunction() override
;
153 bool RunAsync() override
;
156 void OnEventRecorderStarted();
158 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartEventRecorderFunction
);
161 class LogPrivateStopEventRecorderFunction
: public AsyncExtensionFunction
{
163 LogPrivateStopEventRecorderFunction();
164 DECLARE_EXTENSION_FUNCTION("logPrivate.stopEventRecorder",
165 LOGPRIVATE_STOPEVENTRECODER
);
168 ~LogPrivateStopEventRecorderFunction() override
;
170 // AsyncExtensionFunction overrides.
171 bool RunAsync() override
;
174 void OnEventRecorderStopped();
176 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopEventRecorderFunction
);
179 class LogPrivateDumpLogsFunction
: public AsyncExtensionFunction
{
181 LogPrivateDumpLogsFunction();
182 DECLARE_EXTENSION_FUNCTION("logPrivate.dumpLogs", LOGPRIVATE_DUMPLOGS
);
185 ~LogPrivateDumpLogsFunction() override
;
187 // AsyncExtensionFunction overrides.
188 bool RunAsync() override
;
191 // Callback for DebugLogWriter::StoreLogs() call.
192 void OnStoreLogsCompleted(const base::FilePath
& log_path
, bool succeeded
);
193 // Callback for LogPrivateAPI::StopAllWatches() call.
194 void OnStopAllWatches();
195 DISALLOW_COPY_AND_ASSIGN(LogPrivateDumpLogsFunction
);
198 } // namespace extensions
200 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_