OLD | NEW |
| (Empty) |
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/appcache/chrome_appcache_service.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "chrome/browser/browser_list.h" | |
10 #include "chrome/browser/net/chrome_url_request_context.h" | |
11 #include "chrome/common/chrome_constants.h" | |
12 #include "chrome/common/notification_service.h" | |
13 #include "net/base/net_errors.h" | |
14 #include "webkit/appcache/appcache_thread.h" | |
15 | |
16 static bool has_initialized_thread_ids; | |
17 | |
18 namespace { | |
19 | |
20 // Used to defer deleting of local storage until the destructor has finished. | |
21 void DeleteLocalStateOnIOThread(FilePath cache_path) { | |
22 // Post the actual deletion to the DB thread to ensure it happens after the | |
23 // database file has been closed. | |
24 BrowserThread::PostTask( | |
25 BrowserThread::DB, FROM_HERE, | |
26 NewRunnableFunction<bool(*)(const FilePath&, bool), FilePath, bool>( | |
27 &file_util::Delete, cache_path, true)); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 // ---------------------------------------------------------------------------- | |
33 | |
34 ChromeAppCacheService::ChromeAppCacheService() | |
35 : clear_local_state_on_exit_(false) { | |
36 } | |
37 | |
38 void ChromeAppCacheService::InitializeOnIOThread( | |
39 const FilePath& profile_path, bool is_incognito, | |
40 scoped_refptr<HostContentSettingsMap> content_settings_map, | |
41 bool clear_local_state_on_exit) { | |
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
43 | |
44 if (!has_initialized_thread_ids) { | |
45 has_initialized_thread_ids = true; | |
46 appcache::AppCacheThread::Init(BrowserThread::DB, BrowserThread::IO); | |
47 } | |
48 | |
49 host_contents_settings_map_ = content_settings_map; | |
50 registrar_.Add( | |
51 this, NotificationType::PURGE_MEMORY, NotificationService::AllSources()); | |
52 SetClearLocalStateOnExit(clear_local_state_on_exit); | |
53 if (!is_incognito) | |
54 cache_path_ = profile_path.Append(chrome::kAppCacheDirname); | |
55 | |
56 // Init our base class. | |
57 Initialize(cache_path_, | |
58 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); | |
59 set_appcache_policy(this); | |
60 } | |
61 | |
62 ChromeAppCacheService::~ChromeAppCacheService() { | |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
64 | |
65 if (clear_local_state_on_exit_ && !cache_path_.empty()) { | |
66 BrowserThread::PostTask( | |
67 BrowserThread::IO, FROM_HERE, | |
68 NewRunnableFunction(DeleteLocalStateOnIOThread, cache_path_)); | |
69 } | |
70 } | |
71 | |
72 void ChromeAppCacheService::SetOriginQuotaInMemory( | |
73 const GURL& origin, int64 quota) { | |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
75 if (storage()) | |
76 storage()->SetOriginQuotaInMemory(origin, quota); | |
77 } | |
78 | |
79 void ChromeAppCacheService::ResetOriginQuotaInMemory(const GURL& origin) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
81 if (storage()) | |
82 storage()->ResetOriginQuotaInMemory(origin); | |
83 } | |
84 | |
85 void ChromeAppCacheService::SetClearLocalStateOnExit(bool clear_local_state) { | |
86 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
87 BrowserThread::PostTask( | |
88 BrowserThread::IO, FROM_HERE, | |
89 NewRunnableMethod(this, | |
90 &ChromeAppCacheService::SetClearLocalStateOnExit, | |
91 clear_local_state)); | |
92 return; | |
93 } | |
94 clear_local_state_on_exit_ = clear_local_state; | |
95 } | |
96 | |
97 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) { | |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
99 ContentSetting setting = host_contents_settings_map_->GetContentSetting( | |
100 manifest_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); | |
101 DCHECK(setting != CONTENT_SETTING_DEFAULT); | |
102 // We don't prompt for read access. | |
103 return setting != CONTENT_SETTING_BLOCK; | |
104 } | |
105 | |
106 int ChromeAppCacheService::CanCreateAppCache( | |
107 const GURL& manifest_url, net::CompletionCallback* callback) { | |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
109 ContentSetting setting = host_contents_settings_map_->GetContentSetting( | |
110 manifest_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); | |
111 DCHECK(setting != CONTENT_SETTING_DEFAULT); | |
112 return (setting != CONTENT_SETTING_BLOCK) ? net::OK : | |
113 net::ERR_ACCESS_DENIED; | |
114 } | |
115 | |
116 void ChromeAppCacheService::Observe(NotificationType type, | |
117 const NotificationSource& source, | |
118 const NotificationDetails& details) { | |
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
120 DCHECK(type == NotificationType::PURGE_MEMORY); | |
121 PurgeMemory(); | |
122 } | |
123 | |
124 // ---------------------------------------------------------------------------- | |
125 | |
126 static BrowserThread::ID ToBrowserThreadID(int id) { | |
127 DCHECK(has_initialized_thread_ids); | |
128 DCHECK(id == BrowserThread::DB || id == BrowserThread::IO); | |
129 return static_cast<BrowserThread::ID>(id); | |
130 } | |
131 | |
132 namespace appcache { | |
133 | |
134 // An impl of AppCacheThread we need to provide to the appcache lib. | |
135 | |
136 bool AppCacheThread::PostTask( | |
137 int id, | |
138 const tracked_objects::Location& from_here, | |
139 Task* task) { | |
140 return BrowserThread::PostTask(ToBrowserThreadID(id), from_here, task); | |
141 } | |
142 | |
143 bool AppCacheThread::CurrentlyOn(int id) { | |
144 return BrowserThread::CurrentlyOn(ToBrowserThreadID(id)); | |
145 } | |
146 | |
147 } // namespace appcache | |
OLD | NEW |