1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is the Metrics extension.
18
* The Initial Developer of the Original Code is Google Inc.
19
* Portions created by the Initial Developer are Copyright (C) 2006
20
* the Initial Developer. All Rights Reserved.
23
* Brian Ryner <bryner@brianryner.com>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsAutoCompleteCollector.h"
40
#include "nsMetricsService.h"
42
#include "nsIObserverService.h"
43
#include "nsIAutoCompleteController.h"
44
#include "nsIAutoCompleteInput.h"
45
#include "nsIAutoCompletePopup.h"
46
#include "nsIDOMElement.h"
47
#include "nsServiceManagerUtils.h"
49
static const char kAutoCompleteTopic[] = "autocomplete-will-enter-text";
51
nsAutoCompleteCollector::nsAutoCompleteCollector()
55
nsAutoCompleteCollector::~nsAutoCompleteCollector()
59
NS_IMPL_ISUPPORTS2(nsAutoCompleteCollector, nsIMetricsCollector, nsIObserver)
62
nsAutoCompleteCollector::OnAttach()
64
nsCOMPtr<nsIObserverService> obsSvc =
65
do_GetService("@mozilla.org/observer-service;1");
66
NS_ENSURE_STATE(obsSvc);
68
nsresult rv = obsSvc->AddObserver(this, kAutoCompleteTopic, PR_FALSE);
69
NS_ENSURE_SUCCESS(rv, rv);
75
nsAutoCompleteCollector::OnDetach()
77
nsCOMPtr<nsIObserverService> obsSvc =
78
do_GetService("@mozilla.org/observer-service;1");
79
NS_ENSURE_STATE(obsSvc);
81
nsresult rv = obsSvc->RemoveObserver(this, kAutoCompleteTopic);
82
NS_ENSURE_SUCCESS(rv, rv);
88
nsAutoCompleteCollector::OnNewLog()
94
nsAutoCompleteCollector::Observe(nsISupports *subject,
96
const PRUnichar *data)
98
if (strcmp(topic, kAutoCompleteTopic) != 0) {
99
MS_LOG(("Unexpected observer notification received: %s", topic));
100
return NS_ERROR_UNEXPECTED;
103
nsCOMPtr<nsIAutoCompleteInput> input = do_QueryInterface(subject);
105
MS_LOG(("subject isn't an AutoCompleteInput"));
109
nsCOMPtr<nsIAutoCompletePopup> popup;
110
input->GetPopup(getter_AddRefs(popup));
112
MS_LOG(("AutoCompleteInput has no popup"));
117
nsresult rv = popup->GetPopupOpen(&open);
118
NS_ENSURE_SUCCESS(rv, rv);
120
MS_LOG(("AutoComplete popup is closed, not logging"));
124
PRInt32 selectedIndex;
125
rv = popup->GetSelectedIndex(&selectedIndex);
126
NS_ENSURE_SUCCESS(rv, rv);
127
if (selectedIndex == -1) {
128
MS_LOG(("popup has no selected index, not logging"));
133
rv = input->GetTextValue(textValue);
134
NS_ENSURE_SUCCESS(rv, rv);
136
nsCOMPtr<nsIAutoCompleteController> controller;
137
input->GetController(getter_AddRefs(controller));
138
NS_ENSURE_STATE(controller);
141
rv = controller->GetValueAt(selectedIndex, completion);
142
NS_ENSURE_SUCCESS(rv, rv);
144
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(subject);
146
MS_LOG(("subject isn't a DOMElement"));
151
element->GetAttribute(NS_LITERAL_STRING("id"), id);
153
MS_LOG(("Warning: skipping logging because of empty target ID"));
157
// Fill a property bag for the <uielement> item
158
nsCOMPtr<nsIWritablePropertyBag2> properties;
159
nsMetricsUtils::NewPropertyBag(getter_AddRefs(properties));
160
NS_ENSURE_STATE(properties);
162
PRInt32 window = nsMetricsUtils::FindWindowForNode(element);
163
rv = properties->SetPropertyAsUint32(NS_LITERAL_STRING("window"), window);
164
NS_ENSURE_SUCCESS(rv, rv);
166
rv = properties->SetPropertyAsAString(NS_LITERAL_STRING("action"),
167
NS_LITERAL_STRING("autocomplete"));
168
NS_ENSURE_SUCCESS(rv, rv);
170
nsMetricsService *ms = nsMetricsService::get();
174
rv = ms->HashUTF16(id, hashedId);
175
NS_ENSURE_SUCCESS(rv, rv);
177
rv = properties->SetPropertyAsACString(NS_LITERAL_STRING("targetidhash"),
179
NS_ENSURE_SUCCESS(rv, rv);
181
nsCOMPtr<nsIMetricsEventItem> item;
182
ms->CreateEventItem(NS_LITERAL_STRING("uielement"), getter_AddRefs(item));
183
NS_ENSURE_STATE(item);
184
item->SetProperties(properties);
186
// Now fill in the properties for the <autocomplete> child item
187
nsMetricsUtils::NewPropertyBag(getter_AddRefs(properties));
188
NS_ENSURE_STATE(properties);
190
rv = properties->SetPropertyAsUint32(NS_LITERAL_STRING("typedlength"),
192
NS_ENSURE_SUCCESS(rv, rv);
194
rv = properties->SetPropertyAsInt32(NS_LITERAL_STRING("selectedindex"),
196
NS_ENSURE_SUCCESS(rv, rv);
198
rv = properties->SetPropertyAsInt32(NS_LITERAL_STRING("completedlength"),
199
completion.Length());
200
NS_ENSURE_SUCCESS(rv, rv);
202
rv = nsMetricsUtils::AddChildItem(item, NS_LITERAL_STRING("autocomplete"),
204
NS_ENSURE_SUCCESS(rv, rv);
206
rv = ms->LogEvent(item);
207
NS_ENSURE_SUCCESS(rv, rv);
209
MS_LOG(("Logged autocomplete event:\n"
211
" target %s (hash=%s)\n"
213
" selectedindex: %d\n"
214
" completedlength: %d",
215
window, NS_ConvertUTF16toUTF8(id).get(), hashedId.get(),
216
textValue.Length(), selectedIndex, completion.Length()));