File: sdch_test_util.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (107 lines) | stat: -rw-r--r-- 3,651 bytes parent folder | download
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
// Copyright 2015 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 "sdch_test_util.h"

#include <string>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "base/macros.h"
#include "components/cronet/android/test/cronet_test_util.h"
#include "jni/SdchObserver_jni.h"
#include "net/base/sdch_manager.h"
#include "net/base/sdch_observer.h"
#include "net/url_request/url_request_context.h"
#include "url/gurl.h"

using base::android::JavaParamRef;

namespace cronet {

namespace {

class TestSdchObserver : public net::SdchObserver {
 public:
  TestSdchObserver(
      const GURL& target_url,
      net::SdchManager* manager,
      const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref)
      : target_url_(target_url), manager_(manager) {
    jsdch_observer_ref_.Reset(jsdch_observer_ref);
  }

  // SdchObserver implementation
  void OnDictionaryAdded(const GURL& dictionary_url,
                         const std::string& server_hash) override {
    // Only notify if the dictionary for the |target_url_| has been added.
    if (manager_->GetDictionarySet(target_url_)) {
      JNIEnv* env = base::android::AttachCurrentThread();
      Java_SdchObserver_onDictionaryAdded(env, jsdch_observer_ref_);
      manager_->RemoveObserver(this);
      delete this;
    }
  }

  void OnDictionaryRemoved(const std::string& server_hash) override {}

  void OnDictionaryUsed(const std::string& server_hash) override {}

  void OnGetDictionary(const GURL& request_url,
                       const GURL& dictionary_url) override {}

  void OnClearDictionaries() override {}

 private:
  GURL target_url_;
  net::SdchManager* manager_;
  base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref_;

  DISALLOW_COPY_AND_ASSIGN(TestSdchObserver);
};

void AddSdchObserverHelper(
    const GURL& target_url,
    const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref,
    jlong jadapter) {
  JNIEnv* env = base::android::AttachCurrentThread();
  net::URLRequestContext* url_request_context =
      TestUtil::GetURLRequestContext(jadapter);
  // If dictionaries for |target_url| are already added, skip adding the
  // observer.
  if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) {
    Java_SdchObserver_onDictionarySetAlreadyPresent(env, jsdch_observer_ref);
    return;
  }

  url_request_context->sdch_manager()->AddObserver(new TestSdchObserver(
      target_url, url_request_context->sdch_manager(), jsdch_observer_ref));
  Java_SdchObserver_onAddSdchObserverCompleted(env, jsdch_observer_ref);
}

}  // namespace

void AddSdchObserver(JNIEnv* env,
                     const JavaParamRef<jobject>& jsdch_observer,
                     const JavaParamRef<jstring>& jtarget_url,
                     jlong jadapter) {
  base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref;
  // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to
  // use it across threads. |AddSdchObserverHelper| will acquire a new
  // JNIEnv before calling into Java.
  jsdch_observer_ref.Reset(env, jsdch_observer);

  GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url));
  TestUtil::RunAfterContextInit(jadapter,
                                base::Bind(&AddSdchObserverHelper, target_url,
                                           jsdch_observer_ref, jadapter));
}

bool RegisterSdchTestUtil(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace cronet