Home | History | Annotate | Download | only in log_private
      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/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"
     23 
     24 class IOThread;
     25 
     26 namespace content {
     27 class BrowserContext;
     28 }
     29 
     30 namespace extensions {
     31 class ExtensionRegistry;
     32 
     33 // Tracked log files.
     34 class FileResource : public ApiResource {
     35  public:
     36   FileResource(const std::string& owner_extension_id,
     37                const base::FilePath& path);
     38   virtual ~FileResource();
     39 
     40   // ApiResource overrides.
     41   virtual bool IsPersistent() const OVERRIDE;
     42 
     43   static const char kSequenceToken[];
     44   static const base::SequencedWorkerPool::WorkerShutdown kShutdownBehavior =
     45       base::SequencedWorkerPool::BLOCK_SHUTDOWN;
     46 
     47  private:
     48   base::FilePath path_;
     49 
     50   DISALLOW_COPY_AND_ASSIGN(FileResource);
     51 };
     52 
     53 class LogPrivateAPI : public BrowserContextKeyedAPI,
     54                       public ExtensionRegistryObserver,
     55                       public net::NetLog::ThreadSafeObserver {
     56  public:
     57   // Convenience method to get the LogPrivateAPI for a profile.
     58   static LogPrivateAPI* Get(content::BrowserContext* context);
     59 
     60   explicit LogPrivateAPI(content::BrowserContext* context);
     61   virtual ~LogPrivateAPI();
     62 
     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);
     72 
     73   // BrowserContextKeyedAPI implementation.
     74   static BrowserContextKeyedAPIFactory<LogPrivateAPI>* GetFactoryInstance();
     75 
     76  private:
     77   friend class BrowserContextKeyedAPIFactory<LogPrivateAPI>;
     78 
     79   void Initialize();
     80   // ExtensionRegistryObserver implementation.
     81   virtual void OnExtensionUnloaded(
     82       content::BrowserContext* browser_context,
     83       const Extension* extension,
     84       UnloadedExtensionInfo::Reason reason) OVERRIDE;
     85 
     86   // ChromeNetLog::ThreadSafeObserver implementation:
     87   virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE;
     88 
     89   void PostPendingEntries();
     90   void AddEntriesOnUI(scoped_ptr<base::ListValue> value);
     91 
     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);
     96 
     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,
    101                                     IOThread* io_thread,
    102                                     api::log_private::EventSink event_sink);
    103   void MaybeStopNetInternalLogging(const base::Closure& closure);
    104   void StopNetInternalLogging();
    105 
    106   // BrowserContextKeyedAPI implementation.
    107   static const char* service_name() {
    108     return "LogPrivateAPI";
    109   }
    110   static const bool kServiceIsNULLWhileTesting = true;
    111   static const bool kServiceRedirectedInIncognito = true;
    112 
    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> >
    123       log_file_resources_;
    124   bool initialized_;
    125 
    126   DISALLOW_COPY_AND_ASSIGN(LogPrivateAPI);
    127 };
    128 
    129 class LogPrivateGetHistoricalFunction : public AsyncExtensionFunction {
    130  public:
    131   LogPrivateGetHistoricalFunction();
    132   DECLARE_EXTENSION_FUNCTION("logPrivate.getHistorical",
    133                              LOGPRIVATE_GETHISTORICAL);
    134 
    135  protected:
    136   virtual ~LogPrivateGetHistoricalFunction();
    137   virtual bool RunAsync() OVERRIDE;
    138 
    139  private:
    140   void OnSystemLogsLoaded(scoped_ptr<system_logs::SystemLogsResponse> sys_info);
    141 
    142   scoped_ptr<FilterHandler> filter_handler_;
    143 
    144   DISALLOW_COPY_AND_ASSIGN(LogPrivateGetHistoricalFunction);
    145 };
    146 
    147 class LogPrivateStartEventRecorderFunction : public AsyncExtensionFunction {
    148  public:
    149   LogPrivateStartEventRecorderFunction();
    150   DECLARE_EXTENSION_FUNCTION("logPrivate.startEventRecorder",
    151                              LOGPRIVATE_STARTEVENTRECODER);
    152 
    153  protected:
    154   virtual ~LogPrivateStartEventRecorderFunction();
    155   virtual bool RunAsync() OVERRIDE;
    156 
    157  private:
    158   void OnEventRecorderStarted();
    159 
    160   DISALLOW_COPY_AND_ASSIGN(LogPrivateStartEventRecorderFunction);
    161 };
    162 
    163 class LogPrivateStopEventRecorderFunction : public AsyncExtensionFunction {
    164  public:
    165   LogPrivateStopEventRecorderFunction();
    166   DECLARE_EXTENSION_FUNCTION("logPrivate.stopEventRecorder",
    167                              LOGPRIVATE_STOPEVENTRECODER);
    168 
    169  protected:
    170   virtual ~LogPrivateStopEventRecorderFunction();
    171 
    172   // AsyncExtensionFunction overrides.
    173   virtual bool RunAsync() OVERRIDE;
    174 
    175  private:
    176   void OnEventRecorderStopped();
    177 
    178   DISALLOW_COPY_AND_ASSIGN(LogPrivateStopEventRecorderFunction);
    179 };
    180 
    181 class LogPrivateDumpLogsFunction : public AsyncExtensionFunction {
    182  public:
    183   LogPrivateDumpLogsFunction();
    184   DECLARE_EXTENSION_FUNCTION("logPrivate.dumpLogs", LOGPRIVATE_DUMPLOGS);
    185 
    186  protected:
    187   virtual ~LogPrivateDumpLogsFunction();
    188 
    189   // AsyncExtensionFunction overrides.
    190   virtual bool RunAsync() OVERRIDE;
    191 
    192  private:
    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);
    198 };
    199 
    200 }  // namespace extensions
    201 
    202 #endif  // CHROME_BROWSER_EXTENSIONS_API_LOG_PRIVATE_LOG_PRIVATE_API_H_
    203