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