summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines/edit_search_engine_controller.h
blob: e2edee63b1f6f454309b90593ac0c80a192fc84a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_
#define CHROME_BROWSER_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_
#pragma once

#include <string>

#include "base/string16.h"
#include "gfx/native_widget_types.h"

class Profile;
class TemplateURL;

class EditSearchEngineControllerDelegate {
 public:
  // Invoked from the EditSearchEngineController when the user accepts the
  // edits. NOTE: |template_url| is the value supplied to
  // EditSearchEngineController's constructor, and may be NULL. A NULL value
  // indicates a new TemplateURL should be created rather than modifying an
  // existing TemplateURL.
  virtual void OnEditedKeyword(const TemplateURL* template_url,
                               const string16& title,
                               const string16& keyword,
                               const std::string& url) = 0;

 protected:
  virtual ~EditSearchEngineControllerDelegate() {}
};

// EditSearchEngineController provides the core platform independent logic
// for the Edit Search Engine dialog.
class EditSearchEngineController {
 public:
  // The |template_url| and/or |edit_keyword_delegate| may be NULL.
  EditSearchEngineController(
      const TemplateURL* template_url,
      EditSearchEngineControllerDelegate* edit_keyword_delegate,
      Profile* profile);
  ~EditSearchEngineController() {}

  // Returns true if the value of |title_input| is a valid search engine name.
  bool IsTitleValid(const string16& title_input) const;

  // Returns true if the value of |url_input| represents a valid search engine
  // URL. The URL is valid if it contains no search terms and is a valid
  // url, or if it contains a search term and replacing that search term with a
  // character results in a valid url.
  bool IsURLValid(const std::string& url_input) const;

  // Returns true if the value of |keyword_input| represents a valid keyword.
  // The keyword is valid if it is non-empty and does not conflict with an
  // existing entry. NOTE: this is just the keyword, not the title and url.
  bool IsKeywordValid(const string16& keyword_input) const;

  // Completes the add or edit of a search engine.
  void AcceptAddOrEdit(const string16& title_input,
                       const string16& keyword_input,
                       const std::string& url_input);

  // Deletes an unused TemplateURL, if its add was cancelled and it's not
  // already owned by the TemplateURLModel.
  void CleanUpCancelledAdd();

  // Accessors.
  const TemplateURL* template_url() const { return template_url_; }
  const Profile* profile() const { return profile_; }

 private:
  // Fixes up and returns the URL the user has input. The returned URL is
  // suitable for use by TemplateURL.
  std::string GetFixedUpURL(const std::string& url_input) const;

  // The TemplateURL we're displaying information for. It may be NULL. If we
  // have a keyword_editor_view, we assume that this TemplateURL is already in
  // the TemplateURLModel; if not, we assume it isn't.
  const TemplateURL* template_url_;

  // We may have been created by this, in which case we will call back to it on
  // success to add/modify the entry.  May be NULL.
  EditSearchEngineControllerDelegate* edit_keyword_delegate_;

  // Profile whose TemplateURLModel we're modifying.
  Profile* profile_;

  DISALLOW_COPY_AND_ASSIGN(EditSearchEngineController);
};

#endif  // CHROME_BROWSER_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_