OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "components/password_manager/core/browser/log_manager.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "components/password_manager/core/browser/log_router.h" |
| 9 |
| 10 namespace password_manager { |
| 11 |
| 12 namespace { |
| 13 |
| 14 class LogManagerImpl : public LogManager { |
| 15 public: |
| 16 LogManagerImpl(LogRouter* log_router, base::Closure notification_callback); |
| 17 |
| 18 ~LogManagerImpl() override; |
| 19 |
| 20 // LogManager |
| 21 void OnLogRouterAvailabilityChanged(bool router_can_be_used) override; |
| 22 void SetSuspended(bool suspended) override; |
| 23 void LogSavePasswordProgress(const std::string& text) const override; |
| 24 bool IsLoggingActive() const override; |
| 25 |
| 26 private: |
| 27 // A LogRouter instance obtained on construction. May be null. |
| 28 LogRouter* const log_router_; |
| 29 |
| 30 // True if |this| is registered with some LogRouter which can accept logs. |
| 31 bool can_use_log_router_; |
| 32 |
| 33 bool is_suspended_ = false; |
| 34 |
| 35 // Called every time the logging activity status changes. |
| 36 base::Closure notification_callback_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(LogManagerImpl); |
| 39 }; |
| 40 |
| 41 LogManagerImpl::LogManagerImpl(LogRouter* log_router, |
| 42 base::Closure notification_callback) |
| 43 : log_router_(log_router), |
| 44 can_use_log_router_(log_router_ && log_router_->RegisterManager(this)), |
| 45 notification_callback_(notification_callback) {} |
| 46 |
| 47 LogManagerImpl::~LogManagerImpl() { |
| 48 if (log_router_) |
| 49 log_router_->UnregisterManager(this); |
| 50 } |
| 51 |
| 52 void LogManagerImpl::OnLogRouterAvailabilityChanged(bool router_can_be_used) { |
| 53 DCHECK(log_router_); // |log_router_| should be calling this method. |
| 54 if (can_use_log_router_ == router_can_be_used) |
| 55 return; |
| 56 can_use_log_router_ = router_can_be_used; |
| 57 |
| 58 if (!notification_callback_.is_null()) |
| 59 notification_callback_.Run(); |
| 60 } |
| 61 |
| 62 void LogManagerImpl::SetSuspended(bool suspended) { |
| 63 is_suspended_ = suspended; |
| 64 } |
| 65 |
| 66 void LogManagerImpl::LogSavePasswordProgress(const std::string& text) const { |
| 67 if (!IsLoggingActive()) |
| 68 return; |
| 69 log_router_->ProcessLog(text); |
| 70 } |
| 71 |
| 72 bool LogManagerImpl::IsLoggingActive() const { |
| 73 return can_use_log_router_ && !is_suspended_; |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 // static |
| 79 scoped_ptr<LogManager> LogManager::Create(LogRouter* log_router, |
| 80 base::Closure notification_callback) { |
| 81 return make_scoped_ptr(new LogManagerImpl(log_router, notification_callback)); |
| 82 } |
| 83 |
| 84 } // namespace password_manager |
OLD | NEW |