- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_generation_manager.cc
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 #include "chrome/browser/password_manager/password_generation_manager.h"
6
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/password_manager/password_manager.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/profile_sync_service.h"
11 #include "chrome/browser/sync/profile_sync_service_factory.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/autofill/core/browser/autofill_field.h"
17 #include "components/autofill/core/browser/field_types.h"
18 #include "components/autofill/core/browser/form_structure.h"
19 #include "components/autofill/core/browser/password_generator.h"
20 #include "components/autofill/core/common/autofill_messages.h"
21 #include "components/autofill/core/common/form_data.h"
22 #include "components/autofill/core/common/password_form.h"
23 #include "components/user_prefs/pref_registry_syncable.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "ipc/ipc_message_macros.h"
28 #include "ui/gfx/rect.h"
29
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PasswordGenerationManager);
31
32 PasswordGenerationManager::PasswordGenerationManager(
33     content::WebContents* contents)
34     : content::WebContentsObserver(contents) {}
35
36 PasswordGenerationManager::~PasswordGenerationManager() {}
37
38 // static
39 void PasswordGenerationManager::RegisterProfilePrefs(
40     user_prefs::PrefRegistrySyncable* registry) {
41   registry->RegisterBooleanPref(
42       prefs::kPasswordGenerationEnabled,
43       true,
44       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
45 }
46
47 void PasswordGenerationManager::DetectAccountCreationForms(
48     const std::vector<autofill::FormStructure*>& forms) {
49   std::vector<autofill::FormData> account_creation_forms;
50   for (std::vector<autofill::FormStructure*>::const_iterator form_it =
51            forms.begin(); form_it != forms.end(); ++form_it) {
52     autofill::FormStructure* form = *form_it;
53     for (std::vector<autofill::AutofillField*>::const_iterator field_it =
54              form->begin(); field_it != form->end(); ++field_it) {
55       autofill::AutofillField* field = *field_it;
56       if (field->server_type() == autofill::ACCOUNT_CREATION_PASSWORD) {
57         account_creation_forms.push_back(form->ToFormData());
58         break;
59       }
60     }
61   }
62   if (!account_creation_forms.empty() && IsGenerationEnabled()) {
63     SendAccountCreationFormsToRenderer(web_contents()->GetRenderViewHost(),
64                                        account_creation_forms);
65   }
66 }
67
68 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) {
69   bool handled = true;
70   IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message)
71     IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordGenerationPopup,
72                         OnShowPasswordGenerationPopup)
73     IPC_MESSAGE_UNHANDLED(handled = false)
74   IPC_END_MESSAGE_MAP()
75
76   return handled;
77 }
78
79 // In order for password generation to be enabled, we need to make sure:
80 // (1) Password sync is enabled,
81 // (2) Password manager is enabled, and
82 // (3) Password generation preference check box is checked.
83 bool PasswordGenerationManager::IsGenerationEnabled() const {
84   if (!web_contents())
85     return false;
86
87   Profile* profile = Profile::FromBrowserContext(
88       web_contents()->GetBrowserContext());
89
90   if (!PasswordManager::FromWebContents(web_contents())->IsSavingEnabled()) {
91     DVLOG(2) << "Generation disabled because password saving is disabled";
92     return false;
93   }
94
95   bool password_sync_enabled = false;
96   ProfileSyncService* sync_service =
97       ProfileSyncServiceFactory::GetForProfile(profile);
98   if (sync_service) {
99     syncer::ModelTypeSet sync_set = sync_service->GetActiveDataTypes();
100     password_sync_enabled = (sync_service->HasSyncSetupCompleted() &&
101                              sync_set.Has(syncer::PASSWORDS));
102   }
103   if (!password_sync_enabled) {
104     DVLOG(2) << "Generation disabled because passwords are not being synced";
105     return false;
106   }
107
108   if (!profile->GetPrefs()->GetBoolean(prefs::kPasswordGenerationEnabled)) {
109     DVLOG(2) << "Generation disabled by user";
110     return false;
111   }
112
113   return true;
114 }
115
116 void PasswordGenerationManager::SendAccountCreationFormsToRenderer(
117     content::RenderViewHost* host,
118     const std::vector<autofill::FormData>& forms) {
119   host->Send(new AutofillMsg_AccountCreationFormsDetected(
120       host->GetRoutingID(), forms));
121 }
122
123 void PasswordGenerationManager::OnShowPasswordGenerationPopup(
124     const gfx::Rect& bounds,
125     int max_length,
126     const autofill::PasswordForm& form) {
127 #if defined(OS_ANDROID)
128   NOTIMPLEMENTED();
129 #else
130   password_generator_.reset(new autofill::PasswordGenerator(max_length));
131   Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
132   browser->window()->ShowPasswordGenerationBubble(bounds,
133                                                   form,
134                                                   password_generator_.get());
135 #endif  // #if defined(OS_ANDROID)
136 }