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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/discovery/discovery_api.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/api/discovery/suggested_link.h"
9 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h"
10 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/extensions/api/experimental_discovery.h"
13 #include "extensions/common/error_utils.h"
15 namespace discovery = extensions::api::experimental_discovery;
18 const char kInvalidScore[] = "Invalid score, must be between 0 and 1.";
21 namespace extensions {
23 bool DiscoverySuggestFunction::RunImpl() {
24 scoped_ptr<discovery::Suggest::Params> params(
25 discovery::Suggest::Params::Create(*args_));
26 EXTENSION_FUNCTION_VALIDATE(params.get());
29 if (params->details.score != NULL) {
30 score = *params->details.score;
31 if (score < 0.0 || score > 1.0) {
32 error_ = kInvalidScore;
38 const std::string* url_image = ∅
39 if (params->details.url_image != NULL)
40 url_image = params->details.url_image.get();
42 extensions::SuggestedLinksRegistry* registry =
43 extensions::SuggestedLinksRegistryFactory::GetForProfile(GetProfile());
44 scoped_ptr<extensions::SuggestedLink> suggested_link(
45 new extensions::SuggestedLink(params->details.link_url,
46 params->details.link_text,
49 registry->Add(extension_id(), suggested_link.Pass());
53 bool DiscoveryRemoveSuggestionFunction::RunImpl() {
54 scoped_ptr<discovery::RemoveSuggestion::Params> params(
55 discovery::RemoveSuggestion::Params::Create(*args_));
56 EXTENSION_FUNCTION_VALIDATE(params.get());
58 extensions::SuggestedLinksRegistry* registry =
59 extensions::SuggestedLinksRegistryFactory::GetForProfile(GetProfile());
60 registry->Remove(extension_id(), params->link_url);
65 bool DiscoveryClearAllSuggestionsFunction::RunImpl() {
66 extensions::SuggestedLinksRegistry* registry =
67 extensions::SuggestedLinksRegistryFactory::GetForProfile(GetProfile());
68 registry->ClearAll(extension_id());
73 } // namespace extensions