Chromium Code Reviews| Index: components/password_manager/content/browser/password_visibility_service_factory.cc |
| diff --git a/components/password_manager/content/browser/password_visibility_service_factory.cc b/components/password_manager/content/browser/password_visibility_service_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..02bb8fd415247a787f46cc3cad39fbbf456c255e |
| --- /dev/null |
| +++ b/components/password_manager/content/browser/password_visibility_service_factory.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/password_manager/content/browser/password_visibility_service_factory.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/stl_util.h" |
| +#include "components/password_manager/content/browser/password_visibility_service.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace password_manager { |
| + |
| +namespace { |
| + |
| +const char kPasswordVisibilityServiceFactoryWebContentsUserDataKey[] = |
| + "web_contents_password_visibility_service_factory"; |
| + |
| +} // namespace |
| + |
| +void PasswordVisibilityServiceFactory::CreateForWebContents( |
| + content::WebContents* web_contents) { |
| + if (FromWebContents(web_contents)) |
| + return; |
| + |
| + auto new_factory = |
| + base::WrapUnique(new PasswordVisibilityServiceFactory(web_contents)); |
|
vabr (Chromium)
2016/10/11 16:52:45
Please use
base::MakeUnique<PasswordVisibilityServ
estark
2016/10/11 23:45:14
No longer applicable, but MakeUnique doesn't work
vabr (Chromium)
2016/10/12 09:45:22
Good point, you are correct. Thanks for mentioning
|
| + const std::vector<content::RenderFrameHost*> frames = |
| + web_contents->GetAllFrames(); |
| + for (content::RenderFrameHost* frame : frames) { |
| + if (frame->IsRenderFrameLive()) |
| + new_factory->RenderFrameCreated(frame); |
| + } |
| + |
| + web_contents->SetUserData( |
| + kPasswordVisibilityServiceFactoryWebContentsUserDataKey, |
| + new_factory.release()); |
| +} |
| + |
| +PasswordVisibilityServiceFactory::PasswordVisibilityServiceFactory( |
| + content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents) {} |
| + |
| +PasswordVisibilityServiceFactory::~PasswordVisibilityServiceFactory() {} |
| + |
| +// static |
| +PasswordVisibilityServiceFactory* |
| +PasswordVisibilityServiceFactory::FromWebContents( |
| + content::WebContents* contents) { |
| + return static_cast<PasswordVisibilityServiceFactory*>(contents->GetUserData( |
| + kPasswordVisibilityServiceFactoryWebContentsUserDataKey)); |
| +} |
| + |
| +// static |
| +void PasswordVisibilityServiceFactory::BindSensitiveInputVisibilityService( |
| + content::RenderFrameHost* render_frame_host, |
| + blink::mojom::SensitiveInputVisibilityServiceRequest request) { |
| + content::WebContents* web_contents = |
| + content::WebContents::FromRenderFrameHost(render_frame_host); |
| + if (!web_contents) |
| + return; |
| + |
| + PasswordVisibilityServiceFactory::CreateForWebContents(web_contents); |
| + PasswordVisibilityServiceFactory* factory = |
| + PasswordVisibilityServiceFactory::FromWebContents(web_contents); |
| + // We try to bind to the service, but if service is not ready for now or |
| + // totally |
|
vabr (Chromium)
2016/10/11 16:52:45
nit: Please re-indent the comment to use all the h
estark
2016/10/11 23:45:14
no longer applicable
|
| + // not available for this render frame host, the request will be just dropped. |
| + // This would cause the message pipe to be closed, which will raise a |
| + // connection error on the peer side. |
| + if (!factory) |
| + return; |
| + |
| + PasswordVisibilityService* service = |
| + factory->GetServiceForFrame(render_frame_host); |
| + if (service) |
| + service->BindRequest(std::move(request)); |
| +} |
| + |
| +PasswordVisibilityService* PasswordVisibilityServiceFactory::GetServiceForFrame( |
| + content::RenderFrameHost* render_frame_host) { |
| + auto mapping = frame_service_map_.find(render_frame_host); |
| + return mapping == frame_service_map_.end() ? nullptr : mapping->second.get(); |
| +} |
| + |
| +void PasswordVisibilityServiceFactory::RenderFrameCreated( |
| + content::RenderFrameHost* render_frame_host) { |
| + auto insertion_result = |
| + frame_service_map_.insert(std::make_pair(render_frame_host, nullptr)); |
| + // This is called twice for the main frame. |
| + if (insertion_result.second) { // This was the first time. |
| + insertion_result.first->second = |
| + base::MakeUnique<PasswordVisibilityService>(); |
| + } |
| +} |
| + |
| +void PasswordVisibilityServiceFactory::RenderFrameDeleted( |
| + content::RenderFrameHost* render_frame_host) { |
| + frame_service_map_.erase(render_frame_host); |
| +} |
| + |
| +} // namespace password_manager |