Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/search_engines/template_url.h

Issue 9982018: Move most TemplateURL data members to a new struct, TemplateURLData. This allows us to eliminate t… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/gtest_prod_util.h" 12 #include "base/gtest_prod_util.h"
13 #include "base/time.h" 13 #include "base/time.h"
14 #include "chrome/browser/search_engines/template_url_id.h" 14 #include "chrome/browser/search_engines/template_url_id.h"
15 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
16 16
17 class PrefService;
18 class Profile; 17 class Profile;
19 class SearchTermsData; 18 class SearchTermsData;
20 class TemplateURL; 19 class TemplateURL;
21 class TemplateURLParsingContext;
22 class WebDataService;
23 20
24 // TemplateURL represents the relevant portions of the Open Search Description 21
25 // Document (http://www.opensearch.org/Specifications/OpenSearch). 22 // TemplateURLRef -------------------------------------------------------------
26 // The main use case for TemplateURL is to use the TemplateURLRef returned by 23
27 // suggestions_url or url for keyword/suggestion expansion: 24 // A TemplateURLRef represents a single URL within the larger TemplateURL class
28 // . suggestions_url describes a URL that is ideal for as you type suggestions. 25 // (which represents an entire "search engine", see below). If
29 // The returned results are in the mime type application/x-suggestions+json. 26 // SupportsReplacement() is true, this URL has placeholders in it, for which
30 // . url describes a URL that may be used as a shortcut. Returned results are 27 // callers can substitute values to get a "real" URL using ReplaceSearchTerms().
31 // are text/html.
32 // Before using either one, make sure it's non-NULL, and if you intend to use
33 // it to replace search terms, make sure SupportsReplacement returns true.
34 // To use either URL invoke the ReplaceSearchTerms method on the corresponding
35 // TemplateURLRef.
36 // 28 //
37 // For files parsed from the Web, be sure and invoke IsValid. IsValid returns 29 // TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they
38 // true if the URL could be parsed. 30 // access in order to get at important data like the underlying URL string.
39 //
40 // Both TemplateURL and TemplateURLRef have value semantics. This allows the
41 // UI to create a copy while the user modifies the values.
42 class TemplateURLRef { 31 class TemplateURLRef {
43 public: 32 public:
44 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| 33 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion|
45 // parameter. Most callers aren't using Suggest capabilities and should just 34 // parameter. Most callers aren't using Suggest capabilities and should just
46 // pass NO_SUGGESTIONS_AVAILABLE. 35 // pass NO_SUGGESTIONS_AVAILABLE.
47 // NOTE: Because positive values are meaningful, make sure these are negative! 36 // NOTE: Because positive values are meaningful, make sure these are negative!
48 enum AcceptedSuggestion { 37 enum AcceptedSuggestion {
49 NO_SUGGESTION_CHOSEN = -1, 38 NO_SUGGESTION_CHOSEN = -1,
50 NO_SUGGESTIONS_AVAILABLE = -2, 39 NO_SUGGESTIONS_AVAILABLE = -2,
51 }; 40 };
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 mutable std::string host_; 228 mutable std::string host_;
240 mutable std::string path_; 229 mutable std::string path_;
241 mutable std::string search_term_key_; 230 mutable std::string search_term_key_;
242 231
243 // Whether the contained URL is a pre-populated URL. 232 // Whether the contained URL is a pre-populated URL.
244 bool prepopulated_; 233 bool prepopulated_;
245 234
246 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef); 235 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef);
247 }; 236 };
248 237
249 // Describes the relevant portions of a single OSD document. 238
239 // TemplateURLData ------------------------------------------------------------
240
241 // The data for the TemplateURL. Separating this into its own class allows most
242 // users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
243 // whatever fields are desired, then create an immutable TemplateURL from it.
244 struct TemplateURLData {
245 TemplateURLData();
246
sky 2012/04/05 03:42:15 I think you'll want to declare the destructor too.
Peter Kasting 2012/04/05 19:52:21 It did, and I now have.
247 // A short description of the template. This is the name we show to the user
248 // in various places that use TemplateURLs. For example, the location bar
249 // shows this when the user selects a substituting match.
250 string16 short_name;
sky 2012/04/05 03:42:15 move after functions.
Peter Kasting 2012/04/05 19:52:21 Normally I'd agree. In this class I was trying to
251
252 // The shortcut for this TemplateURL. May be empty.
253 void SetKeyword(const string16& keyword) const;
254 const string16& keyword(const TemplateURL* t_url) const;
255 // TODO(pkasting): This should only be necessary until we eliminate keyword
256 // autogeneration.
257 const string16& raw_keyword() const { return keyword_; }
258
259 // Whether to autogenerate a keyword in TemplateURL::GetKeyword(). Most
260 // consumers should not need this.
261 // NOTE: Calling SetKeyword() turns this back off. Manual and automatic
262 // keywords are mutually exclusive.
263 void SetAutogenerateKeyword(bool autogenerate_keyword) const;
264 bool autogenerate_keyword() const { return autogenerate_keyword_; }
265
266 void EnsureKeyword(const TemplateURL* t_url) const;
sky 2012/04/05 03:42:15 Description?
Peter Kasting 2012/04/05 19:52:21 OK. Moved the one in TemplateURL to here.
267
268 // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
269 // it requires substitutions first).
270 void SetURL(const std::string& url);
271 const std::string& url() const { return url_; }
272
273 // Optional additional raw URLs.
274 std::string suggestions_url;
275 std::string instant_url;
276
277 // Optional favicon for the TemplateURL.
278 GURL favicon_url;
279
280 // URL to the OSD file this came from. May be empty.
281 GURL originating_url;
282
283 // Whether this TemplateURL is shown in the default list of search providers.
284 // This is just a property and does not indicate whether the TemplateURL has a
285 // TemplateURLRef that supports replacement. Use
286 // TemplateURL::ShowInDefaultList() to test both.
287 bool show_in_default_list;
288
289 // Whether it's safe for auto-modification code (the autogenerator and the
290 // code that imports data from other browsers) to replace the TemplateURL.
291 // This should be set to false for any TemplateURL the user edits, or any
292 // TemplateURL that the user clearly manually edited in the past, like a
293 // bookmark keyword from another browser.
294 bool safe_for_autoreplace;
295
296 // The list of supported encodings for the search terms. This may be empty,
297 // which indicates the terms should be encoded with UTF-8.
298 std::vector<std::string> input_encodings;
299
300 // Unique identifier of this TemplateURL. The unique ID is set by the
301 // TemplateURLService when the TemplateURL is added to it.
302 TemplateURLID id;
303
304 // Date this TemplateURL was created.
305 //
306 // NOTE: this may be 0, which indicates the TemplateURL was created before we
307 // started tracking creation time.
308 base::Time date_created;
309
310 // The last time this TemplateURL was modified by a user, since creation.
311 //
312 // NOTE: Like date_created above, this may be 0.
313 base::Time last_modified;
314
315 // True if this TemplateURL was automatically created by the administrator via
316 // group policy.
317 bool created_by_policy;
318
319 // Number of times this TemplateURL has been explicitly used to load a URL.
320 // We don't increment this for uses as the "default search engine" since
321 // that's not really "explicit" usage and incrementing would result in pinning
322 // the user's default search engine(s) to the top of the list of searches on
323 // the New Tab page, de-emphasizing the omnibox as "where you go to search".
324 int usage_count;
325
326 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
327 int prepopulate_id;
328
329 // The primary unique identifier for Sync. This set on all TemplateURLs
330 // regardless of whether they have been associated with Sync.
331 std::string sync_guid;
332
333 private:
334 // Private so we can enforce using the setters.
335 // TODO(pkasting): For now these setters are not critical, but later we will
336 // begin using them to ensure that these fields are non-empty.
337 mutable string16 keyword_;
338 std::string url_;
339
340 // TODO(pkasting): These fields will go away soon.
341 mutable bool autogenerate_keyword_;
342 // True if the keyword was generated. This is used to avoid multiple attempts
343 // if generating a keyword failed.
344 mutable bool keyword_generated_;
345 };
346
347
348 // TemplateURL ----------------------------------------------------------------
349
350 // A TemplateURL represents a single "search engine", defined primarily as a
351 // subset of the Open Search Description Document
352 // (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions.
353 // One TemplateURL contains several TemplateURLRefs, which correspond to various
354 // different capabilities (e.g. doing searches or getting suggestions), as well
355 // as a TemplateURLData containing other details like the name, keyword, etc.
356 //
357 // TemplateURLs are intended to be read-only for most users; the only public
358 // non-const method is the Profile getter, which returns a non-const Profile*.
359 // The TemplateURLService, which handles storing and manipulating TemplateURLs,
360 // is made a friend so that it can be the exception to this pattern.
250 class TemplateURL { 361 class TemplateURL {
251 public: 362 public:
252 TemplateURL(); 363 explicit TemplateURL(const TemplateURLData& data);
253 364
254 TemplateURL(const TemplateURL& other); 365 TemplateURL(const TemplateURL& other);
255 TemplateURL& operator=(const TemplateURL& other); 366 TemplateURL& operator=(const TemplateURL& other);
256 367
257 ~TemplateURL(); 368 ~TemplateURL();
258 369
259 // Generates a favicon URL from the specified url. 370 // Generates a favicon URL from the specified url.
260 static GURL GenerateFaviconURL(const GURL& url); 371 static GURL GenerateFaviconURL(const GURL& url);
261 372
262 // A short description of the template. This is the name we show to the user 373 const TemplateURLData& data() const { return data_; }
263 // in various places that use keywords. For example, the location bar shows 374
264 // this when the user selects the keyword. 375 const string16& short_name() const { return data_.short_name; }
265 void set_short_name(const string16& short_name) {
266 short_name_ = short_name;
267 }
268 const string16& short_name() const { return short_name_; }
269 // An accessor for the short_name, but adjusted so it can be appropriately 376 // An accessor for the short_name, but adjusted so it can be appropriately
270 // displayed even if it is LTR and the UI is RTL. 377 // displayed even if it is LTR and the UI is RTL.
271 string16 AdjustedShortNameForLocaleDirection() const; 378 string16 AdjustedShortNameForLocaleDirection() const;
272 379
273 // Parameterized URL for providing the results. 380 const string16& keyword() const { return data_.keyword(this); }
274 void SetURL(const std::string& url); 381 bool autogenerate_keyword() const {
275 const std::string& url() const { return url_; } 382 return data_.autogenerate_keyword();
276
277 // URL providing JSON results. This is typically used to provide suggestions
278 // as you type.
279 void SetSuggestionsURL(const std::string& url);
280 const std::string& suggestions_url() const { return suggestions_url_; }
281
282 // Parameterized URL for instant results.
283 void SetInstantURL(const std::string& url);
284 const std::string& instant_url() const { return instant_url_; }
285
286 // URL to the OSD file this came from. May be empty.
287 void set_originating_url(const GURL& url) {
288 originating_url_ = url;
289 } 383 }
290 const GURL& originating_url() const { return originating_url_; }
291
292 // The shortcut for this template url. May be empty.
293 void set_keyword(const string16& keyword);
294 const string16& keyword() const;
295
296 // Whether to autogenerate a keyword from the url() in GetKeyword(). Most
297 // consumers should not need this.
298 // NOTE: Calling set_keyword() turns this back off. Manual and automatic
299 // keywords are mutually exclusive.
300 void set_autogenerate_keyword(bool autogenerate_keyword) {
301 autogenerate_keyword_ = autogenerate_keyword;
302 if (autogenerate_keyword_) {
303 keyword_.clear();
304 keyword_generated_ = false;
305 }
306 }
307 bool autogenerate_keyword() const {
308 return autogenerate_keyword_;
309 }
310
311 // Ensures that the keyword is generated. Most consumers should not need this 384 // Ensures that the keyword is generated. Most consumers should not need this
312 // because it is done automatically. Use this method on the UI thread, so 385 // because it is done automatically. Use this method on the UI thread, so
313 // the keyword may be accessed on another thread. 386 // the keyword may be accessed on another thread.
314 void EnsureKeyword() const; 387 void EnsureKeyword() const { data_.EnsureKeyword(this); }
315 388
316 // Whether this keyword is shown in the default list of search providers. This 389 const std::string& url() const { return data_.url(); }
317 // is just a property and does not indicate whether this TemplateURL has 390 const std::string& suggestions_url() const { return data_.suggestions_url; }
318 // a TemplateURLRef that supports replacement. Use ShowInDefaultList to 391 const std::string& instant_url() const { return data_.instant_url; }
319 // test both. 392 const GURL& favicon_url() const { return data_.favicon_url; }
320 // The default value is false. 393
321 void set_show_in_default_list(bool show_in_default_list) { 394 const GURL& originating_url() const { return data_.originating_url; }
322 show_in_default_list_ = show_in_default_list; 395
323 } 396 bool show_in_default_list() const { return data_.show_in_default_list; }
324 bool show_in_default_list() const { return show_in_default_list_; }
325 397
326 // Returns true if show_in_default_list() is true and this TemplateURL has a 398 // Returns true if show_in_default_list() is true and this TemplateURL has a
327 // TemplateURLRef that supports replacement. 399 // TemplateURLRef that supports replacement.
328 bool ShowInDefaultList() const; 400 bool ShowInDefaultList() const;
329 401
330 // Whether it's safe for auto-modification code (the autogenerator and the 402 bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; }
331 // code that imports data from other browsers) to replace the TemplateURL.
332 // This should be set to false for any keyword the user edits, or any keyword
333 // that the user clearly manually edited in the past, like a bookmark keyword
334 // from another browser.
335 void set_safe_for_autoreplace(bool safe_for_autoreplace) {
336 safe_for_autoreplace_ = safe_for_autoreplace;
337 }
338 bool safe_for_autoreplace() const { return safe_for_autoreplace_; }
339 403
340 // The favicon. This is optional.
341 void set_favicon_url(const GURL& url) { favicon_url_ = url; }
342 const GURL& favicon_url() const { return favicon_url_; }
343
344 // Date this keyword was created.
345 //
346 // NOTE: this may be 0, which indicates the keyword was created before we
347 // started tracking creation time.
348 void set_date_created(base::Time time) { date_created_ = time; }
349 base::Time date_created() const { return date_created_; }
350
351 // The last time this keyword was modified by a user, since creation.
352 //
353 // NOTE: Like date_created above, this may be 0.
354 void set_last_modified(base::Time time) { last_modified_ = time; }
355 base::Time last_modified() const { return last_modified_; }
356
357 // True if this TemplateURL was automatically created by the administrator via
358 // group policy.
359 void set_created_by_policy(bool created_by_policy) {
360 created_by_policy_ = created_by_policy;
361 }
362 bool created_by_policy() const { return created_by_policy_; }
363
364 // Number of times this keyword has been explicitly used to load a URL. We
365 // don't increment this for uses as the "default search engine" since that's
366 // not really "explicit" usage and incrementing would result in pinning the
367 // user's default search engine(s) to the top of the list of searches on the
368 // New Tab page, de-emphasizing the omnibox as "where you go to search".
369 void set_usage_count(int count) { usage_count_ = count; }
370 int usage_count() const { return usage_count_; }
371
372 // The list of supported encodings for the search terms. This may be empty,
373 // which indicates the terms should be encoded with UTF-8.
374 void set_input_encodings(const std::vector<std::string>& encodings) {
375 input_encodings_ = encodings;
376 }
377 void add_input_encoding(const std::string& encoding) {
378 input_encodings_.push_back(encoding);
379 }
380 const std::vector<std::string>& input_encodings() const { 404 const std::vector<std::string>& input_encodings() const {
381 return input_encodings_; 405 return data_.input_encodings;
382 } 406 }
383 407
384 // Returns the unique identifier of this TemplateURL. The unique ID is set 408 TemplateURLID id() const { return data_.id; }
385 // by the TemplateURLService when the TemplateURL is added to it.
386 TemplateURLID id() const { return id_; }
387 409
388 // Copies the data from |other|'s TemplateURLRef members into |this|. 410 base::Time date_created() const { return data_.date_created; }
389 void CopyURLRefs(const TemplateURL& other); 411 base::Time last_modified() const { return data_.last_modified; }
390 412
391 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. 413 bool created_by_policy() const { return data_.created_by_policy; }
392 // SetPrepopulateId also sets any TemplateURLRef's prepopulated flag to true
393 // if |id| > 0 and false otherwise.
394 void SetPrepopulateId(int id);
395 int prepopulate_id() const { return prepopulate_id_; }
396 414
397 const std::string& sync_guid() const { return sync_guid_; } 415 int usage_count() const { return data_.usage_count; }
398 void set_sync_guid(const std::string& guid) { sync_guid_ = guid; } 416
417 int prepopulate_id() const { return data_.prepopulate_id; }
418
419 const std::string& sync_guid() const { return data_.sync_guid; }
399 420
400 const TemplateURLRef& url_ref() const { return url_ref_; } 421 const TemplateURLRef& url_ref() const { return url_ref_; }
401 const TemplateURLRef& suggestions_url_ref() const { 422 const TemplateURLRef& suggestions_url_ref() const {
402 return suggestions_url_ref_; 423 return suggestions_url_ref_;
403 } 424 }
404 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; } 425 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; }
405 426
406 // Returns true if |url| supports replacement. 427 // Returns true if |url| supports replacement.
407 bool SupportsReplacement() const; 428 bool SupportsReplacement() const;
408 429
409 // Like SupportsReplacement but usable on threads other than the UI thread. 430 // Like SupportsReplacement but usable on threads other than the UI thread.
410 bool SupportsReplacementUsingTermsData( 431 bool SupportsReplacementUsingTermsData(
411 const SearchTermsData& search_terms_data) const; 432 const SearchTermsData& search_terms_data) const;
412 433
413 std::string GetExtensionId() const; 434 std::string GetExtensionId() const;
414 bool IsExtensionKeyword() const; 435 bool IsExtensionKeyword() const;
415 436
416 private: 437 private:
417 friend void MergeEnginesFromPrepopulateData(
418 PrefService* prefs,
419 WebDataService* service,
420 std::vector<TemplateURL*>* template_urls,
421 const TemplateURL** default_search_provider);
422 friend class KeywordTable;
423 friend class KeywordTableTest;
424 friend class SearchHostToURLsMap;
425 friend class TemplateURLParsingContext;
426 friend class TemplateURLService; 438 friend class TemplateURLService;
427 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, 439
428 ResolveSyncKeywordConflict); 440 void SetURL(const std::string& url);
441 void SetPrepopulateId(int id);
429 442
430 // Invalidates cached values on this object and its child TemplateURLRefs. 443 // Invalidates cached values on this object and its child TemplateURLRefs.
431 void InvalidateCachedValues() const; 444 void InvalidateCachedValues() const;
432 445
433 // Unique identifier, used when archived to the database. 446 TemplateURLData data_;
434 void set_id(TemplateURLID id) { id_ = id; }
435
436 string16 short_name_;
437 std::string url_;
438 std::string suggestions_url_;
439 std::string instant_url_;
440 GURL originating_url_;
441 mutable string16 keyword_;
442 bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached
443 // generated keyword if available.
444 mutable bool keyword_generated_; // True if the keyword was generated. This
445 // is used to avoid multiple attempts if
446 // generating a keyword failed.
447 bool show_in_default_list_;
448 bool safe_for_autoreplace_;
449 GURL favicon_url_;
450 // List of supported input encodings.
451 std::vector<std::string> input_encodings_;
452 TemplateURLID id_;
453 base::Time date_created_;
454 base::Time last_modified_;
455 bool created_by_policy_;
456 int usage_count_;
457 int prepopulate_id_;
458 // The primary unique identifier for Sync. This is only set on TemplateURLs
459 // that have been associated with Sync.
460 std::string sync_guid_;
461
462 TemplateURLRef url_ref_; 447 TemplateURLRef url_ref_;
463 TemplateURLRef suggestions_url_ref_; 448 TemplateURLRef suggestions_url_ref_;
464 TemplateURLRef instant_url_ref_; 449 TemplateURLRef instant_url_ref_;
465 450
466 // TODO(sky): Add date last parsed OSD file. 451 // TODO(sky): Add date last parsed OSD file.
467 }; 452 };
468 453
469 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ 454 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698