Chromium Code Reviews| Index: chrome/browser/safe_browsing/android_safe_browsing_api_handler.cc |
| diff --git a/chrome/browser/safe_browsing/android_safe_browsing_api_handler.cc b/chrome/browser/safe_browsing/android_safe_browsing_api_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c26ee0426b4495ebfb4c11c4c49a077acd010b18 |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/android_safe_browsing_api_handler.cc |
| @@ -0,0 +1,75 @@ |
| +// Copyright (c) 2012 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/safe_browsing/android_safe_browsing_api_handler.h" |
| + |
| +#include <string> |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +// TODO(nparker): Include JNI-generated file |
| + |
| +using content::BrowserThread; |
| + |
| +AndroidSafeBrowsingAPIHandler::AndroidSafeBrowsingAPIHandler() { |
| + j_context_ = base::android::GetApplicationContext(); |
| + DCHECK(j_context_); |
| + |
| + // TODO(nparker): Fetch API key pertaining to Chrome. |
| + api_key_ = "<todo-get-appropriate-api-key>"; |
| +} |
| + |
| +bool AndroidSafeBrowsingAPIHandler::StartURLCheck( |
| + const URLCheckCallback& callback, |
| + const GURL& url, |
| + const std::vector<SBThreatType>& threat_types) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + // Make copy of the callback to add a ref to it. OnURLCheckDone() |
| + // will delete this ptr, thus releasing the internal ref. |
|
mattm
2015/05/08 02:17:26
Stale comment? Also not sure talking about refs he
Nathan Parker
2015/05/12 01:04:01
Done.
|
| + URLCheckCallback* callback_ptr = new URLCheckCallback(callback); |
| + intptr_t callback_id_int = reinterpret_cast<intptr_t>(callback_ptr); |
| + jlong callback_id = static_cast<jlong>(callback_id_int); |
| + |
| + // TODO(nparker): Reduce logging before launch of this feature. |
| + VLOG(1) << "Starting check " << callback_id << " for URL " << url; |
| + |
| + // TODO(nparker): Implement this. |
| + // Convert threat types to API's enums. |
| + // Call JNI method. |
| + |
| + // Until we have the Java implementation, just invoke the callback in 500ms |
| + // for testing. |
| + return BrowserThread::PostDelayedTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&OnURLCheckDone, callback_id, true, jstring()), |
| + base::TimeDelta::FromMilliseconds(500)); |
| +} |
| + |
| +// Static. |
|
mattm
2015/05/08 02:17:26
nit: In chrome I've only ever seen these comments
Nathan Parker
2015/05/12 01:04:01
Done.
|
| +void AndroidSafeBrowsingAPIHandler::OnURLCheckDone(jlong callback_id, |
| + jboolean isSuccessful, |
| + jstring metadata) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!callback_id) { |
| + DCHECK(false) << "Bad callback_id"; |
| + return; |
|
mattm
2015/05/08 02:17:26
I don't think you should try to handle this failur
Nathan Parker
2015/05/12 01:04:01
Interesting. I may do that in a few other places
|
| + } |
| + VLOG(1) << "URLCheckDone invoked for check " << callback_id; |
| + |
| + // Convert java long long int to c++ pointer, take ownership. |
| + scoped_ptr<URLCheckCallback> callback( |
| + reinterpret_cast<URLCheckCallback*>(callback_id)); |
| + |
| + // TODO(nparker): |
| + // 1) Convert metadata to binary proto of MalwarePatternType so |
| + // SafeBrowsingUIManager::DisplayBlockingPage() can unmarshal it. |
| + // 2) Pick the "worst" threat type from metadata and map to SBThreatType. |
| + |
| + callback->Run(SB_THREAT_TYPE_SAFE, std::string()); |
| + VLOG(1) << "Done with check " << callback_id; |
| +} |