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/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/browser_context_keyed_api_factory.h" 18 #include "extensions/browser/extension_registry_observer.h" 19 #include "net/base/net_log.h" 20 21 namespace content { 22 class BrowserContext; 23 } 24 25 namespace extensions { 26 class ExtensionRegistry; 27 28 class LogPrivateAPI : public BrowserContextKeyedAPI, 29 public ExtensionRegistryObserver, 30 public net::NetLog::ThreadSafeObserver { 31 public: 32 // Convenience method to get the LogPrivateAPI for a profile. 33 static LogPrivateAPI* Get(content::BrowserContext* context); 34 35 explicit LogPrivateAPI(content::BrowserContext* context); 36 virtual ~LogPrivateAPI(); 37 38 void StartNetInternalsWatch(const std::string& extension_id); 39 void StopNetInternalsWatch(const std::string& extension_id); 40 41 // BrowserContextKeyedAPI implementation. 42 static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance(); 43 44 private: 45 friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>; 46 47 // ExtensionRegistryObserver implementation. 48 virtual void OnExtensionUnloaded( 49 content::BrowserContext* browser_context, 50 const Extension* extension, 51 UnloadedExtensionInfo::Reason reason) OVERRIDE; 52 53 // ChromeNetLog::ThreadSafeObserver implementation: 54 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; 55 56 void PostPendingEntries(); 57 void AddEntriesOnUI(scoped_ptr<base::ListValue> value); 58 59 void MaybeStartNetInternalLogging(); 60 void MaybeStopNetInternalLogging(); 61 void StopNetInternalLogging(); 62 63 // BrowserContextKeyedAPI implementation. 64 static const char* service_name() { 65 return "LogPrivateAPI"; 66 } 67 static const bool kServiceIsNULLWhileTesting = true; 68 static const bool kServiceRedirectedInIncognito = true; 69 70 content::BrowserContext* const browser_context_; 71 bool logging_net_internals_; 72 std::set<std::string> net_internal_watches_; 73 scoped_ptr<base::ListValue> pending_entries_; 74 75 // Listen to extension unloaded notifications. 76 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 77 extension_registry_observer_; 78 }; 79 80 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction { 81 public: 82 LogPrivateGetHistoricalFunction(); 83 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical", 84 LOGPRIVATE_GETHISTORICAL); 85 86 protected: 87 virtual ~LogPrivateGetHistoricalFunction(); 88 virtual bool RunAsync() OVERRIDE; 89 90 private: 91 void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info); 92 93 scoped_ptr<FilterHandler> filter_handler_; 94 95 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction); 96 }; 97 98 class LogPrivateStartNetInternalsWatchFunction 99 : public ChromeSyncExtensionFunction { 100 public: 101 LogPrivateStartNetInternalsWatchFunction(); 102 DECLARE_EXTENSION_FUNCTION("logPrivate.startNetInternalsWatch", 103 LOGPRIVATE_STARTNETINTERNALSWATCH); 104 105 protected: 106 virtual ~LogPrivateStartNetInternalsWatchFunction(); 107 virtual bool RunSync() OVERRIDE; 108 109 private: 110 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartNetInternalsWatchFunction); 111 }; 112 113 class LogPrivateStopNetInternalsWatchFunction 114 : public ChromeSyncExtensionFunction { 115 public: 116 LogPrivateStopNetInternalsWatchFunction(); 117 DECLARE_EXTENSION_FUNCTION("logPrivate.stopNetInternalsWatch", 118 LOGPRIVATE_STOPNETINTERNALSWATCH); 119 120 protected: 121 virtual ~LogPrivateStopNetInternalsWatchFunction(); 122 virtual bool RunSync() OVERRIDE; 123 124 private: 125 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopNetInternalsWatchFunction); 126 }; 127 128 } // namespace extensions 129 130 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_ 131