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/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/extension_registry_observer.h"
19 #include "net/base/net_log.h"
25 namespace extensions {
26 class ExtensionRegistry;
28 class LogPrivateAPI : public BrowserContextKeyedAPI,
29 public ExtensionRegistryObserver,
30 public net::NetLog::ThreadSafeObserver {
32 // Convenience method to get the LogPrivateAPI for a profile.
33 static LogPrivateAPI* Get(content::BrowserContext* context);
35 explicit LogPrivateAPI(content::BrowserContext* context);
36 virtual ~LogPrivateAPI();
38 void StartNetInternalsWatch(const std::string& extension_id);
39 void StopNetInternalsWatch(const std::string& extension_id);
41 // BrowserContextKeyedAPI implementation.
42 static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance();
45 friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>;
47 // ExtensionRegistryObserver implementation.
48 virtual void OnExtensionUnloaded(
49 content::BrowserContext* browser_context,
50 const Extension* extension,
51 UnloadedExtensionInfo::Reason reason) OVERRIDE;
53 // ChromeNetLog::ThreadSafeObserver implementation:
54 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE;
56 void PostPendingEntries();
57 void AddEntriesOnUI(scoped_ptr<base::ListValue> value);
59 void MaybeStartNetInternalLogging();
60 void MaybeStopNetInternalLogging();
61 void StopNetInternalLogging();
63 // BrowserContextKeyedAPI implementation.
64 static const char* service_name() {
65 return "LogPrivateAPI";
67 static const bool kServiceIsNULLWhileTesting = true;
68 static const bool kServiceRedirectedInIncognito = true;
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_;
75 // Listen to extension unloaded notifications.
76 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
77 extension_registry_observer_;
80 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction {
82 LogPrivateGetHistoricalFunction();
83 DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical",
84 LOGPRIVATE_GETHISTORICAL);
87 virtual ~LogPrivateGetHistoricalFunction();
88 virtual bool RunAsync() OVERRIDE;
91 void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info);
93 scoped_ptr<FilterHandler> filter_handler_;
95 DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction);
98 class LogPrivateStartNetInternalsWatchFunction
99 : public ChromeSyncExtensionFunction {
101 LogPrivateStartNetInternalsWatchFunction();
102 DECLARE_EXTENSION_FUNCTION("logPrivate.startNetInternalsWatch",
103 LOGPRIVATE_STARTNETINTERNALSWATCH);
106 virtual ~LogPrivateStartNetInternalsWatchFunction();
107 virtual bool RunSync() OVERRIDE;
110 DISALLOW_COPY_AND_ASSIGN(LogPrivateStartNetInternalsWatchFunction);
113 class LogPrivateStopNetInternalsWatchFunction
114 : public ChromeSyncExtensionFunction {
116 LogPrivateStopNetInternalsWatchFunction();
117 DECLARE_EXTENSION_FUNCTION("logPrivate.stopNetInternalsWatch",
118 LOGPRIVATE_STOPNETINTERNALSWATCH);
121 virtual ~LogPrivateStopNetInternalsWatchFunction();
122 virtual bool RunSync() OVERRIDE;
125 DISALLOW_COPY_AND_ASSIGN(LogPrivateStopNetInternalsWatchFunction);
128 } // namespace extensions
130 #endif // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_