~ubuntu-branches/ubuntu/gutsy/firefox/gutsy

« back to all changes in this revision

Viewing changes to extensions/metrics/src/nsAutoCompleteCollector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ian Jackson
  • Date: 2006-10-10 18:49:32 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20061010184932-da75izt7y0e59afq
Tags: 1.99+2.0rc2+dfsg-0ubuntu1
* New upstream version 2.0rc2.
* Fix/workaround for epiphany GtkSocket lifetype crash:
  apply patch id=241087 from Mozilla Bugzilla #241535 to fix LP #63814.
* Change application name to `Firefox', as requested by mdz.
  Files changed:
    - browser/locales/en-US/chrome/branding/brand.dtd
    - browser/locales/en-US/chrome/branding/brand.properties;
  New values:
    - brandShortName and brandFullName: `Bon Echo' => `Firefox'
    - vendorShortName: `Mozilla' => `Ubuntu'
* Make preferences dialogue fit again (bah!).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 *
 
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/
 
10
 *
 
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
 
14
 * License.
 
15
 *
 
16
 * The Original Code is the Metrics extension.
 
17
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *  Brian Ryner <bryner@brianryner.com>
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsAutoCompleteCollector.h"
 
40
#include "nsMetricsService.h"
 
41
 
 
42
#include "nsIObserverService.h"
 
43
#include "nsIAutoCompleteController.h"
 
44
#include "nsIAutoCompleteInput.h"
 
45
#include "nsIAutoCompletePopup.h"
 
46
#include "nsIDOMElement.h"
 
47
#include "nsServiceManagerUtils.h"
 
48
 
 
49
static const char kAutoCompleteTopic[] = "autocomplete-will-enter-text";
 
50
 
 
51
nsAutoCompleteCollector::nsAutoCompleteCollector()
 
52
{
 
53
}
 
54
 
 
55
nsAutoCompleteCollector::~nsAutoCompleteCollector()
 
56
{
 
57
}
 
58
 
 
59
NS_IMPL_ISUPPORTS2(nsAutoCompleteCollector, nsIMetricsCollector, nsIObserver)
 
60
 
 
61
NS_IMETHODIMP
 
62
nsAutoCompleteCollector::OnAttach()
 
63
{
 
64
  nsCOMPtr<nsIObserverService> obsSvc =
 
65
    do_GetService("@mozilla.org/observer-service;1");
 
66
  NS_ENSURE_STATE(obsSvc);
 
67
 
 
68
  nsresult rv = obsSvc->AddObserver(this, kAutoCompleteTopic, PR_FALSE);
 
69
  NS_ENSURE_SUCCESS(rv, rv);
 
70
 
 
71
  return NS_OK;
 
72
}
 
73
 
 
74
NS_IMETHODIMP
 
75
nsAutoCompleteCollector::OnDetach()
 
76
{
 
77
  nsCOMPtr<nsIObserverService> obsSvc =
 
78
    do_GetService("@mozilla.org/observer-service;1");
 
79
  NS_ENSURE_STATE(obsSvc);
 
80
 
 
81
  nsresult rv = obsSvc->RemoveObserver(this, kAutoCompleteTopic);
 
82
  NS_ENSURE_SUCCESS(rv, rv);
 
83
 
 
84
  return NS_OK;
 
85
}
 
86
 
 
87
NS_IMETHODIMP
 
88
nsAutoCompleteCollector::OnNewLog()
 
89
{
 
90
  return NS_OK;
 
91
}
 
92
 
 
93
NS_IMETHODIMP
 
94
nsAutoCompleteCollector::Observe(nsISupports *subject,
 
95
                                 const char *topic,
 
96
                                 const PRUnichar *data)
 
97
{
 
98
  if (strcmp(topic, kAutoCompleteTopic) != 0) {
 
99
    MS_LOG(("Unexpected observer notification received: %s", topic));
 
100
    return NS_ERROR_UNEXPECTED;
 
101
  }
 
102
 
 
103
  nsCOMPtr<nsIAutoCompleteInput> input = do_QueryInterface(subject);
 
104
  if (!input) {
 
105
    MS_LOG(("subject isn't an AutoCompleteInput"));
 
106
    return NS_OK;
 
107
  }
 
108
 
 
109
  nsCOMPtr<nsIAutoCompletePopup> popup;
 
110
  input->GetPopup(getter_AddRefs(popup));
 
111
  if (!popup) {
 
112
    MS_LOG(("AutoCompleteInput has no popup"));
 
113
    return NS_OK;
 
114
  }
 
115
 
 
116
  PRBool open;
 
117
  nsresult rv = popup->GetPopupOpen(&open);
 
118
  NS_ENSURE_SUCCESS(rv, rv);
 
119
  if (!open) {
 
120
    MS_LOG(("AutoComplete popup is closed, not logging"));
 
121
    return NS_OK;
 
122
  }
 
123
 
 
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"));
 
129
    return NS_OK;
 
130
  }
 
131
 
 
132
  nsString textValue;
 
133
  rv = input->GetTextValue(textValue);
 
134
  NS_ENSURE_SUCCESS(rv, rv);
 
135
 
 
136
  nsCOMPtr<nsIAutoCompleteController> controller;
 
137
  input->GetController(getter_AddRefs(controller));
 
138
  NS_ENSURE_STATE(controller);
 
139
 
 
140
  nsString completion;
 
141
  rv = controller->GetValueAt(selectedIndex, completion);
 
142
  NS_ENSURE_SUCCESS(rv, rv);
 
143
 
 
144
  nsCOMPtr<nsIDOMElement> element = do_QueryInterface(subject);
 
145
  if (!element) {
 
146
    MS_LOG(("subject isn't a DOMElement"));
 
147
    return NS_OK;
 
148
  }
 
149
 
 
150
  nsString id;
 
151
  element->GetAttribute(NS_LITERAL_STRING("id"), id);
 
152
  if (id.IsEmpty()) {
 
153
    MS_LOG(("Warning: skipping logging because of empty target ID"));
 
154
    return NS_OK;
 
155
  }
 
156
 
 
157
  // Fill a property bag for the <uielement> item
 
158
  nsCOMPtr<nsIWritablePropertyBag2> properties;
 
159
  nsMetricsUtils::NewPropertyBag(getter_AddRefs(properties));
 
160
  NS_ENSURE_STATE(properties);
 
161
 
 
162
  PRInt32 window = nsMetricsUtils::FindWindowForNode(element);
 
163
  rv = properties->SetPropertyAsUint32(NS_LITERAL_STRING("window"), window);
 
164
  NS_ENSURE_SUCCESS(rv, rv);
 
165
 
 
166
  rv = properties->SetPropertyAsAString(NS_LITERAL_STRING("action"),
 
167
                                        NS_LITERAL_STRING("autocomplete"));
 
168
  NS_ENSURE_SUCCESS(rv, rv);
 
169
 
 
170
  nsMetricsService *ms = nsMetricsService::get();
 
171
  NS_ENSURE_STATE(ms);
 
172
 
 
173
  nsCString hashedId;
 
174
  rv = ms->HashUTF16(id, hashedId);
 
175
  NS_ENSURE_SUCCESS(rv, rv);
 
176
 
 
177
  rv = properties->SetPropertyAsACString(NS_LITERAL_STRING("targetidhash"),
 
178
                                        hashedId);
 
179
  NS_ENSURE_SUCCESS(rv, rv);
 
180
 
 
181
  nsCOMPtr<nsIMetricsEventItem> item;
 
182
  ms->CreateEventItem(NS_LITERAL_STRING("uielement"), getter_AddRefs(item));
 
183
  NS_ENSURE_STATE(item);
 
184
  item->SetProperties(properties);
 
185
 
 
186
  // Now fill in the properties for the <autocomplete> child item
 
187
  nsMetricsUtils::NewPropertyBag(getter_AddRefs(properties));
 
188
  NS_ENSURE_STATE(properties);
 
189
 
 
190
  rv = properties->SetPropertyAsUint32(NS_LITERAL_STRING("typedlength"),
 
191
                                      textValue.Length());
 
192
  NS_ENSURE_SUCCESS(rv, rv);
 
193
 
 
194
  rv = properties->SetPropertyAsInt32(NS_LITERAL_STRING("selectedindex"),
 
195
                                      selectedIndex);
 
196
  NS_ENSURE_SUCCESS(rv, rv);
 
197
 
 
198
  rv = properties->SetPropertyAsInt32(NS_LITERAL_STRING("completedlength"),
 
199
                                      completion.Length());
 
200
  NS_ENSURE_SUCCESS(rv, rv);
 
201
 
 
202
  rv = nsMetricsUtils::AddChildItem(item, NS_LITERAL_STRING("autocomplete"),
 
203
                                    properties);
 
204
  NS_ENSURE_SUCCESS(rv, rv);
 
205
 
 
206
  rv = ms->LogEvent(item);
 
207
  NS_ENSURE_SUCCESS(rv, rv);
 
208
 
 
209
  MS_LOG(("Logged autocomplete event:\n"
 
210
          "  window id: %d\n"
 
211
          "  target %s (hash=%s)\n"
 
212
          "  typedlength: %d\n"
 
213
          "  selectedindex: %d\n"
 
214
          "  completedlength: %d",
 
215
          window, NS_ConvertUTF16toUTF8(id).get(), hashedId.get(),
 
216
          textValue.Length(), selectedIndex, completion.Length()));
 
217
 
 
218
  return NS_OK;
 
219
}