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#include "chrome/browser/extensions/extension_test_api.h" 6 7#include <string> 8 9#include "base/memory/singleton.h" 10#include "chrome/browser/extensions/extension_service.h" 11#include "chrome/browser/extensions/extensions_quota_service.h" 12#include "chrome/browser/profiles/profile.h" 13#include "chrome/browser/ui/browser.h" 14#include "content/common/notification_service.h" 15 16namespace { 17 18// If you see this error in your test, you need to set the config state 19// to be returned by chrome.test.getConfig(). Do this by calling 20// ExtensionTestGetConfigFunction::set_test_config_state(Value* state) 21// in test set up. 22const char kNoTestConfigDataError[] = "Test configuration was not set."; 23 24} // namespace 25 26ExtensionTestPassFunction::~ExtensionTestPassFunction() {} 27 28bool ExtensionTestPassFunction::RunImpl() { 29 NotificationService::current()->Notify( 30 NotificationType::EXTENSION_TEST_PASSED, 31 Source<Profile>(dispatcher()->profile()), 32 NotificationService::NoDetails()); 33 return true; 34} 35 36ExtensionTestFailFunction::~ExtensionTestFailFunction() {} 37 38bool ExtensionTestFailFunction::RunImpl() { 39 std::string message; 40 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); 41 NotificationService::current()->Notify( 42 NotificationType::EXTENSION_TEST_FAILED, 43 Source<Profile>(dispatcher()->profile()), 44 Details<std::string>(&message)); 45 return true; 46} 47 48ExtensionTestLogFunction::~ExtensionTestLogFunction() {} 49 50bool ExtensionTestLogFunction::RunImpl() { 51 std::string message; 52 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); 53 return true; 54} 55 56ExtensionTestQuotaResetFunction::~ExtensionTestQuotaResetFunction() {} 57 58bool ExtensionTestQuotaResetFunction::RunImpl() { 59 ExtensionService* service = profile()->GetExtensionService(); 60 ExtensionsQuotaService* quota = service->quota_service(); 61 quota->Purge(); 62 quota->violators_.clear(); 63 return true; 64} 65 66ExtensionTestCreateIncognitoTabFunction:: 67 ~ExtensionTestCreateIncognitoTabFunction() {} 68 69bool ExtensionTestCreateIncognitoTabFunction::RunImpl() { 70 std::string url; 71 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); 72 Browser::OpenURLOffTheRecord(profile(), GURL(url)); 73 return true; 74} 75 76bool ExtensionTestSendMessageFunction::RunImpl() { 77 std::string message; 78 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); 79 AddRef(); // balanced in Reply 80 NotificationService::current()->Notify( 81 NotificationType::EXTENSION_TEST_MESSAGE, 82 Source<ExtensionTestSendMessageFunction>(this), 83 Details<std::string>(&message)); 84 return true; 85} 86ExtensionTestSendMessageFunction::~ExtensionTestSendMessageFunction() {} 87 88void ExtensionTestSendMessageFunction::Reply(const std::string& message) { 89 result_.reset(Value::CreateStringValue(message)); 90 SendResponse(true); 91 Release(); // balanced in RunImpl 92} 93 94// static 95void ExtensionTestGetConfigFunction::set_test_config_state( 96 DictionaryValue* value) { 97 TestConfigState* test_config_state = TestConfigState::GetInstance(); 98 test_config_state->set_config_state(value); 99} 100 101ExtensionTestGetConfigFunction::TestConfigState::TestConfigState() 102 : config_state_(NULL) {} 103 104// static 105ExtensionTestGetConfigFunction::TestConfigState* 106ExtensionTestGetConfigFunction::TestConfigState::GetInstance() { 107 return Singleton<TestConfigState>::get(); 108} 109 110ExtensionTestGetConfigFunction::~ExtensionTestGetConfigFunction() {} 111 112bool ExtensionTestGetConfigFunction::RunImpl() { 113 TestConfigState* test_config_state = TestConfigState::GetInstance(); 114 115 if (!test_config_state->config_state()) { 116 error_ = kNoTestConfigDataError; 117 return false; 118 } 119 120 result_.reset(test_config_state->config_state()->DeepCopy()); 121 return true; 122} 123