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. 4 5 #include "chrome/browser/extensions/api/discovery/discovery_api.h" 6 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" 14 15 namespace discovery = extensions::api::experimental_discovery; 16 17 namespace { 18 const char kInvalidScore[] = "Invalid score, must be between 0 and 1."; 19 } // namespace 20 21 namespace extensions { 22 RunImpl()23bool DiscoverySuggestFunction::RunImpl() { 24 scoped_ptr<discovery::Suggest::Params> params( 25 discovery::Suggest::Params::Create(*args_)); 26 EXTENSION_FUNCTION_VALIDATE(params.get()); 27 28 double score = 1.0; 29 if (params->details.score != NULL) { 30 score = *params->details.score; 31 if (score < 0.0 || score > 1.0) { 32 error_ = kInvalidScore; 33 return false; 34 } 35 } 36 37 std::string empty; 38 const std::string* url_image = ∅ 39 if (params->details.url_image != NULL) 40 url_image = params->details.url_image.get(); 41 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, 47 *url_image, 48 score)); 49 registry->Add(extension_id(), suggested_link.Pass()); 50 return true; 51 } 52 RunImpl()53bool DiscoveryRemoveSuggestionFunction::RunImpl() { 54 scoped_ptr<discovery::RemoveSuggestion::Params> params( 55 discovery::RemoveSuggestion::Params::Create(*args_)); 56 EXTENSION_FUNCTION_VALIDATE(params.get()); 57 58 extensions::SuggestedLinksRegistry* registry = 59 extensions::SuggestedLinksRegistryFactory::GetForProfile(GetProfile()); 60 registry->Remove(extension_id(), params->link_url); 61 62 return true; 63 } 64 RunImpl()65bool DiscoveryClearAllSuggestionsFunction::RunImpl() { 66 extensions::SuggestedLinksRegistry* registry = 67 extensions::SuggestedLinksRegistryFactory::GetForProfile(GetProfile()); 68 registry->ClearAll(extension_id()); 69 70 return true; 71 } 72 73 } // namespace extensions 74 75