• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "chrome/browser/ui/webui/password_manager_internals/password_manager_internals_ui.h"
6 
7 #include <algorithm>
8 #include <set>
9 
10 #include "base/values.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/url_constants.h"
13 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
14 #include "components/password_manager/core/browser/password_manager_internals_service.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_data_source.h"
17 #include "grit/password_manager_internals_resources.h"
18 #include "net/base/escape.h"
19 
20 using password_manager::PasswordManagerInternalsService;
21 using password_manager::PasswordManagerInternalsServiceFactory;
22 
23 namespace {
24 
CreatePasswordManagerInternalsHTMLSource()25 content::WebUIDataSource* CreatePasswordManagerInternalsHTMLSource() {
26   content::WebUIDataSource* source = content::WebUIDataSource::Create(
27       chrome::kChromeUIPasswordManagerInternalsHost);
28 
29   source->AddResourcePath(
30       "password_manager_internals.js",
31       IDR_PASSWORD_MANAGER_INTERNALS_PASSWORD_MANAGER_INTERNALS_JS);
32   source->SetDefaultResource(
33       IDR_PASSWORD_MANAGER_INTERNALS_PASSWORD_MANAGER_INTERNALS_HTML);
34   return source;
35 }
36 
37 }  // namespace
38 
PasswordManagerInternalsUI(content::WebUI * web_ui)39 PasswordManagerInternalsUI::PasswordManagerInternalsUI(content::WebUI* web_ui)
40     : WebUIController(web_ui),
41       WebContentsObserver(web_ui->GetWebContents()),
42       registered_with_logging_service_(false) {
43   // Set up the chrome://password-manager-internals/ source.
44   content::WebUIDataSource::Add(Profile::FromWebUI(web_ui),
45                                 CreatePasswordManagerInternalsHTMLSource());
46 }
47 
~PasswordManagerInternalsUI()48 PasswordManagerInternalsUI::~PasswordManagerInternalsUI() {
49   UnregisterFromLoggingService();
50 }
51 
DidStartLoading(content::RenderViewHost *)52 void PasswordManagerInternalsUI::DidStartLoading(
53     content::RenderViewHost* /* render_view_host */) {
54   UnregisterFromLoggingService();
55 }
56 
DidStopLoading(content::RenderViewHost *)57 void PasswordManagerInternalsUI::DidStopLoading(
58     content::RenderViewHost* /* render_view_host */) {
59   PasswordManagerInternalsService* service =
60       PasswordManagerInternalsServiceFactory::GetForBrowserContext(
61           Profile::FromWebUI(web_ui()));
62   if (service) {
63     registered_with_logging_service_ = true;
64     std::string past_logs(service->RegisterReceiver(this));
65     LogSavePasswordProgress(past_logs);
66   }
67 }
68 
LogSavePasswordProgress(const std::string & text)69 void PasswordManagerInternalsUI::LogSavePasswordProgress(
70     const std::string& text) {
71   if (!registered_with_logging_service_)
72     return;
73   std::string no_quotes(text);
74   std::replace(no_quotes.begin(), no_quotes.end(), '"', ' ');
75   base::StringValue text_string_value(net::EscapeForHTML(no_quotes));
76   web_ui()->CallJavascriptFunction("addSavePasswordProgressLog",
77                                    text_string_value);
78 }
79 
UnregisterFromLoggingService()80 void PasswordManagerInternalsUI::UnregisterFromLoggingService() {
81   if (!registered_with_logging_service_)
82     return;
83   PasswordManagerInternalsService* service =
84       PasswordManagerInternalsServiceFactory::GetForBrowserContext(
85           Profile::FromWebUI(web_ui()));
86   if (service)
87     service->UnregisterReceiver(this);
88 }
89