1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_
6#define CHROME_BROWSER_UI_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_
7#pragma once
8
9#include <string>
10
11#include "base/string16.h"
12#include "ui/gfx/native_widget_types.h"
13
14class Profile;
15class TemplateURL;
16
17class EditSearchEngineControllerDelegate {
18 public:
19  // Invoked from the EditSearchEngineController when the user accepts the
20  // edits. NOTE: |template_url| is the value supplied to
21  // EditSearchEngineController's constructor, and may be NULL. A NULL value
22  // indicates a new TemplateURL should be created rather than modifying an
23  // existing TemplateURL.
24  virtual void OnEditedKeyword(const TemplateURL* template_url,
25                               const string16& title,
26                               const string16& keyword,
27                               const std::string& url) = 0;
28
29 protected:
30  virtual ~EditSearchEngineControllerDelegate() {}
31};
32
33// EditSearchEngineController provides the core platform independent logic
34// for the Edit Search Engine dialog.
35class EditSearchEngineController {
36 public:
37  // The |template_url| and/or |edit_keyword_delegate| may be NULL.
38  EditSearchEngineController(
39      const TemplateURL* template_url,
40      EditSearchEngineControllerDelegate* edit_keyword_delegate,
41      Profile* profile);
42  ~EditSearchEngineController() {}
43
44  // Returns true if the value of |title_input| is a valid search engine name.
45  bool IsTitleValid(const string16& title_input) const;
46
47  // Returns true if the value of |url_input| represents a valid search engine
48  // URL. The URL is valid if it contains no search terms and is a valid
49  // url, or if it contains a search term and replacing that search term with a
50  // character results in a valid url.
51  bool IsURLValid(const std::string& url_input) const;
52
53  // Returns true if the value of |keyword_input| represents a valid keyword.
54  // The keyword is valid if it is non-empty and does not conflict with an
55  // existing entry. NOTE: this is just the keyword, not the title and url.
56  bool IsKeywordValid(const string16& keyword_input) const;
57
58  // Completes the add or edit of a search engine.
59  void AcceptAddOrEdit(const string16& title_input,
60                       const string16& keyword_input,
61                       const std::string& url_input);
62
63  // Deletes an unused TemplateURL, if its add was cancelled and it's not
64  // already owned by the TemplateURLModel.
65  void CleanUpCancelledAdd();
66
67  // Accessors.
68  const TemplateURL* template_url() const { return template_url_; }
69  const Profile* profile() const { return profile_; }
70
71 private:
72  // Fixes up and returns the URL the user has input. The returned URL is
73  // suitable for use by TemplateURL.
74  std::string GetFixedUpURL(const std::string& url_input) const;
75
76  // The TemplateURL we're displaying information for. It may be NULL. If we
77  // have a keyword_editor_view, we assume that this TemplateURL is already in
78  // the TemplateURLModel; if not, we assume it isn't.
79  const TemplateURL* template_url_;
80
81  // We may have been created by this, in which case we will call back to it on
82  // success to add/modify the entry.  May be NULL.
83  EditSearchEngineControllerDelegate* edit_keyword_delegate_;
84
85  // Profile whose TemplateURLModel we're modifying.
86  Profile* profile_;
87
88  DISALLOW_COPY_AND_ASSIGN(EditSearchEngineController);
89};
90
91#endif  // CHROME_BROWSER_UI_SEARCH_ENGINES_EDIT_SEARCH_ENGINE_CONTROLLER_H_
92