summaryrefslogtreecommitdiffstats
path: root/chrome/browser/privacy_blacklist/blacklist_manager.cc
blob: f71a26fbfa78c226627c8897f5730342df2bd6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/privacy_blacklist/blacklist_manager.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/thread.h"
#include "chrome/browser/privacy_blacklist/blacklist.h"
#include "chrome/browser/privacy_blacklist/blacklist_io.h"
#include "chrome/browser/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"

BlacklistPathProvider::~BlacklistPathProvider() {
}

BlacklistManager::BlacklistManager(Profile* profile,
                                   BlacklistPathProvider* path_provider)
    : first_read_finished_(false),
      profile_(profile),
      compiled_blacklist_path_(
        profile->GetPath().Append(chrome::kPrivacyBlacklistFileName)),
      path_provider_(path_provider) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  registrar_.Add(this,
                 NotificationType::EXTENSION_LOADED,
                 Source<Profile>(profile));
  registrar_.Add(this,
                 NotificationType::EXTENSION_UNLOADED,
                 Source<Profile>(profile));
  ReadBlacklist();
}

void BlacklistManager::Observe(NotificationType type,
                               const NotificationSource& source,
                               const NotificationDetails& details) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  switch (type.value) {
    case NotificationType::EXTENSION_LOADED:
    case NotificationType::EXTENSION_UNLOADED:
      CompileBlacklist();
      break;
    default:
      NOTREACHED();
      break;
  }
}

void BlacklistManager::CompileBlacklist() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
      NewRunnableMethod(this, &BlacklistManager::DoCompileBlacklist,
                        path_provider_->GetPersistentBlacklistPaths()));
}

void BlacklistManager::DoCompileBlacklist(
    const std::vector<FilePath>& source_blacklists) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));

  bool success = true;

  Blacklist blacklist;
  std::string error_string;

  for (std::vector<FilePath>::const_iterator i = source_blacklists.begin();
       i != source_blacklists.end(); ++i) {
    if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) {
      success = false;
      break;
    }
  }

  // Only overwrite the current compiled blacklist if we read all source
  // files successfully.
  if (success)
    success = BlacklistIO::WriteBinary(&blacklist, compiled_blacklist_path_);

  ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
      NewRunnableMethod(this, &BlacklistManager::OnBlacklistCompilationFinished,
                        success));
}

void BlacklistManager::OnBlacklistCompilationFinished(bool success) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  if (success) {
    ReadBlacklist();
  } else {
    string16 error_message(ASCIIToUTF16("Blacklist compilation failed."));
    NotificationService::current()->Notify(
        NotificationType::BLACKLIST_MANAGER_ERROR,
        Source<Profile>(profile_),
        Details<string16>(&error_message));
  }
}

void BlacklistManager::ReadBlacklist() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
      NewRunnableMethod(this, &BlacklistManager::DoReadBlacklist,
                        path_provider_->GetTransientBlacklistPaths()));
}

void BlacklistManager::DoReadBlacklist(
    const std::vector<FilePath>& transient_blacklists) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));

  scoped_ptr<Blacklist> blacklist(new Blacklist);
  if (!BlacklistIO::ReadBinary(blacklist.get(), compiled_blacklist_path_)) {
    ReportBlacklistReadResult(NULL);
    return;
  }

  std::string error_string;
  std::vector<FilePath>::const_iterator i;
  for (i = transient_blacklists.begin();
       i != transient_blacklists.end(); ++i) {
    if (!BlacklistIO::ReadText(blacklist.get(), *i, &error_string)) {
      ReportBlacklistReadResult(NULL);
      return;
    }
  }

  ReportBlacklistReadResult(blacklist.release());
}

void BlacklistManager::ReportBlacklistReadResult(Blacklist* blacklist) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
  ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
      NewRunnableMethod(this, &BlacklistManager::OnBlacklistReadFinished,
                        blacklist));
}

void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));

  if (!blacklist) {
    if (!first_read_finished_) {
      // If we're loading for the first time, the compiled blacklist could
      // just not exist. Try compiling it once.
      first_read_finished_ = true;
      CompileBlacklist();
    } else {
      string16 error_message(ASCIIToUTF16("Blacklist read failed."));
      NotificationService::current()->Notify(
          NotificationType::BLACKLIST_MANAGER_ERROR,
          Source<Profile>(profile_),
          Details<string16>(&error_message));
    }
    return;
  }
  first_read_finished_ = true;
  compiled_blacklist_.reset(blacklist);

  NotificationService::current()->Notify(
      NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED,
      Source<Profile>(profile_),
      Details<Blacklist>(blacklist));
}