Home | History | Annotate | Download | only in browser
      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 "components/autofill/content/browser/autofill_driver_impl.h"
      6 
      7 #include "base/command_line.h"
      8 #include "components/autofill/core/browser/autofill_external_delegate.h"
      9 #include "components/autofill/core/browser/autofill_manager.h"
     10 #include "components/autofill/core/browser/autofill_manager_delegate.h"
     11 #include "components/autofill/core/common/autofill_messages.h"
     12 #include "components/autofill/core/browser/form_structure.h"
     13 #include "components/autofill/core/common/autofill_switches.h"
     14 #include "content/public/browser/navigation_controller.h"
     15 #include "content/public/browser/navigation_details.h"
     16 #include "content/public/browser/notification_service.h"
     17 #include "content/public/browser/notification_source.h"
     18 #include "content/public/browser/notification_types.h"
     19 #include "content/public/browser/render_view_host.h"
     20 #include "content/public/browser/web_contents.h"
     21 #include "content/public/common/frame_navigate_params.h"
     22 #include "ipc/ipc_message_macros.h"
     23 
     24 namespace autofill {
     25 
     26 namespace {
     27 
     28 const char kAutofillDriverImplWebContentsUserDataKey[] =
     29     "web_contents_autofill_driver_impl";
     30 
     31 }  // namespace
     32 
     33 // static
     34 void AutofillDriverImpl::CreateForWebContentsAndDelegate(
     35     content::WebContents* contents,
     36     autofill::AutofillManagerDelegate* delegate,
     37     const std::string& app_locale,
     38     AutofillManager::AutofillDownloadManagerState enable_download_manager) {
     39   if (FromWebContents(contents))
     40     return;
     41 
     42   contents->SetUserData(kAutofillDriverImplWebContentsUserDataKey,
     43                         new AutofillDriverImpl(contents,
     44                                                delegate,
     45                                                app_locale,
     46                                                enable_download_manager));
     47   // Trigger the lazy creation of AutocheckoutWhitelistManagerService, and
     48   // schedule a fetch of the Autocheckout whitelist file if it's not already
     49   // loaded. This helps ensure that the whitelist will be available by the time
     50   // the user navigates to a form on which Autocheckout should be enabled.
     51   delegate->GetAutocheckoutWhitelistManager();
     52 }
     53 
     54 // static
     55 AutofillDriverImpl* AutofillDriverImpl::FromWebContents(
     56     content::WebContents* contents) {
     57   return static_cast<AutofillDriverImpl*>(
     58       contents->GetUserData(kAutofillDriverImplWebContentsUserDataKey));
     59 }
     60 
     61 AutofillDriverImpl::AutofillDriverImpl(
     62     content::WebContents* web_contents,
     63     autofill::AutofillManagerDelegate* delegate,
     64     const std::string& app_locale,
     65     AutofillManager::AutofillDownloadManagerState enable_download_manager)
     66     : content::WebContentsObserver(web_contents),
     67       autofill_manager_(new AutofillManager(
     68           this, delegate, app_locale, enable_download_manager)) {
     69   SetAutofillExternalDelegate(scoped_ptr<AutofillExternalDelegate>(
     70       new AutofillExternalDelegate(web_contents, autofill_manager_.get(),
     71                                    this)));
     72 
     73   registrar_.Add(this,
     74                  content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
     75                  content::Source<content::WebContents>(web_contents));
     76   registrar_.Add(
     77       this,
     78       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
     79       content::Source<content::NavigationController>(
     80           &(web_contents->GetController())));
     81 }
     82 
     83 AutofillDriverImpl::~AutofillDriverImpl() {}
     84 
     85 content::WebContents* AutofillDriverImpl::GetWebContents() {
     86   return web_contents();
     87 }
     88 
     89 bool AutofillDriverImpl::RendererIsAvailable() {
     90   return (web_contents()->GetRenderViewHost() != NULL);
     91 }
     92 
     93 void AutofillDriverImpl::SetRendererActionOnFormDataReception(
     94     RendererFormDataAction action) {
     95   if (!RendererIsAvailable())
     96     return;
     97 
     98   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
     99   switch(action) {
    100     case FORM_DATA_ACTION_PREVIEW:
    101       host->Send(new AutofillMsg_SetAutofillActionPreview(
    102           host->GetRoutingID()));
    103       return;
    104     case FORM_DATA_ACTION_FILL:
    105       host->Send(new AutofillMsg_SetAutofillActionFill(host->GetRoutingID()));
    106       return;
    107   }
    108 }
    109 
    110 void AutofillDriverImpl::SendFormDataToRenderer(int query_id,
    111                                                 const FormData& data) {
    112   if (!RendererIsAvailable())
    113     return;
    114   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
    115   host->Send(
    116       new AutofillMsg_FormDataFilled(host->GetRoutingID(), query_id, data));
    117 }
    118 
    119 void AutofillDriverImpl::SendAutofillTypePredictionsToRenderer(
    120     const std::vector<FormStructure*>& forms) {
    121   if (!CommandLine::ForCurrentProcess()->HasSwitch(
    122       switches::kShowAutofillTypePredictions))
    123     return;
    124 
    125   content::RenderViewHost* host = GetWebContents()->GetRenderViewHost();
    126   if (!host)
    127     return;
    128 
    129   std::vector<FormDataPredictions> type_predictions;
    130   FormStructure::GetFieldTypePredictions(forms, &type_predictions);
    131   host->Send(
    132       new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(),
    133                                                     type_predictions));
    134 }
    135 
    136 void AutofillDriverImpl::RendererShouldClearFilledForm() {
    137   if (!RendererIsAvailable())
    138     return;
    139   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
    140   host->Send(new AutofillMsg_ClearForm(host->GetRoutingID()));
    141 }
    142 
    143 void AutofillDriverImpl::RendererShouldClearPreviewedForm() {
    144   if (!RendererIsAvailable())
    145     return;
    146   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
    147   host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID()));
    148 }
    149 
    150 bool AutofillDriverImpl::OnMessageReceived(const IPC::Message& message) {
    151   bool handled = true;
    152   IPC_BEGIN_MESSAGE_MAP(AutofillDriverImpl, message)
    153     IPC_MESSAGE_FORWARD(AutofillHostMsg_FormsSeen, autofill_manager_.get(),
    154                         AutofillManager::OnFormsSeen)
    155     IPC_MESSAGE_FORWARD(AutofillHostMsg_FormSubmitted, autofill_manager_.get(),
    156                         AutofillManager::OnFormSubmitted)
    157     IPC_MESSAGE_FORWARD(AutofillHostMsg_TextFieldDidChange,
    158                         autofill_manager_.get(),
    159                         AutofillManager::OnTextFieldDidChange)
    160     IPC_MESSAGE_FORWARD(AutofillHostMsg_QueryFormFieldAutofill,
    161                         autofill_manager_.get(),
    162                         AutofillManager::OnQueryFormFieldAutofill)
    163     IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowAutofillDialog,
    164                         autofill_manager_.get(),
    165                         AutofillManager::OnShowAutofillDialog)
    166     IPC_MESSAGE_FORWARD(AutofillHostMsg_FillAutofillFormData,
    167                         autofill_manager_.get(),
    168                         AutofillManager::OnFillAutofillFormData)
    169     IPC_MESSAGE_FORWARD(AutofillHostMsg_DidPreviewAutofillFormData,
    170                         autofill_manager_.get(),
    171                         AutofillManager::OnDidPreviewAutofillFormData)
    172     IPC_MESSAGE_FORWARD(AutofillHostMsg_DidFillAutofillFormData,
    173                         autofill_manager_.get(),
    174                         AutofillManager::OnDidFillAutofillFormData)
    175     IPC_MESSAGE_FORWARD(AutofillHostMsg_DidEndTextFieldEditing,
    176                         autofill_manager_.get(),
    177                         AutofillManager::OnDidEndTextFieldEditing)
    178     IPC_MESSAGE_FORWARD(AutofillHostMsg_HideAutofillUI, autofill_manager_.get(),
    179                         AutofillManager::OnHideAutofillUI)
    180     IPC_MESSAGE_FORWARD(AutofillHostMsg_AddPasswordFormMapping,
    181                         autofill_manager_.get(),
    182                         AutofillManager::OnAddPasswordFormMapping)
    183     IPC_MESSAGE_FORWARD(AutofillHostMsg_ShowPasswordSuggestions,
    184                         autofill_manager_.get(),
    185                         AutofillManager::OnShowPasswordSuggestions)
    186     IPC_MESSAGE_FORWARD(AutofillHostMsg_SetDataList, autofill_manager_.get(),
    187                         AutofillManager::OnSetDataList)
    188     IPC_MESSAGE_FORWARD(AutofillHostMsg_RequestAutocomplete,
    189                         autofill_manager_.get(),
    190                         AutofillManager::OnRequestAutocomplete)
    191     IPC_MESSAGE_FORWARD(AutofillHostMsg_AutocheckoutPageCompleted,
    192                         autofill_manager_.get(),
    193                         AutofillManager::OnAutocheckoutPageCompleted)
    194     IPC_MESSAGE_FORWARD(AutofillHostMsg_MaybeShowAutocheckoutBubble,
    195                         autofill_manager_.get(),
    196                         AutofillManager::OnMaybeShowAutocheckoutBubble)
    197     IPC_MESSAGE_UNHANDLED(handled = false)
    198   IPC_END_MESSAGE_MAP()
    199   return handled;
    200 }
    201 
    202 void AutofillDriverImpl::DidNavigateMainFrame(
    203     const content::LoadCommittedDetails& details,
    204     const content::FrameNavigateParams& params) {
    205   if (details.is_navigation_to_different_page())
    206     autofill_manager_->Reset();
    207 }
    208 
    209 void AutofillDriverImpl::SetAutofillExternalDelegate(
    210     scoped_ptr<AutofillExternalDelegate> delegate) {
    211   autofill_external_delegate_ = delegate.Pass();
    212   autofill_manager_->SetExternalDelegate(autofill_external_delegate_.get());
    213 }
    214 
    215 void AutofillDriverImpl::SetAutofillManager(
    216     scoped_ptr<AutofillManager> manager) {
    217   autofill_manager_ = manager.Pass();
    218   autofill_manager_->SetExternalDelegate(autofill_external_delegate_.get());
    219 }
    220 
    221 void AutofillDriverImpl::Observe(
    222     int type,
    223     const content::NotificationSource& source,
    224     const content::NotificationDetails& details) {
    225   if (type == content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED) {
    226     if (!*content::Details<bool>(details).ptr())
    227       autofill_manager_->delegate()->HideAutofillPopup();
    228   } else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
    229     autofill_manager_->delegate()->HideAutofillPopup();
    230   } else {
    231     NOTREACHED();
    232   }
    233 }
    234 
    235 }  // namespace autofill
    236