1// Copyright 2014 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/updater/extension_cache_fake.h"
6
7#include "base/bind.h"
8#include "base/stl_util.h"
9#include "content/public/browser/browser_thread.h"
10
11namespace extensions {
12
13ExtensionCacheFake::ExtensionCacheFake() {
14  ExtensionCache::SetForTesting(this);
15}
16
17ExtensionCacheFake::~ExtensionCacheFake() {
18  ExtensionCache::SetForTesting(NULL);
19}
20
21void ExtensionCacheFake::Start(const base::Closure& callback) {
22  content::BrowserThread::PostTask(
23      content::BrowserThread::UI,
24      FROM_HERE,
25      callback);
26}
27
28void ExtensionCacheFake::Shutdown(const base::Closure& callback) {
29  content::BrowserThread::PostTask(
30      content::BrowserThread::UI,
31      FROM_HERE,
32      callback);
33}
34
35void ExtensionCacheFake::AllowCaching(const std::string& id) {
36  allowed_extensions_.insert(id);
37}
38
39bool ExtensionCacheFake::GetExtension(const std::string& id,
40                                      base::FilePath* file_path,
41                                      std::string* version) {
42  Map::iterator it = cache_.find(id);
43  if (it == cache_.end()) {
44    return false;
45  } else {
46    if (version)
47      *version = it->second.first;
48    if (file_path)
49      *file_path = it->second.second;
50    return true;
51  }
52}
53
54void ExtensionCacheFake::PutExtension(const std::string& id,
55                                      const base::FilePath& file_path,
56                                      const std::string& version,
57                                      const PutExtensionCallback& callback) {
58  if (ContainsKey(allowed_extensions_, id)) {
59    cache_[id].first = version;
60    cache_[id].second = file_path;
61    content::BrowserThread::PostTask(
62        content::BrowserThread::UI,
63        FROM_HERE,
64        base::Bind(callback, file_path, false));
65  } else {
66    callback.Run(file_path, true);
67  }
68}
69
70}  // namespace extensions
71