| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/log_private/log_private_api.h" | 5 #include "chrome/browser/extensions/api/log_private/log_private_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | |
| 17 #include "chrome/browser/extensions/api/log_private/filter_handler.h" | 16 #include "chrome/browser/extensions/api/log_private/filter_handler.h" |
| 18 #include "chrome/browser/extensions/api/log_private/log_parser.h" | 17 #include "chrome/browser/extensions/api/log_private/log_parser.h" |
| 19 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" | 18 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" |
| 20 #include "chrome/browser/feedback/system_logs/scrubbed_system_logs_fetcher.h" | 19 #include "chrome/browser/feedback/system_logs/scrubbed_system_logs_fetcher.h" |
| 21 #include "chrome/browser/io_thread.h" | 20 #include "chrome/browser/io_thread.h" |
| 22 #include "chrome/browser/net/chrome_net_log.h" | 21 #include "chrome/browser/net/chrome_net_log.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/common/extensions/api/log_private.h" | 23 #include "chrome/common/extensions/api/log_private.h" |
| 25 #include "content/public/browser/notification_details.h" | |
| 26 #include "content/public/browser/notification_source.h" | |
| 27 #include "extensions/browser/event_router.h" | 24 #include "extensions/browser/event_router.h" |
| 28 #include "extensions/browser/extension_function.h" | 25 #include "extensions/browser/extension_function.h" |
| 26 #include "extensions/browser/extension_registry.h" |
| 29 | 27 |
| 30 using content::BrowserThread; | 28 using content::BrowserThread; |
| 31 | 29 |
| 32 namespace events { | 30 namespace events { |
| 33 const char kOnAddNetInternalsEntries[] = "logPrivate.onAddNetInternalsEntries"; | 31 const char kOnAddNetInternalsEntries[] = "logPrivate.onAddNetInternalsEntries"; |
| 34 } // namespace events | 32 } // namespace events |
| 35 | 33 |
| 36 namespace extensions { | 34 namespace extensions { |
| 37 namespace { | 35 namespace { |
| 38 | 36 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 64 } | 62 } |
| 65 | 63 |
| 66 } // namespace | 64 } // namespace |
| 67 | 65 |
| 68 // static | 66 // static |
| 69 LogPrivateAPI* LogPrivateAPI::Get(content::BrowserContext* context) { | 67 LogPrivateAPI* LogPrivateAPI::Get(content::BrowserContext* context) { |
| 70 return GetFactoryInstance()->Get(context); | 68 return GetFactoryInstance()->Get(context); |
| 71 } | 69 } |
| 72 | 70 |
| 73 LogPrivateAPI::LogPrivateAPI(content::BrowserContext* context) | 71 LogPrivateAPI::LogPrivateAPI(content::BrowserContext* context) |
| 74 : profile_(Profile::FromBrowserContext(context)), | 72 : browser_context_(context), |
| 75 logging_net_internals_(false) { | 73 logging_net_internals_(false), |
| 76 registrar_.Add(this, | 74 extension_registry_observer_(this) { |
| 77 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 75 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 78 content::Source<Profile>(profile_)); | |
| 79 } | 76 } |
| 80 | 77 |
| 81 LogPrivateAPI::~LogPrivateAPI() { | 78 LogPrivateAPI::~LogPrivateAPI() { |
| 82 } | 79 } |
| 83 | 80 |
| 84 void LogPrivateAPI::StartNetInternalsWatch(const std::string& extension_id) { | 81 void LogPrivateAPI::StartNetInternalsWatch(const std::string& extension_id) { |
| 85 net_internal_watches_.insert(extension_id); | 82 net_internal_watches_.insert(extension_id); |
| 86 BrowserThread::PostTask( | 83 BrowserThread::PostTask( |
| 87 BrowserThread::IO, FROM_HERE, | 84 BrowserThread::IO, FROM_HERE, |
| 88 base::Bind(&LogPrivateAPI::MaybeStartNetInternalLogging, | 85 base::Bind(&LogPrivateAPI::MaybeStartNetInternalLogging, |
| 89 base::Unretained(this))); | 86 base::Unretained(this))); |
| 90 } | 87 } |
| 91 | 88 |
| 92 void LogPrivateAPI::StopNetInternalsWatch(const std::string& extension_id) { | 89 void LogPrivateAPI::StopNetInternalsWatch(const std::string& extension_id) { |
| 93 net_internal_watches_.erase(extension_id); | 90 net_internal_watches_.erase(extension_id); |
| 94 MaybeStopNetInternalLogging(); | 91 MaybeStopNetInternalLogging(); |
| 95 } | 92 } |
| 96 | 93 |
| 97 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> > | 94 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> > |
| 98 g_factory = LAZY_INSTANCE_INITIALIZER; | 95 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 99 | 96 |
| 100 // static | 97 // static |
| 101 BrowserContextKeyedAPIFactory<LogPrivateAPI>* | 98 BrowserContextKeyedAPIFactory<LogPrivateAPI>* |
| 102 LogPrivateAPI::GetFactoryInstance() { | 99 LogPrivateAPI::GetFactoryInstance() { |
| 103 return &g_factory.Get(); | 100 return g_factory.Pointer(); |
| 104 } | 101 } |
| 105 | 102 |
| 106 void LogPrivateAPI::OnAddEntry(const net::NetLog::Entry& entry) { | 103 void LogPrivateAPI::OnAddEntry(const net::NetLog::Entry& entry) { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 108 if (!pending_entries_.get()) { | 105 if (!pending_entries_.get()) { |
| 109 pending_entries_.reset(new base::ListValue()); | 106 pending_entries_.reset(new base::ListValue()); |
| 110 BrowserThread::PostDelayedTask( | 107 BrowserThread::PostDelayedTask( |
| 111 BrowserThread::IO, FROM_HERE, | 108 BrowserThread::IO, FROM_HERE, |
| 112 base::Bind(&LogPrivateAPI::PostPendingEntries, base::Unretained(this)), | 109 base::Bind(&LogPrivateAPI::PostPendingEntries, base::Unretained(this)), |
| 113 base::TimeDelta::FromMilliseconds(kNetLogEventDelayMilliseconds)); | 110 base::TimeDelta::FromMilliseconds(kNetLogEventDelayMilliseconds)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 126 void LogPrivateAPI::AddEntriesOnUI(scoped_ptr<base::ListValue> value) { | 123 void LogPrivateAPI::AddEntriesOnUI(scoped_ptr<base::ListValue> value) { |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 128 | 125 |
| 129 for (std::set<std::string>::iterator ix = net_internal_watches_.begin(); | 126 for (std::set<std::string>::iterator ix = net_internal_watches_.begin(); |
| 130 ix != net_internal_watches_.end(); ++ix) { | 127 ix != net_internal_watches_.end(); ++ix) { |
| 131 // Create the event's arguments value. | 128 // Create the event's arguments value. |
| 132 scoped_ptr<base::ListValue> event_args(new base::ListValue()); | 129 scoped_ptr<base::ListValue> event_args(new base::ListValue()); |
| 133 event_args->Append(value->DeepCopy()); | 130 event_args->Append(value->DeepCopy()); |
| 134 scoped_ptr<Event> event(new Event(events::kOnAddNetInternalsEntries, | 131 scoped_ptr<Event> event(new Event(events::kOnAddNetInternalsEntries, |
| 135 event_args.Pass())); | 132 event_args.Pass())); |
| 136 EventRouter::Get(profile_)->DispatchEventToExtension(*ix, event.Pass()); | 133 EventRouter::Get(browser_context_) |
| 134 ->DispatchEventToExtension(*ix, event.Pass()); |
| 137 } | 135 } |
| 138 } | 136 } |
| 139 | 137 |
| 140 void LogPrivateAPI::MaybeStartNetInternalLogging() { | 138 void LogPrivateAPI::MaybeStartNetInternalLogging() { |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 142 if (!logging_net_internals_) { | 140 if (!logging_net_internals_) { |
| 143 g_browser_process->io_thread()->net_log()->AddThreadSafeObserver( | 141 g_browser_process->io_thread()->net_log()->AddThreadSafeObserver( |
| 144 this, net::NetLog::LOG_ALL_BUT_BYTES); | 142 this, net::NetLog::LOG_ALL_BUT_BYTES); |
| 145 logging_net_internals_ = true; | 143 logging_net_internals_ = true; |
| 146 } | 144 } |
| 147 } | 145 } |
| 148 | 146 |
| 149 void LogPrivateAPI::MaybeStopNetInternalLogging() { | 147 void LogPrivateAPI::MaybeStopNetInternalLogging() { |
| 150 if (net_internal_watches_.empty()) { | 148 if (net_internal_watches_.empty()) { |
| 151 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
| 152 BrowserThread::IO, FROM_HERE, | 150 BrowserThread::IO, FROM_HERE, |
| 153 base::Bind(&LogPrivateAPI:: StopNetInternalLogging, | 151 base::Bind(&LogPrivateAPI:: StopNetInternalLogging, |
| 154 base::Unretained(this))); | 152 base::Unretained(this))); |
| 155 } | 153 } |
| 156 } | 154 } |
| 157 | 155 |
| 158 void LogPrivateAPI::StopNetInternalLogging() { | 156 void LogPrivateAPI::StopNetInternalLogging() { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 if (net_log() && logging_net_internals_) { | 158 if (net_log() && logging_net_internals_) { |
| 161 net_log()->RemoveThreadSafeObserver(this); | 159 net_log()->RemoveThreadSafeObserver(this); |
| 162 logging_net_internals_ = false; | 160 logging_net_internals_ = false; |
| 163 } | 161 } |
| 164 } | 162 } |
| 165 | 163 |
| 166 void LogPrivateAPI::Observe(int type, | 164 void LogPrivateAPI::OnExtensionUnloaded( |
| 167 const content::NotificationSource& source, | 165 content::BrowserContext* browser_context, |
| 168 const content::NotificationDetails& details) { | 166 const Extension* extension, |
| 169 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | 167 UnloadedExtensionInfo::Reason reason) { |
| 170 const Extension* extension = | 168 StopNetInternalsWatch(extension->id()); |
| 171 content::Details<const UnloadedExtensionInfo>(details)->extension; | |
| 172 StopNetInternalsWatch(extension->id()); | |
| 173 } | |
| 174 } | 169 } |
| 175 | 170 |
| 176 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { | 171 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { |
| 177 } | 172 } |
| 178 | 173 |
| 179 LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() { | 174 LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() { |
| 180 } | 175 } |
| 181 | 176 |
| 182 bool LogPrivateGetHistoricalFunction::RunImpl() { | 177 bool LogPrivateGetHistoricalFunction::RunImpl() { |
| 183 // Get parameters | 178 // Get parameters |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 LogPrivateStopNetInternalsWatchFunction:: | 228 LogPrivateStopNetInternalsWatchFunction:: |
| 234 ~LogPrivateStopNetInternalsWatchFunction() { | 229 ~LogPrivateStopNetInternalsWatchFunction() { |
| 235 } | 230 } |
| 236 | 231 |
| 237 bool LogPrivateStopNetInternalsWatchFunction::RunImpl() { | 232 bool LogPrivateStopNetInternalsWatchFunction::RunImpl() { |
| 238 LogPrivateAPI::Get(GetProfile())->StopNetInternalsWatch(extension_id()); | 233 LogPrivateAPI::Get(GetProfile())->StopNetInternalsWatch(extension_id()); |
| 239 return true; | 234 return true; |
| 240 } | 235 } |
| 241 | 236 |
| 242 } // namespace extensions | 237 } // namespace extensions |
| OLD | NEW |